<template>
  <!-- 列表 -->
  <div>
    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col v-if="activeName === 'alreadyIqcInspection'" :span="1.5">
        <el-button
          type="success"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >质检结果导出</el-button
        >
      </el-col>
      <right-toolbar
        :show-search.sync="showSearch"
        :columns="columnsList"
        @queryTable="handleQuery"
        @saveSetting="getColumnsList"
      />
    </el-row>
    <AutoResizer :given-height="220">
      <template #default="{ height }">
        <el-table
          v-if="columnsList.length > 0"
          ref="multipleTable"
          v-loading="loading"
          :data="list"
          :height="height"
          border
        >
          <el-table-column
            v-if="columnsList[14].visible"
            :key="Math.random()"
            label="检验单号"
            align="center"
            prop="id"
            fixed="left"
            width="165"
          >
            <template slot-scope="scope">
              <el-button
                type="text"
                size="mini"
                @click="handleInspectDetails(scope.row)"
                >{{ scope.row.id }}</el-button
              >
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[0].visible"
            :key="Math.random()"
            label="物料批次"
            align="center"
            prop="materialLots"
            width="120"
            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[15].visible"
            :key="Math.random()"
            label="检验数量"
            align="center"
            prop="receiptQty"
          />
          <el-table-column
            v-if="columnsList[4].visible"
            :key="Math.random()"
            label="物料编码"
            align="center"
            prop="materialNo"
            width="120"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[5].visible"
            :key="Math.random()"
            label="物料名称"
            align="center"
            prop="materialName"
            width="120"
            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[17].visible"
            :key="Math.random()"
            label="审核人"
            align="center"
            prop="auditor"
          />
          <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[16].visible"
            :key="Math.random()"
            label="审核状态"
            align="center"
            prop="auditStatus"
            width="120"
          >
            <template slot-scope="scope">
              <dict-tag
                :type="DICT_TYPE.WMS_AUDIT_STATUS"
                :value="scope.row.auditStatus"
              />
            </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[2].visible"
            :key="Math.random()"
            label="采购订单"
            align="center"
            prop="purchaseOrderNo"
            width="120"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[19].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[18].visible"
            :key="Math.random()"
            label="审核时间"
            align="center"
            prop="auditTime"
            width="160"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.auditTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[20].visible"
            :key="Math.random()"
            label="检验人"
            align="center"
            prop="inspector"
            width="100"
          />
          <el-table-column
            v-if="columnsList[21].visible"
            :key="Math.random()"
            label="处理结果"
            align="center"
            prop="auditStatus"
          >
            <template slot-scope="scope">
              <dict-tag
                :type="DICT_TYPE.WMS_IQC_APPROVAL_STATUS"
                :value="scope.row.auditStatus"
              />
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[22].visible"
            :key="Math.random()"
            label="检验结果"
            align="center"
            prop="noPassType"
          >
            <template slot-scope="scope">
              <dict-tag
                :type="DICT_TYPE.WMS_INSPECT_RESULTS"
                :value="scope.row.noPassType"
              />
            </template>
          </el-table-column>
          <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-refresh"
                size="mini"
                @click="callBack(scope.row)"
                >撤回</el-button
              >
              <!-- <el-button
                type="text"
                icon="el-icon-view"
                size="mini"
                @click="InspectDetails(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"
    />

    <!-- 检验详情查看 -->
    <!-- <InspectDetail ref="inspectDetail" /> -->
  </div>
</template>

<script>
import {
  getIqcInspectionPage,
  inspectionReBack,
} from "@/api/wms/quality/iqcInspectionExecute";
import { getFormColumnsList } from "@/api/system/saveColumns/index";
import { mapGetters } from "vuex";
import { isArray } from "min-dash";
import { exportQualityInspectionResults } from "@/api/wms/quality/iqcInspectionExecute";

export default {
  name: "AlreadyIqcInspection",
  components: {},
  props: {
    activeName: {
      type: String,
      default: "",
    },
    queryParamspro: {
      type: Object,
      default: () => {},
    },
  },
  inject: ["paramsQuery"],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      filePreviewvisible: false,
      // 总条数
      total: 0,
      // 领料单列表
      list: [],
      // 用户列表
      users: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      baseData: "",
      auditStatusSelect: null,
      //   是否显示查看弹出层
      viewOpen: false,
      advParams: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        materialNo: null,
        materialLots: null,
        // type: 3,
        audited: 1,
        processNo: "PURCHASE",
        id: null,
      },
      //   表单参数
      form: {},
      // 表单校验
      rules: {},
      // 显示隐藏列参数
      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"]),
  },
  created() {
    // 获取显隐列列表
    this.getColumnsList();
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      const params = {
        ...this.queryParams,
        ...this.paramsQuery,
        ...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");
    },
    handleName(row, column, cellValue) {
      const user = this.users.find((item) => item.id.toString() === cellValue);
      return user && user.nickname;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    // 撤回
    callBack({ id }) {
      this.loading = true;
      inspectionReBack({ inspectionId: id })
        .then((response) => {
          this.$message.success("已撤回");
          this.getList();
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 查看检验详情
    handleInspectDetails(row) {
      const {
        materialLots,
        materialNo,
        auditStatus,
        auditor,
        creator,
        processNo,
        unitCode,
      } = row;
      // this.$refs.inspectDetail.visible = true
      // this.$refs.inspectDetail.queryParams.materialLots = materialLots
      // this.$refs.inspectDetail.queryParams.materialNo = materialNo
      // this.$refs.inspectDetail.queryParams.type = 0
      // this.$refs.inspectDetail.apiStatus = auditStatus
      // this.$refs.inspectDetail.adjustForm.auditor = auditor
      // this.$refs.inspectDetail.adjustForm.inspector = creator
      // this.$refs.inspectDetail.queryParams.processNo = processNo

      // 跳转到检验执行
      this.$router.push({
        path: "/quality/iqcInspection/inspect-details",
        query: {
          auditStatus,
          unitCode,
          materialLots,
          materialNo,
          processNo,
          type: 0,
          auditor,
          creator,
        },
      });
    },
    // 获取显示隐藏列
    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);
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      const params = {
        ...this.queryParams,
        ...this.paramsQuery,
        ...this.advParams,
      };
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal
        .confirm("是否确认导出质检结果数据?")
        .then(() => {
          this.exportLoading = true;
          return exportQualityInspectionResults(params);
        })
        .then((response) => {
          this.$download.excel(response, "质检结果.xls");
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>

<style></style>
