<template>
  <!-- 列表 -->
  <div>
    <AutoResizer :given-height="250">
      <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[7].visible"
            :key="Math.random()"
            label="检验单号"
            align="center"
            prop="headerId"
            width="180"
          />
          <el-table-column
            v-if="columnsList[10].visible"
            :key="Math.random()"
            label="工序"
            align="center"
            prop="processName"
            width="120"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[0].visible"
            :key="Math.random()"
            label="产品编码"
            align="center"
            prop="materialNo"
            width="130"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[1].visible"
            :key="Math.random()"
            label="产品名称"
            align="center"
            prop="materialName"
            min-width="150"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[4].visible && (paramsQuery.queryType === 'first' || paramsQuery.queryType === 'firstControl')"
            :key="Math.random()"
            label="首件是否管控"
            align="center"
            prop="firstInspectionControl"
            width="120"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <dict-tag :type="DICT_TYPE.WMS_IPQC_FIRST_INSPECTION_CONTROL" :value="scope.row.firstInspectionControl" />
            </template>
          </el-table-column>
          <el-table-column
            v-if="paramsQuery.queryType === 'single' && columnsList[11].visible"
            :key="Math.random()"
            label="审核人"
            align="center"
            prop="auditor"
            width="120"
          />
          <el-table-column
            v-if="paramsQuery.queryType !== 'merge' && columnsList[5].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="ipqcDate"
            width="160"
          />
          <el-table-column
            v-if="paramsQuery.queryType === 'single' && columnsList[12].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="paramsQuery.queryType === 'single' && columnsList[9].visible"
            :key="Math.random()"
            label="检验人"
            align="center"
            prop="inspector"
            width="100"
          />
          <!-- <el-table-column
            v-if="paramsQuery.queryType === 'single' && columnsList[13].visible"
            :key="Math.random()"
            label="检验结果"
            align="center"
            prop="inspection"
            width="120"
          >
            <template slot-scope="scope">
              <dict-tag :type="DICT_TYPE.WMS_TEST_RESULT" :value="scope.row.inspection" />
            </template>
          </el-table-column> -->
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
            width="140"
            fixed="right"
          >
            <template slot-scope="scope">

              <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" />
    <SingleInspect ref="singleInspect" />
  </div>

</template>

<script>
import { getIpqcPage } from '@/api/wms/quality/ipqcInspectionExecute'
import InspectDetail from './InspectDetail.vue'
import SingleInspect from './SingleInspect.vue'
import { mapGetters } from 'vuex'
import { getFormColumnsList } from '@/api/system/saveColumns/index'
import { isArray } from 'min-dash'

export default {
  name: 'AlreadyIqcInspection',
  components: {
    InspectDetail,
    SingleInspect
  },
  props: {
    'activeName': {
      type: String,
      default: ''
    },
    'queryParamspro': {
      type: Object,
      default: () => {}
    }
  },
  inject: ['paramsQuery'],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      filePreviewvisible: false,
      // 总条数
      total: 0,
      // 领料单列表
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      baseData: '',
      auditStatusSelect: null,
      //   是否显示查看弹出层
      viewOpen: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        materialNo: null,
        materialLots: null,
        auditStatus: 1,
        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: true },
        { key: 11, label: '审核人', visible: true },
        { key: 12, label: '审核时间', visible: true },
        { key: 13, label: '检验结果', visible: true },
        { key: 14, label: '巡检时间', visible: true }
      ],
      // 显示隐藏持久化参数
      columnsList: [],
      // 高级查询参数
      advParams: {}
    }
  },
  computed: {
    ...mapGetters([
      'userId'
    ])
  },
  created() {
    // 获取显隐列列表
    this.getColumnsList()
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      const params = {
        ...this.queryParams,
        ...this.paramsQuery,
        ...this.advParams
      }
      getIpqcPage(params).then(response => {
        this.list = response.data.list
        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.$nextTick(() => {
        this.$refs.multipleTable.doLayout()
        this.getList()
      })
    },
    // 查看检验详情
    InspectDetails(row) {
      // const { queryType } = this.paramsQuery
      const { headerId, materialNo, auditStatus, auditor, creator, processNo, materialLots, queryType } = row

      // 设置公共参数
      const setCommonParams = (targetRef, refType) => {
        // 表体参数
        targetRef.queryParams.queryType = queryType
        targetRef.queryParams.materialLots = materialLots
        targetRef.queryParams.headerId = headerId

        targetRef.queryParams.materialNo = materialNo
        targetRef.queryParams.materialLotsHead = headerId
        targetRef.apiStatus = auditStatus
        targetRef.formParams.materialNo = materialNo
        targetRef.queryParams.type = 3
        targetRef.queryParams.processNo = processNo
        console.log(refType)
        if (refType === 'this.$refs.inspectDetail') {
          targetRef.adjustForm.auditor = auditor
          targetRef.adjustForm.inspector = creator
        } else if (refType === 'this.$refs.singleInspect') {
          targetRef.queryParams.headerId = materialLots
          targetRef.queryParams.materialLotsHead = materialLots
        }
      }

      // 表体参数
      // this.$refs.inspectDetail.queryParams.queryType = queryType
      // this.$refs.inspectDetail.queryParams.materialLots = materialLots
      // this.$refs.inspectDetail.queryParams.headerId = headerId

      // this.$refs.inspectDetail.queryParams.materialNo = materialNo
      // this.$refs.inspectDetail.queryParams.materialLotsHead = headerId
      // this.$refs.inspectDetail.apiStatus = auditStatus
      // this.$refs.inspectDetail.formParams.materialNo = materialNo
      // this.$refs.inspectDetail.queryParams.type = 3
      // this.$refs.inspectDetail.queryParams.processNo = processNo
      // this.$refs.inspectDetail.adjustForm.auditor = auditor
      // this.$refs.inspectDetail.adjustForm.inspector = creator
      if (['single', 'first', 'firstControl'].includes(queryType)) {
        // this.$refs.singleInspect.queryParams.headerId = materialLots
        // this.$refs.singleInspect.queryParams.queryType = queryType
        // this.$refs.singleInspect.queryParams.materialLots = materialLots

        // this.$refs.singleInspect.queryParams.materialNo = materialNo
        // this.$refs.singleInspect.queryParams.materialLotsHead = materialLots
        // this.$refs.singleInspect.apiStatus = auditStatus
        // this.$refs.singleInspect.formParams.materialNo = materialNo
        // this.$refs.singleInspect.queryParams.type = 3
        // this.$refs.singleInspect.queryParams.processNo = processNo

        setCommonParams(this.$refs.singleInspect, 'this.$refs.singleInspect')
        this.$refs.singleInspect.visible = true
        this.$refs.inspectDetail.visible = false
      } else {
        setCommonParams(this.$refs.inspectDetail, 'this.$refs.inspectDetail')
        this.$refs.inspectDetail.visible = true
        this.$refs.singleInspect.visible = 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
          // 对比默认数据和接口数据，将缺失的列添加到接口数据中
          this.columnsList = this.columns.reduce((acc, defaultColumn) => {
            const found = res.data.find(apiColumn => apiColumn.key === defaultColumn.key)
            if (!found) {
              acc.push({ ...defaultColumn, vueForm: componentName, userId: this.userId, id: null })
            }
            return acc
          }, res.data)
        } else {
          this.columnsList = this.columns
        }
      }).catch((err) => {
        if (err) {
          this.columnsList = this.columns
        }
      }).finally(() => {
        this.$emit('columns', this.columnsList)
      })
    }
  }
}
</script>

      <style>

      </style>

