<template>
  <el-dialog
    title="请选择要退的刀模版"
    v-if="showFlag"
    :visible.sync="showFlag"
    :modal="true"
    width="80%"
    append-to-body
    :destroy-on-close="true"
    :close-on-click-modal="false"
  >
    <el-form :model="queryParams" @submit.native.prevent ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="工作单元名称" prop="workunitName">
        <el-input
          v-model="queryParams.workunitName"
          placeholder="请输入工作单元名称"
          style="width:220px;"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="刀模SN" prop="toolCode">
        <el-input
          v-model="queryParams.toolCode"
          placeholder="请输入刀模SN"
          style="width:220px;"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="itemList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="物料编码" align="center" prop="itemCode" />
      <el-table-column  label="物料名称" align="center" prop="itemName" />
      <el-table-column label="SN号" align="center" prop="toolCode" />
      <el-table-column label="工序" align="center" prop="processId" />
      <el-table-column label="工作单元名称" align="center" prop="workunitName" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="showFlag = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listRecords } from "@/api/mes/tm/toolmachinesrecord";
import { genCode } from "@/api/system/autocode/rule";
import { getToken } from "@/utils/auth";
import { treeselect } from "@/api/mes/md/itemtype";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "MdItemSingle",
  dicts: ["sys_yes_no", "mes_item_product"],
  components: { Treeselect },
  props:{
    toolRequestItemList:{
      type:Array
    }
  },
  data() {
    return {
      showFlag: false,
      // 选中数组
      ids: [],
      selectedRows: [],
      // 非单个禁用
      single: true,

      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料产品表格数据
      itemList: null,
      // 部门树选项
      itemTypeOptions: undefined,
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        typeStr: '',
        workunitName: null,
        toolCode: null,
        warehouseStr: '1'
      },
    };
  },
  watch: {
    // 根据名称筛选分类树
    showFlag: {
      handler (newName, oldName) {
        console.log(newName, 'newName')
        if (newName) {
          this.getList();
        }
      },
      immediate: true
    }
  },
  created() {

  },
  mounted() {
    // this.getList();
  },
  methods: {
    /** 查询物料编码列表 */
    getList() {
      this.loading = true;
      const params = JSON.parse(JSON.stringify(this.queryParams))
      if (!params.workunitName) delete params.workunitName
      if (!params.toolCode) delete params.toolCode
      listRecords(params).then((response) => {
        this.itemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        typeStr: '',
        workunitName: null,
        toolCode: null,
        warehouseStr: '1'
      }
      this.handleQuery();
    },
     // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.itemId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
      this.selectedRows = selection;
    },
    //确定选中
    confirmSelect(){
      if(this.ids.length==0) {
          this.$notify({
              title:'提示',
              type:'warning',
              message: '请至少选择一条数据!'
          });
          return;
      }
      let a=0;
      if(!this.toolRequestItemList.length==0){
        
        this.toolRequestItemList.forEach(item => {
          this.selectedRows.forEach(items =>{
            if(item.toolCode === items.toolCode){
              this.$message.error(item.toolCode+'数据已经被选择,请不要重复选择');
              a++;
            }
          });
        });
       if(!a==0){
        return;
       }
      }

      
      
      console.log(this.selectedRows);
      this.$emit('onSelected',this.selectedRows);
      this.showFlag = false;
    }
  },
};
</script>
