<template>
  <el-card class="cards">
    <div slot="header">
      <span class="card-title">
        分切批次来源
      </span>
      <!-- <el-col :span="1.5"> -->
      <el-button style="float: right; padding: 3px 0" type="text" @click="handleToSearch">搜索</el-button>
      <el-button style="float: right; padding: 3px 0; margin-right: 6px; color:#909399; " type="text" @click="resetQuery">重置</el-button>
      <!-- </el-col> -->

    </div>
    <!-- 列表 -->
    <el-table
      ref="multipleTable"
      v-loading="loading"
      :data="list"
      border
      :row-key="getOnlyRowKey"
      max-height="400px"
      @selection-change="tableSelected"
      @select="select"
      @select-all="selectAll"
    >
      <el-table-column
        type="selection"
        width="39"
        fixed="left"
        :reserve-selection="true"
        :selectable="selected"
        align="center"
      />
      <el-table-column
        label="批次号"
        align="center"
        prop="batchNumber"
        min-width="170"
        fixed="left"
        show-overflow-tooltip
      />
      <el-table-column
        label="物料批次"
        align="center"
        prop="materialLots"
        min-width="140"
        show-overflow-tooltip
      />
      <el-table-column
        label="规格"
        align="center"
        prop="size"
        min-width="80"
        show-overflow-tooltip
      />
      <el-table-column
        label="单位"
        align="center"
        prop="unitCode"
        min-width="60"
        show-overflow-tooltip
      />
      <el-table-column
        label="长度(m)"
        align="center"
        prop="length"
        min-width="60"
        show-overflow-tooltip
      />
      <el-table-column
        label="宽度(mm)"
        align="center"
        prop="width"
        min-width="60"
        show-overflow-tooltip
      />
      <el-table-column
        label="厚度(mm)"
        align="center"
        prop="thickness"
        min-width="60"
        show-overflow-tooltip
      />
      <el-table-column
        label="批次面积(㎡)"
        align="center"
        prop="qty"
        min-width="80"
        show-overflow-tooltip
      />
      <el-table-column
        label="分切状态"
        align="center"
        prop="ifSlit"
        min-width="90"
      >
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.WMS_SLITTING_STATUS" :value="scope.row.ifSlit" />
        </template>
      </el-table-column>
      <el-table-column
        label="生产日期"
        align="center"
        prop="produceDate"
        min-width="120"
        show-overflow-tooltip
      />
      <el-table-column
        label="有效日期"
        align="center"
        prop="validTill"
        min-width="120"
        show-overflow-tooltip
      />
    </el-table>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog
      title="搜索"
      :visible.sync="open"
      width="800px"
    >
      <el-form
        v-show="showSearch"
        ref="queryForm"
        :model="queryParams"
        size="small"
        label-width="98px"
      >
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="批次号" prop="batchNumber">
              <el-input
                v-model="queryParams.batchNumber"
                placeholder="请输入批次号"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最小宽度" prop="minWidth">
              <el-input
                v-model="queryParams.minWidth"
                placeholder="请输入最小宽度"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最大宽度" prop="maxWidth">
              <el-input
                v-model="queryParams.maxWidth"
                placeholder="请输入最大宽度"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="有效期排序" prop="validTillSort">
              <!-- <el-input
                v-model="queryParams.validTillSort"
                placeholder="请输入有效期排序"
                clearable
                @keyup.enter.native="handleQuery"
              /> -->
              <DictSelect v-model="queryParams.validTillSort" placeholder="请选择有效期排序" dict-type="WMS_IS_OVER_DATE_SORT" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="宽度排序" prop="widthSort">
              <!-- <el-input
                v-model="queryParams.widthSort"
                placeholder="请输入宽度排序"
                clearable
                @keyup.enter.native="handleQuery"
              /> -->
              <DictSelect v-model="queryParams.widthSort" placeholder="请选择宽度排序" dict-type="WMS_IS_OVER_DATE_SORT" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产日期" prop="produceDate">
              <el-date-picker
                v-model="queryParams.produceDate"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :default-time="['00:00:00', '23:59:59']"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="有效期" prop="validTill">
              <el-date-picker
                v-model="queryParams.validTill"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :default-time="['00:00:00', '23:59:59']"
              />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="open = false">取 消</el-button>
        <el-button type="primary" @click="handleQuerySource">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
import { getSlittingLotsList } from '@/api/wms/inLibrary/slittingOrder'

export default {
  name: 'SlittingOrder',
  components: {
  },
  inject: ['paramsQuery'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      isEdit: false,
      // 点击的单元格
      tableClickIndex: null,
      // 总条数
      total: 0,
      // 分切任务单列表
      list: [],
      currentRows: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 是否选择了材料汇总
      isSelectedMaterial: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        eBillNo: null,
        alitStatus: null,
        materialNo: null,
        slitOrderNo: null,
        createTime: [],
        validTill: [],
        produceDate: [],
        batchNumber: null,
        minWidth: null,
        maxWidth: null,
        validTillSort: null,
        widthSort: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      currentRow: {}
    }
  },
  created() {
    this.queryParams.materialNo = this.$route.query.materialNo
    this.queryParams.slitOrderNo = this.$route.query.ebillNo
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      getSlittingLotsList(this.queryParams).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.loading = false
      })
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        eBillNo: undefined,
        alitStatus: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 表格选中
    tableSelected(val) {
      if (val.length > 0 && Object.keys(this.currentRow).length === 0) {
        this.$emit('batchSourceList', val)
        const [row] = val
        this.currentRow = row
        return
      }

      if (val.length > 0) {
        this.currentRow.id = null
        const [row] = val
        if (row.id !== this.currentRow.id) {
          this.$emit('batchSourceList', val)
          this.currentRow = row
        }
      }
    },
    select(selection, row) {
      if (selection.length > 1) {
        const delRow = selection.shift()
        this.$refs.multipleTable.toggleRowSelection(delRow, false)
      }
    },
    selectAll(selection) {
      if (selection.length > 1) {
        selection.length = 1
      }
    },
    // 多选框禁用
    selected(row) {
      return this.isSelectedMaterial
    },
    // 表格的唯一键
    getOnlyRowKey(row) {
      return row.id
    },
    // 取消勾选
    handleCancelSelect() {
      this.$refs.multipleTable.clearSelection()
    },
    // 弹出查询框
    handleToSearch() {
      this.open = true
    },
    // 查询母卷
    handleQuerySource() {
      this.open = false
      this.handleQuery()
    }
  }
}
</script>

<style scoped>
.card-title {
  font-size: 14px;
}

</style>
