<template>
  <!-- 列表 -->
  <div>
    <AutoResizer :given-height="250">
      <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="materialNo"
            min-width="150"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[1].visible"
            :key="Math.random()"
            label="产品名称"
            align="center"
            prop="materialName"
            width="150"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[2].visible"
            :key="Math.random()"
            label="产线名称"
            align="center"
            prop="productionName"
            min-width="130"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[3].visible"
            :key="Math.random()"
            label="工单号"
            align="center"
            prop="mesHotPressId"
            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"
          >
            <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="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[6].visible"
            :key="Math.random()"
            label="巡检日期"
            align="center"
            prop="ipqcDate"
            width="160"
          />
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
            width="120"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                type="text"
                icon="el-icon-magic-stick"
                size="mini"
                @click="handleInspection(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" @complete="inspectRefresh" />
    <SingleInspect ref="singleInspect" @complete="submitComplete" />
  </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: 'AwaitIqcInspection',
  components: {
    InspectDetail,
    SingleInspect
  },
  props: {
    'activeName': {
      type: String,
      default: ''
    },
    'queryParamspro': {
      type: Object,
      default: () => {}
    }
  },
  inject: ['paramsQuery'],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 检验选择
      open: false,
      // 总条数
      total: 0,
      // 领料单列表
      list: [],
      // 弹出层标题
      title: '',
      //   是否显示查看弹出层
      viewOpen: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        auditStatus: -1
      },
      excueParams: {},
      //   表单参数
      form: {
        code: ''
      },
      // 表单校验
      rules: {
        code: [{
          required: true,
          message: '检验类别不能为空',
          trigger: 'change'
        }]
      },
      // 高级查询参数
      advParams: {},
      // 显示隐藏列参数
      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: []
    }
  },
  computed: {
    ...mapGetters([
      'userId'
    ])
  },
  created() {
    // 获取显隐列列表
    this.getColumnsList()
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      const params = {
        ...this.paramsQuery,
        ...this.queryParams,
        ...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.getList()
    },
    // 检验执行
    handleInspection(row) {
      // const { queryType } = this.paramsQuery
      const { materialNo, auditStatus, processNo, headerId, 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
        if (refType === 'this.$refs.singleInspect') {
          targetRef.queryParams.headerId = materialLots
          targetRef.queryParams.queryType = queryType
          targetRef.queryParams.materialLotsHead = materialLots
        }
      }
      if (['single', 'first', 'firstControl'].includes(queryType)) {
        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
      }
    },
    // 送审后刷新
    inspectRefresh() {
      this.getList()
      this.$emit('inspectComplete')
    },

    submitComplete() {
      this.getList()
      this.$emit('submitComplete')
    },
    inspectClose() {
      this.form.code = undefined
    },
    // 获取显示隐藏列
    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>

