<template>
  <!-- 列表 -->
  <div>
    <AutoResizer :given-height="220">
      <template #default="{ height }">
        <el-table
          v-if="columnsList.length > 0"
          v-loading="loading"
          :data="list"
          :height="height"
          border
        >
          <el-table-column
            v-if="columnsList[0].visible"
            :key="Math.random()"
            label="物料批次"
            align="center"
            prop="materialLots"
            width="150"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[1].visible"
            :key="Math.random()"
            label="送货批次"
            align="center"
            prop="deliverBatch"
            width="150"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[2].visible"
            :key="Math.random()"
            label="采购订单"
            align="center"
            prop="purchaseOrderNo"
            width="120"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[3].visible"
            :key="Math.random()"
            label="采购数量"
            align="center"
            prop="materialQty"
          />
          <el-table-column
            v-if="columnsList[4].visible"
            :key="Math.random()"
            label="物料编码"
            align="center"
            prop="materialNo"
            width="150"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[5].visible"
            :key="Math.random()"
            label="物料名称"
            align="center"
            prop="materialName"
            width="150"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[23].visible"
            :key="Math.random()"
            label="图号"
            align="center"
            prop="drawingNo"
            width="150"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[24].visible"
            :key="Math.random()"
            label="客户料号"
            align="center"
            prop="customerMaterialNo"
            width="150"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[6].visible"
            :key="Math.random()"
            label="到货数量"
            align="center"
            prop="receiptQty"
          />
          <el-table-column
            v-if="columnsList[7].visible"
            :key="Math.random()"
            label="检验状态"
            align="center"
            prop="testStatus"
          >
            <template slot-scope="scope">
              <dict-tag
                :type="DICT_TYPE.WMS_TEST_STATUS"
                :value="scope.row.testStatus"
              />
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[8].visible"
            :key="Math.random()"
            label="单位"
            align="center"
            prop="unitCode"
          />
          <el-table-column
            v-if="columnsList[9].visible"
            :key="Math.random()"
            label="供应商"
            align="center"
            prop="supplierName"
            width="120"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[10].visible"
            :key="Math.random()"
            label="存储条件"
            align="center"
            prop="storageEnvironment"
            width="120"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[11].visible"
            :key="Math.random()"
            label="登记时间"
            align="center"
            prop="createTime"
            width="160"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[12].visible"
            :key="Math.random()"
            label="登记人"
            align="center"
            prop="creator"
          />
          <el-table-column
            v-if="columnsList[13].visible"
            :key="Math.random()"
            label="累计已到货数量"
            align="center"
            prop="receiveQty"
            width="120"
          />
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
            width="160"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                type="text"
                icon="el-icon-magic-stick"
                size="mini"
                @click="handleInspection(scope.row)"
                >检验执行</el-button
              >
              <el-button
                type="text"
                icon="el-icon-delete"
                size="mini"
                class="text-danger"
                @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </template>
    </AutoResizer>

    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 检验选择 -->
    <el-dialog
      v-dialogDrag
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
      @close="inspectClose"
    >
      <span> 该检验项还未新增方案，请确定是否去新增方案 </span>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 检验标准新增 -->
    <UpdateInspection
      ref="updateInspection"
      @addComplete="addComplete"
      @choiceComplete="getList"
    />
    <!-- <UpdateInspection ref="updateInspection" @updateCase="getList" @addComplete="getList" /> -->
  </div>
</template>

<script>
import {
  getIqcInspectionPage,
  createMaterialItemResult,
  deleteInspection,
} from "@/api/wms/quality/iqcInspectionExecute";
import UpdateInspection from "./UpdateInspection.vue";
import { getFormColumnsList } from "@/api/system/saveColumns/index";
import { mapGetters } from "vuex";
import { isArray } from "min-dash";

export default {
  name: "AwaitIqcInspection",
  components: {
    UpdateInspection,
  },
  props: {
    activeName: {
      type: String,
      default: "",
    },
    queryParamspro: {
      type: Object,
      default: () => {},
    },
  },
  inject: ["paramsQuery"],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 检验选择
      open: false,
      // 总条数
      total: 0,
      // 领料单列表
      list: [],
      //   用户列表
      users: [],
      // 弹出层标题
      title: "",
      //   是否显示查看弹出层
      viewOpen: false,
      advParams: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        auditStatus: -1,
      },
      excueParams: {},
      //   表单参数
      form: {
        code: "",
      },
      // 表单校验
      rules: {
        code: [
          {
            required: true,
            message: "检验类别不能为空",
            trigger: "change",
          },
        ],
      },
      // 显示隐藏列参数
      columns: [
        { key: 0, label: "物料批次", visible: true },
        { key: 1, label: "送货批次", visible: true },
        { key: 2, label: "采购订单", visible: true },
        { key: 3, label: "采购数量", visible: true },
        { key: 4, label: "物料编码", visible: true },
        { key: 5, label: "物料名称", visible: true },
        { key: 6, label: "到货数量", visible: true },
        { key: 7, label: "检验状态", visible: true },
        { key: 8, label: "单位", visible: true },
        { key: 9, label: "供应商", visible: true },
        { key: 10, label: "存储条件", visible: false },
        { key: 11, label: "登记时间", visible: true },
        { key: 12, label: "登记人", visible: true },
        { key: 13, label: "累计已到货数量", visible: true },
        { key: 14, label: "检验单号", visible: true },
        { key: 15, label: "检验数量", visible: true },
        { key: 16, label: "审核状态", visible: true },
        { key: 17, label: "审核人", visible: true },
        { key: 18, label: "审核时间", visible: true },
        { key: 19, label: "检验时间", visible: true },
        { key: 20, label: "检验人", visible: true },
        { key: 21, label: "处理结果", visible: true },
        { key: 22, label: "检验结果", visible: true },
        { key: 23, label: "图号", visible: true },
        { key: 24, label: "客户料号", visible: true },
      ],
      // 显示隐藏持久化参数
      columnsList: [],
    };
  },
  computed: {
    ...mapGetters(["userId"]),
  },
  watch: {
    // 监听路由参数的变化
    $route() {
      // 判断 id 是否存在并调用 getList 方法
      if (this.$route.query) {
        this.getList();
      }
    },
  },
  created() {
    // 获取显隐列列表
    this.getColumnsList();
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      const params = {
        ...this.paramsQuery,
        ...this.queryParams,
        ...this.advParams,
      };
      getIqcInspectionPage(params)
        .then((response) => {
          this.list = response.data.list;
          this.users = response.data.users;
          this.total = response.data.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        wmsIncomingReceiptId: undefined,
        orderType: undefined,
        erpStockId: undefined,
        wmsStoreAreaId: undefined,
        wmsStoreId: undefined,
        inQty: undefined,
        funitId: undefined,
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    // handleName(row, column, cellValue) {
    //   const user = this.users.find(item => item.id.toString() === cellValue)
    //   return user && user.nickname
    // },
    // 检验执行
    // async handleInspection(row) {
    //   const {
    //     materialNo,
    //     materialLots,
    //     masterId,
    //     groupMasterId,
    //     auditStatus,
    //     materialName,
    //     processNo,
    //     unitCode,
    //   } = row;
    //   this.excueParams = {
    //     materialLots,
    //     materialNo,
    //     processNo,
    //   };
    //   if (!masterId && !groupMasterId) {
    //     this.open = true;
    //     this.form.materialNo = materialNo;
    //     this.form.name = materialName;
    //     this.form.code = masterId;
    //   }
    //   if (!masterId && groupMasterId) {
    //     this.excueParams.wmsMaterialItemMasterId = groupMasterId;
    //   }
    //   if ((masterId && !groupMasterId) || (masterId && groupMasterId)) {
    //     this.excueParams.wmsMaterialItemMasterId = masterId;
    //   }
    //   createMaterialItemResult(this.excueParams).then(({ code, data, msg }) => {
    //     if (code === 0) {
    //       // 跳转到检验执行
    //       //  麦禾田  path: '/wmsquality/iqcInspection/inspect-details',
    //       // 东苏发  path: '/quality/iqcInspection/inspect-details',
    //       this.$router.push({
    //         path: "/quality/iqcInspection/inspect-details",
    //         query: {
    //           auditStatus,
    //           materialName,
    //           materialLots,
    //           materialNo,
    //           processNo,
    //           type: 0,
    //           unitCode,
    //         },
    //         replace: true,
    //       });
    //     } else {
    //       if (data?.code !== 0) {
    //         this.$modal.msgWarning(data?.msg);
    //         this.$refs.updateInspection.btnText = "add";
    //         this.$refs.updateInspection.visible = true;
    //         this.$refs.updateInspection.commitDisable = true;
    //         this.$refs.updateInspection.queryObj.version = "草稿";
    //         this.$refs.updateInspection.queryParams.type = 0;
    //         this.$refs.updateInspection.queryParams.materialNo = materialNo;
    //         this.$refs.updateInspection.queryParams.name = materialName;
    //         this.$refs.updateInspection.queryParams.processNo = "PURCHASE";
    //       }
    //       return;
    //     }
    //   });
    //   // this.$refs.inspectDetail.queryParams.materialLots = materialLots
    //   // this.$refs.inspectDetail.queryParams.materialNo = materialNo
    //   // this.$refs.inspectDetail.apiStatus = auditStatus
    //   // this.$refs.inspectDetail.formParams.materialNo = materialNo
    //   // this.$refs.updateInspection.queryParams.materialNo = materialNo
    //   // this.$refs.updateInspection.queryParams.name = name
    //   // this.$refs.inspectDetail.queryParams.processNo = processNo
    //   // this.$refs.inspectList.formParams = row
    //   // this.$refs.inspectDetail.queryParams.type = 0
    // },
    // 检验执行
    async handleInspection(row) {
      const {
        materialNo,
        materialLots,
        masterId,
        groupMasterId,
        auditStatus,
        materialName,
        processNo,
        unitCode,
      } = row;
      this.excueParams = {
        materialLots,
        materialNo,
        processNo,
      };
      if (!masterId && !groupMasterId) {
        this.open = true;
        this.form.materialNo = materialNo;
        this.form.name = materialName;
        this.form.code = masterId;
        return;
      }
      if (!masterId && groupMasterId) {
        this.excueParams.wmsMaterialItemMasterId = groupMasterId;
      }
      if ((masterId && !groupMasterId) || (masterId && groupMasterId)) {
        this.excueParams.wmsMaterialItemMasterId = masterId;
      }

      try {
        const { code, data, msg } = await createMaterialItemResult(
          this.excueParams
        );
        if (code === 0) {
          this.$router.push({
            path: "/quality/iqcInspection/inspect-details",
            query: {
              auditStatus,
              materialName,
              materialLots,
              materialNo,
              processNo,
              type: 0,
              unitCode,
            },
            replace: true,
          });
        } else {
          this.$modal.msgWarning(data?.msg);
          this.$refs.updateInspection.btnText = "add";
          this.$refs.updateInspection.visible = true;
          this.$refs.updateInspection.commitDisable = true;
          this.$refs.updateInspection.queryObj.version = "草稿";
          this.$refs.updateInspection.queryParams.type = 0;
          this.$refs.updateInspection.queryParams.materialNo = materialNo;
          this.$refs.updateInspection.queryParams.name = materialName;
          this.$refs.updateInspection.queryParams.processNo = "PURCHASE";
          return;
        }
      } catch (e) {
        this.$modal.msgError("调用检验接口失败");
        return;
      }
    },
    submitForm() {
      const { code, materialNo, name } = this.form;
      this.excueParams.wmsMaterialItemMasterId = code;
      this.$refs.updateInspection.btnText = "add";
      this.$refs.updateInspection.commitDisable = true;
      this.$refs.updateInspection.queryObj.version = "草稿";
      this.$refs.updateInspection.queryParams.type = 0;
      this.$refs.updateInspection.queryParams.materialNo = materialNo;
      this.$refs.updateInspection.queryParams.name = name;
      this.$refs.updateInspection.queryParams.processNo = "PURCHASE";
      this.$refs.updateInspection.visible = true;
    },
    // 删除检验项
    handleDelete(row) {
      const { id, materialLots } = row;
      this.$modal
        .confirm('是否确认物料批次为"' + materialLots + '"的数据项?')
        .then(function () {
          return deleteInspection(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    inspectRefresh() {
      this.getList();
      this.$emit("inspectComplete");
    },
    inspectClose() {
      this.form.code = undefined;
    },
    addComplete() {
      this.getList();
      this.open = false;
    },
    // 获取显示隐藏列
    getColumnsList() {
      const { componentName } = this.$route.meta || null;
      const params = {
        userId: this.userId,
        vueForm: componentName,
      };
      getFormColumnsList(params)
        .then((res) => {
          if (isArray(res.data)) {
            this.columnsList = res.data.length === 0 ? this.columns : res.data;
          } else {
            this.columnsList = this.columns;
          }
        })
        .catch((err) => {
          if (err) {
            this.columnsList = this.columns;
          }
        })
        .finally(() => {
          this.$emit("columns", this.columnsList);
        });
    },
  },
};
</script>

<style></style>
