<template>
  <el-dialog
    title="库存物料选择"
    append-to-body
    :visible.sync="dialogVisible"
    destroy-on-close
    :show-close="false"
    width="1300px"
    @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 }"
          clearable
          @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>
    <!--表格渲染-->
    <div style="padding: 10px" />
    <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 slot="left" type="info" @click="dialogVisible = false">关闭</el-button>
      <el-button slot="left" type="primary" @click="submit">保存</el-button>
    </span>
  </el-dialog>
</template>

<script>

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

export default {
  name: 'AddDtl',
  components: { rrOperation, pagination },
  cruds() {
    return CRUD({ title: '用户', idField: 'storagevehicleext_id', url: 'api/moveStor/getCanuseIvt',
      query: {
        struct_code: '',
        remark: '',
        sect_id: '',
        stor_id: ''
      },
      optShow: {
        add: false,
        edit: false,
        del: false,
        reset: true,
        download: false
      }})
  },
  mixins: [presenter(), header()],
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    openParam: {
      type: String
    },
    storId: {
      type: String
    },
    sectList: {
      type: Array
    }
  },
  dicts: ['ST_QUALITY_SCODE', 'ST_IVT_LEVEL', 'is_usable'],
  data() {
    return {
      dialogVisible: false,
      opendtlParam: '',
      sects: [],
      rows: [],
      defaultList: []
    }
  },
  watch: {
    dialogShow: {
      handler(newValue, oldValue) {
        this.dialogVisible = newValue
      }
    },
    openParam: {
      handler(newValue, oldValue) {
        this.opendtlParam = newValue
      }
    },
    sectList: {
      handler(newValue, oldValue) {
        this.defaultList === newValue
      }
    }
  },
  methods: {
    [CRUD.HOOK.beforeRefresh]() {
      this.crud.query.stor_id = this.storId
      if (this.storId === this.defaultList[0]) {
        this.crud.query.sect_id = this.defaultList[1]
      }
    },
    open() {
      crudSectattr.getSect({ 'stor_id': this.storId }).then(res => {
        this.sects = res.content
      })
      this.crud.toQuery()
    },
    close() {
      this.crud.resetQuery(false)
      this.$emit('update:dialogShow', false)
    },
    ivt_levelFormat(row, column) {
      return this.dict.label.ST_IVT_LEVEL[row.ivt_level]
    },
    is_activeFormat(row, column) {
      return this.dict.label.is_usable[row.is_active]
    },
    sectQueryChange(val) {
      if (val.length === 1) {
        this.crud.query.stor_id = val[0]
        this.crud.query.sect_id = ''
      }
      if (val.length === 0) {
        this.crud.query.sect_id = ''
        this.crud.query.stor_id = ''
      }
      if (val.length === 2) {
        this.crud.query.stor_id = val[0]
        this.crud.query.sect_id = val[1]
      }
      this.crud.toQuery()
    },
    submit() {
      this.rows = this.$refs.table.selection
      if (this.rows.length <= 0) {
        this.$message('请先勾选物料')
        return
      }
      this.$emit('update:dialogShow', false)
      this.$emit('tableChanged', this.rows)
      this.crud.resetQuery(false)
    }
  }
}
</script>

