<template>
  <el-dialog
    title="库存选择"
    append-to-body
    :visible.sync="dialogVisible"
    destroy-on-close
    width="1000px"
    @close="close"
    @open="open"
  >
    <!-- 搜索 -->
    <el-form
      :inline="true"
      class="demo-form-inline"
      label-position="right"
      label-width="80px"
      label-suffix=":"
    >
      <el-form-item label="库区查询">
        <el-cascader
          v-model="defaultList"
          placeholder="库区"
          :options="sects"
          :props="{ checkStrictly: true }"
          @change="sectQueryChange"
        />
      </el-form-item>
      <el-form-item label="货位编码">
        <el-input
          v-model="query.struct_code"
          clearable
          size="mini"
          placeholder="货位号模糊查询"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
      </el-form-item>
      <el-form-item label="物料编码">
        <el-input
          v-model="query.material_code"
          clearable
          size="mini"
          placeholder="物料"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
      </el-form-item>
      <el-form-item label="批次">
        <el-input
          v-model="query.pcsn"
          clearable
          size="mini"
          placeholder="批次"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
      </el-form-item>
      <rrOperation />
    </el-form>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      style="width: 100%;"
      border
      :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="turnout_sect_name" label="库区名称" :min-width="flexWidth('turnout_sect_name',crud.data,'库区名称')" />
      <el-table-column prop="turnout_struct_code" label="货位编码" :min-width="flexWidth('turnout_struct_code',crud.data,'货位编码')" />
      <el-table-column prop="storagevehicle_code" label="载具编码" :min-width="flexWidth('storagevehicle_code',crud.data,'载具编码')" />
      <el-table-column prop="material_code" label="物料编码" :min-width="flexWidth('material_code',crud.data,'物料编码')" />
      <el-table-column prop="material_name" label="物料名称" :min-width="flexWidth('material_name',crud.data,'物料名称')" />
      <el-table-column prop="pcsn" label="批次" :min-width="flexWidth('pcsn',crud.data,'批次')" />
      <el-table-column prop="qty" label="重量" :formatter="crud.formatNum3" :min-width="flexWidth('qty',crud.data,'重量')" />
      <el-table-column prop="qty_unit_name" label="重量单位" :min-width="flexWidth('qty_unit_name',crud.data,'重量单位')" />
    </el-table>
    <!--分页组件-->
    <pagination />
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>

import CRUD, { crud, header, presenter } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import crudSectattr from '@/views/wms/basedata/sectattr/sectattr'

export default {
  name: 'StructDiv',
  components: { crudOperation, rrOperation, pagination },
  cruds() {
    return CRUD({
      title: '库存物料',
      optShow: {},
      url: 'api/moveStor/getCanuseIvt',
      idField: 'struct_id',
      sort: 'storagevehicleext_id,desc'
    })
  },
  mixins: [presenter(), header()],
  dicts: ['d_lock_type'],
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    sectProp: {
      type: Array
    }
  },
  data() {
    return {
      sects: [],
      defaultList: [],
      dialogVisible: false,
      sect: {},
      mol_type: '',
      checkrow: {},
      rows: []
    }
  },
  watch: {
    dialogShow: {
      handler(newValue, oldValue) {
        this.dialogVisible = newValue
      }
    },
    sectProp: {
      handler(newValue, oldValue) {
        this.defaultList = newValue
      }
    }
  },
  methods: {
    open() {
      this.crud.resetQuery(false)
      crudSectattr.getSect({ 'stor_id': this.defaultList[0] }).then(res => {
        this.sects = res.data
      })
      if (this.sect) {
        this.query.sect = this.sect
        if (this.sect.length === 1) {
          this.query.stor_id = this.sect[0]
          this.query.sect_id = ''
        }
        if (this.sect.length === 0) {
          this.query.sect_id = ''
          this.query.stor_id = ''
        }
        if (this.sect.length === 2) {
          this.query.stor_id = this.sect[0]
          this.query.sect_id = this.sect[1]
        }
      }
      this.query.is_used = '1'
      this.query.is_delete = '0'
      this.query.stor_id = this.defaultList[0]
      this.crud.toQuery()
    },
    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]
      }
      this.crud.toQuery()
    },
    handleSelectionChange(val, row) {
      if (val.length > 1) {
        this.$refs.table.clearSelection()
        this.$refs.table.toggleRowSelection(val.pop())
        this.checkrow = row
      } else {
        this.checkrow = row
      }
    },
    /**
     * 接受父组件传值
     * @param msg
     */
    getMsg(msg) {
      this.mol_type = msg
    },
    lockFormat(row, column) {
      return this.dict.label.d_lock_type[row.lock_type]
    },
    onSelectAll() {
      this.$refs.table.clearSelection()
    },
    close() {
      this.sects = null
      this.$emit('update:dialogShow', false)
    },
    submit() {
      this.rows = this.$refs.table.selection
      if (this.rows.length <= 0) {
        this.$message('请先勾选仓位')
        return
      }
      this.rows.forEach((item) => {
        item.struct_code = item.turnout_struct_code
        item.sect_code = item.turnout_sect_code
        item.sect_name = item.turnout_sect_name
        item.base_qty = item.qty
      })
      this.$emit('update:dialogShow', false)
      this.$emit('tableChanged', this.rows)
    }
  }
}
</script>

