<template>
  <basic-container>
    <div class="search_box"></div>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      v-model="form"
      v-model:page="page"
      ref="crud"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template #menu-left>
        <el-button type="danger" @click="batchDel()">删除</el-button>
      </template>
      <template #menu-right>
        <el-button type="primary" @click="handleImport()">导入</el-button>
      </template>
      <template #menu="{ row }"> </template>

      <template #heatTreat="scope">
        <el-tag v-if="scope.row.afterPlating" type="success">是</el-tag>
        <el-tag v-else type="warning">否</el-tag>
      </template>
    </avue-crud>
  <!-- 导入 -->
    <basic-import v-if="isShowImport" title="导入" :isShow="isShowImport"
      templateUrl="/blade-desk/QA/CycleTestItem/download-excel-template"
      templateName="试验项目模板.xlsx"
      importUrl="/blade-desk/QA/CycleTestItem/import-excel"
     @closeDialog="closeDialog"></basic-import>
  </basic-container>
</template>

<script>
import {
  add,
  getRole,
  getRoleAlias,
  getRoleTreeById,
  grant,
  grantTree,
  remove,
  update,
} from "@/api/system/role";
import { mapGetters } from "vuex";
import { validatenull } from "@/utils/validate";
import basicImport from '@/components/basic-import/main.vue';
export default {
  components: {
    basicImport,
  },
  data() {
    return {
      isShowImport: false,
      inBatchesOpen: false,
      platingSmallOpen: false,
      planOrderArr: [],
      isOpen: false,
      rowItem: {},
      poId: null,
      form: {},
      box: false,
      props: {
        label: "title",
        value: "key",
      },
      menuGrantList: [],
      dataScopeGrantList: [],
      apiScopeGrantList: [],
      apiGrantList: [],
      menuTreeObj: [],
      dataScopeTreeObj: [],
      apiScopeTreeObj: [],
      selectionList: [],
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        columnSort: true,
        tip: false,
        height: "auto",
        calcHeight: 32,
        simplePage: false,
        searchShow: true,
        searchMenuSpan: 6,
        searchIcon: true,
        searchIndex: 3,
        tree: false,
        border: true,
        index: true,
        selection: false,
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        editBtnText: "修改",
        labelWidth: 120,
        menuWidth: 80,
        dialogWidth: 900,
        dialogClickModal: false,
        searchEnter: true,
        excelBtn: false,
        filterBtn: true,
        searchShowBtn: false,
        excelBtn: true,
        showOverflowTooltip: true,
        addBtnIcon: " ",
        viewBtnIcon: " ",
        delBtnIcon: " ",
        editBtnIcon: " ",
        gridBtn: false,
        searchLabelPosition: "left",
        searchGutter: 24,
        searchSpan: 6,
        menuAlign: "left",
        gridBtn: false,
        searchMenuPosition: "right",
        align: "center",
        column: [
          {
            label: "主加工单位",
            prop: "tsName",
            search: true,
            sortable: true,
            overHidden: true,
            fixed: "left",
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入主加工单位",
                trigger: "blur",
              },
            ],
          },
          
          {
            label: "当前工序",
            prop: "ppsName",
            fixed: "left",
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入当前工序",
                trigger: "blur",
              },
            ],
          },
          {
            label: "下一工序",
            prop: "nextPpsName",
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入当前工序",
                trigger: "blur",
              },
            ],
          },
          {
            label: "订单状态",
            prop: "runStatus",
            type: "select",
            search: true,
            sortable: true,
            overHidden: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入订单状态",
                trigger: "blur",
              },
            ],
            dicData: [
              {
                value: 2,
                label: "待生产",
              },
              {
                value: 3,
                label: "生产中",
              },
              {
                value: 4,
                label: "检验中",
              },
              {
                value: 6,
                label: "审理中",
              },
              {
                value: 5,
                label: "工艺变更",
              },
              {
                value: 19,
                label: "已返工",
              },
              {
                value: 15,
                label: "已完成",
              },
              {
                value: 21,
                label: "已作废",
              },
            ],
          },
          {
            label: "车间订单号",
            prop: "woCode",
            search: true,
            sortable: true,
            overHidden: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入车间订单号",
                trigger: "blur",
              },
            ],
          },
          {
            label: "加工单位",
            prop: "processingUnit",
            search: false,
            sortable: true,
            fixed: "left",
            overHidden: true,
            hide: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入加工单位",
                trigger: "blur",
              },
            ],
          },
          {
            label: "加工班组",
            prop: "tsId",
            search: true,
            sortable: true,
            overHidden: true,
            width: 200,
            hide: true,
            rules: [
              {
                required: true,
                message: "请输入加工单位-班组",
                trigger: "blur",
              },
            ],
          },
          {
            label: "加工外协商",
            prop: "ocId",
            search: true,
            sortable: true,
            overHidden: true,
            width: 200,
            hide: true,
            rules: [
              {
                required: true,
                message: "请输入加工单位-外协商",
                trigger: "blur",
              },
            ],
          },
          {
            label: "生产订单号",
            prop: "yoCode",
            search: true,
            sortable: true,
            overHidden: true,
            // hide: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入生产订单号",
                trigger: "blur",
              },
            ],
          },
          {
            label: "计划单号",
            prop: "poCode",
            search: true,
            sortable: true,
            // hide: true,
            overHidden: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入计划单号",
                trigger: "blur",
              },
            ],
          },
          {
            label: "返工单号",
            prop: "qcReworkCode",
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入返工单号",
                trigger: "blur",
              },
            ],
          },
          {
            label: "零件号",
            prop: "partCode",
            search: true,
            sortable: true,
            overHidden: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入零件号",
                trigger: "blur",
              },
            ],
          },
          {
            label: "批次号",
            prop: "batchNo",
            search: true,
            sortable: true,
            overHidden: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入批次号",
                trigger: "blur",
              },
            ],
          },
          {
            label: "流程卡号",
            prop: "cardNo",
            search: true,
            sortable: true,
            overHidden: true,
            hide: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入流程卡号",
                trigger: "blur",
              },
            ],
          },
          {
            label: "产品型号",
            prop: "productModel",
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入产品名称",
                trigger: "blur",
              },
            ],
          },
          {
            label: "产品名称",
            prop: "false",
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入产品名称",
                trigger: "blur",
              },
            ],
          },
          // {
          //   label: "计划下达时间",
          //   prop: "sendDownTime",
          //   search: true,
          //   sortable: true,
          //   overHidden: true,
          //   width: 200,
          //   rules: [
          //     {
          //       required: true,
          //       message: "请输入计划下达时间",
          //       trigger: "blur",
          //     },
          //   ],
          // },
           {
            label: "MES流程卡号",
            prop: "false",
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入MES流程卡号",
                trigger: "blur",
              },
            ],
          },
          {
            label: "生产标识",
            prop: "prodIdent",
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入生产标识",
                trigger: "blur",
              },
            ],
          },
          {
            label: "镀种信息",
            prop: "plate",
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入镀种信息",
                trigger: "blur",
              },
            ],
          },
          {
            label: "生产数量",
            prop: "makeQty",
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入生产数量",
                trigger: "blur",
              },
            ],
          },
          {
            label: "面积(dm²)",
            prop: "poArea",
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入面积(dm²)",
                trigger: "blur",
              },
            ],
          },
          
           {
            label: "单批次面积",
            prop: "batchArea",
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入单批次面积",
                trigger: "blur",
              },
            ],
          },
           {
            label: "订单优先级",
            prop: "priorityTitle",
            type: "select",
            search: true,
            sortable: true,
            overHidden: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入订单优先级",
                trigger: "blur",
              },
            ],
            dicData: [
              {
                value: 1,
                label: "正常-白色",
              },
              {
                value: 2,
                label: "项目要求日期急件-橙色",
              },
              {
                value: 3,
                label: "合同急件-黄色",
              },
              {
                value: 4,
                label: "绩效零件-青色",
              },
              {
                value: 5,
                label: "调度标注急件-蓝色",
              },
            ],
          },
          {
            label: "需求部门",
            prop: "useDept",
            search: true,
            sortable: true,
            overHidden: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入需求部门",
                trigger: "blur",
              },
            ],
          },
          {
            label: "需求交期",
            prop: "demandDate",
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入需求交期",
                trigger: "blur",
              },
            ],
          },
          {
            label: "会议要求周期",
            prop: "meetCycle",
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入会议要求周期",
                trigger: "blur",
              },
            ],
          },
          {
            label: "调度员",
            prop: "userName",
            search: true,
            sortable: true,
            overHidden: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入调度员",
                trigger: "blur",
              },
            ],
          },
          {
            label: "协作计划",
            prop: "collaborateText",
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入协作计划",
                trigger: "blur",
              },
            ],
          },
           {
            label: "计划下达时间",
            prop: "planTime",
            search: true,
            sortable: true,
            overHidden: true,
            type: "datetimerange",
            format: 'YYYY-MM-DD HH:mm:ss',
            valueFormat: 'YYYY-MM-DD HH:mm:ss',
            startPlaceholder: '时间日期开始',
            endPlaceholder: '时间日期结束',
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入计划下达时间",
                trigger: "blur",
              },
            ],
          }, {
            label: "计划完工时间",
            prop: "planEndDate",
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入计划完工时间",
                trigger: "blur",
              },
            ],
          },
          {
            label: "镀后入库时间",
            prop: "putStoreTime",
            search: true,
            sortable: true,
            overHidden: true,
            width: 200,
            type: "datetimerange",
            format: 'yyyy-MM-DD HH:mm:ss',
            valueFormat: 'yyyy-MM-DD HH:mm:ss',
            startPlaceholder: '时间日期开始',
            endPlaceholder: '时间日期结束',
            rules: [
              {
                required: true,
                message: "请输入镀后入库时间",
                trigger: "blur",
              },
            ],
          },
          
          {
            label: "外协状态",
            prop: "oemType",
            type: "select",
            search: false,
            hide: true,
            sortable: true,
            overHidden: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入外协状态",
                trigger: "blur",
              },
            ],
            dicData: [
              {
                value: -1,
                label: "场内",
              },
              {
                value: 2,
                label: "外协",
              },
            ],
          },
          // {
          //   label: "产品型号",
          //   prop: "productType",
          //   search: false,
          //   sortable: true,
          //   overHidden: true,
          //   width: 200,
          //   rules: [
          //     {
          //       required: true,
          //       message: "请输入产品型号",
          //       trigger: "blur",
          //     },
          //   ],
          // },
          {
            label: "生产数量",
            prop: "productionQuantity",
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入生产数量",
                trigger: "blur",
              },
            ],
          },
          {
            label: "入库数量",
            prop: "inventoryQty",
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入入库数量",
                trigger: "blur",
              },
            ],
          },
          {
            label: "试验数量",
            prop: "testQuantity",
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入试验数量",
                trigger: "blur",
              },
            ],
          },
          {
            label: "报废数量",
            prop: "scrapQuantity",
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入报废数量",
                trigger: "blur",
              },
            ],
          },
          
         
          
          // {
          //   label: "已入库数量",
          //   prop: "inventoryQty",
          //   search: false,
          //   sortable: true,
          //   overHidden: true,
          //   width: 200,
          //   rules: [
          //     {
          //       required: true,
          //       message: "请输入已入库数量",
          //       trigger: "blur",
          //     },
          //   ],
          // },
          // {
          //   label: "未入库数量",
          //   prop: "notInQty",
          //   search: false,
          //   sortable: true,
          //   overHidden: true,
          //   width: 200,
          //   rules: [
          //     {
          //       required: true,
          //       message: "请输入未入库数量",
          //       trigger: "blur",
          //     },
          //   ],
          // },
          // {
          //   label: "下序",
          //   prop: "nextPpsName",
          //   search: false,
          //   sortable: true,
          //   overHidden: true,
          //   width: 200,
          //   rules: [
          //     {
          //       required: true,
          //       message: "请输入下序",
          //       trigger: "blur",
          //     },
          //   ],
          // },
          // {
          //   label: "下一班组",
          //   prop: "nextTeamName",
          //   search: false,
          //   sortable: true,
          //   overHidden: true,
          //   width: 200,
          //   rules: [
          //     {
          //       required: true,
          //       message: "请输入下一班组",
          //       trigger: "blur",
          //     },
          //   ],
          // },
          // {
          //   label: "领料状态",
          //   prop: "pickingStatusTitle",
          //   search: true,
          //   sortable: true,
          //   overHidden: true,
          //   width: 200,
          //   rules: [
          //     {
          //       required: true,
          //       message: "请输入领料状态",
          //       trigger: "blur",
          //     },
          //   ],
          //   dicData: [
          //     {
          //       value: 0,
          //       label: "未领",
          //     },
          //     {
          //       value: 1,
          //       label: "已领",
          //     },
          //   ],
          // },
          // {
          //   label: "接收人员",
          //   prop: "failureMan",
          //   search: false,
          //   sortable: true,
          //   overHidden: true,
          //   width: 200,
          //   rules: [
          //     {
          //       required: true,
          //       message: "请输入接收人员",
          //       trigger: "blur",
          //     },
          //   ],
          // },
          
          {
            label: "备注",
            prop: "memo",
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入备注",
                trigger: "blur",
              },
            ],
          },
          // {
          //   label: "是否协作",
          //   prop: "collaborate",
          //   type: "select",
          //   hide: true,
          //   search: true,
          //   sortable: true,
          //   overHidden: true,
          //   width: 200,
          //   rules: [
          //     {
          //       required: true,
          //       message: "请输入是否协作",
          //       trigger: "blur",
          //     },
          //   ],
          //   dicData: [
          //     {
          //       value: true,
          //       label: "是",
          //     },
          //     {
          //       value: false,
          //       label: "否",
          //     },
          //   ],
          // },
        ],
      },

      data: [],
      isRushOpen: false, //加急弹框
      isBatchOpen: false, //分批处理
    };
  },
  methods: {
    // 点击导入按钮
    handleImport() {
      this.isShowImport = true
    },
    // 批量接收
    batchReceiving() {
      this.$confirm("确定将选择数据批量接收?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // return remove(row.id);
        })
        .then(() => {
          // this.onLoad(this.page);
          // this.$message({
          //     type: 'success',
          //     message: '操作成功!',
          // });
        });
    },
    // 批量关闭
    batchDel() {
      this.$confirm("确定将选择数据批量删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // return remove(row.id);
        })
        .then(() => {
          // this.onLoad(this.page);
          // this.$message({
          //     type: 'success',
          //     message: '操作成功!',
          // });
        });
    },
    // 加急
    rushFn() {
      this.isRushOpen = true;
    },
    closeDialog() {
      this.isRushOpen = false;
      this.isBatchOpen = false;
    },
    inBatchesFn(row) {
      this.rowItem = row;
      this.isBatchOpen = true;
    },
    subclass() {
      this.platingSmallOpen = true;
    },
    lookProcessRoute(row) {
      this.isOpen = true;
      this.rowItem = row;
    },
    cancel(refresh) {
      if (refresh) {
        this.$refs.myTable.load();
      }
      this.inBatchesOpen = false;
      this.isOpen = false;
      this.planFormOpen = false;
      this.platingSmallOpen = false;
    },
    initData(roleId) {
      getRoleTreeById(roleId).then((res) => {
        const column = this.findObject(this.option.column, "parentId");
        column.dicData = res.data.data;
      });
    },
    submit() {
      const menuList = this.$refs.treeMenu.getCheckedKeys();
      const dataScopeList = this.$refs.treeDataScope.getCheckedKeys();
      const apiScopeList = this.$refs.treeApiScope.getCheckedKeys();
      grant(this.idsArray, menuList, dataScopeList, apiScopeList).then(() => {
        this.box = false;
        this.$message({
          type: "success",
          message: "操作成功!",
        });
        this.selectionList = [];
        this.onLoad(this.page);
      });
    },
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },

    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    beforeOpen(done, type) {
      if (["add", "edit"].includes(type)) {
        this.initData(this.form.id);
      }
      if (["edit", "view"].includes(type)) {
        if (this.form.parentId === "0") {
          this.form.parentId = "";
        }
      }
      getRoleAlias().then((res) => {
        const column = this.findObject(this.option.column, "currentAlias");
        column.dicData = res.data.data;
      });
      done();
    },
    handleRole() {
      if (this.selectionList.length !== 1) {
        this.$message.warning("只能选择一条数据");
        return;
      }
      this.menuTreeObj = [];
      this.dataScopeTreeObj = [];
      this.apiScopeTreeObj = [];
      grantTree().then((res) => {
        this.menuGrantList = res.data.data.menu;
        this.dataScopeGrantList = res.data.data.dataScope;
        this.apiScopeGrantList = res.data.data.apiScope;
        getRole(this.ids).then((res) => {
          this.menuTreeObj = res.data.data.menu;
          this.dataScopeTreeObj = res.data.data.dataScope;
          this.apiScopeTreeObj = res.data.data.apiScope;
          this.box = true;
        });
      });
    },
    handleRowRole(row) {
      this.menuTreeObj = [];
      this.dataScopeTreeObj = [];
      this.apiScopeTreeObj = [];
      grantTree().then((res) => {
        this.menuGrantList = res.data.data.menu;
        this.dataScopeGrantList = res.data.data.dataScope;
        this.apiScopeGrantList = res.data.data.apiScope;
        getRole(row.id).then((res) => {
          this.menuTreeObj = res.data.data.menu;
          this.dataScopeTreeObj = res.data.data.dataScope;
          this.apiScopeTreeObj = res.data.data.apiScope;
          this.selectionList.push(row);
          this.box = true;
        });
      });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    handleChange(file, fileList) {
      // proxy.$Export.xlsx(file.raw).then((data) => {
      //     data.value = data.results;
      // });
      this.$message({
        type: "success",
        message: "操作成功!",
      });
    },

    onLoad(page, params = {}) {
      this.loading = true;
      this.data = [{}];
      this.page.total = this.data.length;
      //   getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
      //     this.data = res.data.data.records;
      this.loading = false;
      //     this.page.total = res.data.data.total
      //     this.selectionClear();
      //   });
    },
  },
  mounted() {},
};
</script>
