<template>
  <el-dialog
    title="任务修改"
    append-to-body
    :visible.sync="dialogVisible"
    destroy-on-close
    fullscreen
    width="1000px"
    @close="close"
    @open="open"
  >

    <el-row :gutter="20">
      <el-col :span="20" style="border: 1px solid white">
        <span />
      </el-col>
      <el-col :span="4">
        <span>
          <el-button icon="el-icon-check" size="mini" :loading="crud.cu === 2" type="primary" @click="submit">确定</el-button>
          <el-button icon="el-icon-close" size="mini" type="info" @click="dialogVisible = false">关闭</el-button>
        </span>
      </el-col>
    </el-row>

    <el-form ref="taskForm" style="border: 1px solid #cfe0df;margin-top: 10px;padding-top: 10px;" :inline="true" :model="form" size="mini" label-width="85px" label-suffix=":">
      <el-form-item label="任务号" prop="task_code">
        <label slot="label">任&nbsp;&nbsp;务&nbsp;号:</label>
        <el-input v-model="form.task_code" disabled style="width: 210px" />
      </el-form-item>
      <el-form-item label="存储载具" prop="storagevehicle_code">
        <el-input v-model="form.storagevehicle_code" disabled style="width: 210px" />
      </el-form-item>
      <el-form-item label="原入库点" prop="point_code">
        <el-input v-model="form.point_code" disabled style="width: 210px" />
      </el-form-item>

      <el-form-item label="入库点" prop="new_point_code">
        <label slot="label">入&nbsp;&nbsp;库&nbsp;点:</label>
        <el-select
          v-model="form.new_point_code"
          clearable
          placeholder="请选择"
          class="filter-item"
          style="width: 210px"
        >
          <el-option
            v-for="item in pointlist"
            :key="item.point_code"
            :label="item.point_code"
            :value="item.point_code"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="原货位" prop="struct_code">
        <label slot="label">原&nbsp;&nbsp;货&nbsp;位:</label>
        <el-input v-model="form.struct_code" disabled style="width: 210px" />
      </el-form-item>
      <el-form-item label="新货位" prop="new_struct_code">
        <label slot="label">新&nbsp;&nbsp;货&nbsp;位:</label>
        <el-input v-model="form.new_struct_code" disabled style="width: 210px" />
      </el-form-item>
      <el-form-item label="所属库区" prop="new_struct_code">
        <el-cascader
          v-model="query.sect"
          style="width: 210px"
          placeholder="所属库区"
          :options="sects"
          :props="{ checkStrictly: true }"
          clearable
          @change="sectQueryChange"
        />
      </el-form-item>
      <el-form-item label="模糊查询" prop="struct_code">
        <el-input
          v-model="query.search"
          clearable
          style="width: 200px"
          size="mini"
          placeholder="输入货位编码、名称"
          prefix-icon="el-icon-search"
          class="filter-item"
        />
      </el-form-item>
      <rrOperation />
    </el-form>

    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      style="width: 100%"
      :row-class-name="tableRowClassName"
      highlight-current-row
      border
      :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
      @row-click="clcikRow"
      @select="handleSelectionChange"
      @select-all="onSelectAll"
    >
      <el-table-column prop="struct_code" label="货位编码" />
      <el-table-column prop="struct_name" label="货位名称" />
      <el-table-column prop="material_height_type_name" label="高度类型" />
      <el-table-column prop="sect_name" label="库区名称" />
    </el-table>
    <!--分页组件-->
    <pagination />

  </el-dialog>
</template>

<script>

import CRUD, { header, presenter } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import crudStructattr from '@/views/wms/basedata/st/struct/structattr'
import crudSectattr from '@/views/wms/basedata/st/sect/sectattr'
import crudRawAssist from '@/views/wms/st/inbill/rawassist'
import crudPoint from '@/views/wms/sch/point'

export default {
  name: 'StructDiv',
  components: { crudOperation, rrOperation, pagination },
  cruds() {
    return CRUD({
      title: '仓位',
      optShow: {},
      url: 'api/structattr',
      idField: 'struct_id',
      sort: 'struct_id,desc',
      crudMethod: { ...crudStructattr }
    })
  },
  mixins: [presenter(), header()],
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    sectProp: {
      type: Object,
      default: null
    },
    bucketForm: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      sects: [],
      classes: [],
      pointlist: [],
      dialogVisible: false,
      sectProp: null,
      checkrow: {},
      form: {
        task_id: '',
        task_code: '',
        storagevehicle_code: '',
        point_code: '',
        new_point_code: '',
        struct_code: '',
        new_struct_code: ''
      },
      rows: []
    }
  },
  watch: {
    dialogShow: {
      handler(newValue, oldValue) {
        this.dialogVisible = newValue
      }
    },
    sectProp: {
      handler(newValue, oldValue) {
        this.sectProp = newValue
      }
    },
    bucketForm: {
      handler(newValue, oldValue) {
        this.form = newValue
      }
    }
  },
  methods: {
    [CRUD.HOOK.beforeRefresh]() {
      this.crud.query.lock_type = '00'
    },
    open() {
      crudSectattr.getSect({ 'is_materialstore': '1', 'sect_type_attr': '00' }).then(res => {
        this.sects = res.content
      })
      crudPoint.getPoint({ 'area_type': '21' }).then(res => {
        this.pointlist = res
      })
      if (this.sectProp) {
        this.query.sect = this.sectProp
        if (this.sectProp.length === 1) {
          this.query.stor_id = this.sectProp[0]
          this.query.sect_id = ''
        }
        if (this.sectProp.length === 0) {
          this.query.sect_id = ''
          this.query.stor_id = ''
        }
        if (this.sectProp.length === 2) {
          this.query.stor_id = this.sectProp[0]
          this.query.sect_id = this.sectProp[1]
        }
      }
      this.crud.toQuery()
    },
    clcikRow(row, column, event) {
      this.form.new_struct_code = row.struct_code
    },
    tableRowClassName({ row, rowIndex }) {
      row.index = rowIndex
    },
    sectQueryChange(val) {
      if (val.length === 1) {
        this.query.stor_id = val[0]
        this.query.sect_id = ''
      }
      if (val.length === 0) {
        this.query.sect_id = ''
        this.query.stor_id = ''
      }
      if (val.length === 2) {
        this.query.stor_id = val[0]
        this.query.sect_id = val[1]
      }
    },
    handleSelectionChange(val, row) {
      if (val.length > 1) {
        this.$refs.table.clearSelection()
        this.$refs.table.toggleRowSelection(val.pop())
      } else {
        this.checkrow = row
      }
    },
    onSelectAll() {
      this.$refs.table.clearSelection()
    },
    close() {
      this.crud.resetQuery()
      this.query.sect = null
      this.$emit('update:dialogShow', false)
    },
    submit() {
      this.crud.resetQuery()
      crudRawAssist.updateTask(this.form).then(res => {
        this.$emit('update:dialogShow', false)
        this.$emit('updateCommit', this.checkrow)
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-dialog__body {
  padding-top: 0px;
}
</style>

