<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[0].visible"
            :key="Math.random()"
            label="产品编码"
            align="center"
            prop="materialNo"
            width="150"
            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="130"
          >
            <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[8].visible"
            :key="Math.random()"
            label="送审时间"
            align="center"
            prop="inspectTime"
            width="160"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.inspectTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[14].visible&& (paramsQuery.queryType !== 'merge')"
            :key="Math.random()"
            label="巡检时间"
            align="center"
            prop="inspectionTime"
            width="160"
          />
          <el-table-column
            v-if="columnsList[9].visible"
            :key="Math.random()"
            label="检验人"
            align="center"
            prop="inspector"
            width="100"
          />
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
            width="80"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                type="text"
                icon="el-icon-view"
                size="mini"
                @click="handleDetails(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" @resetComplete="resetComplete" @complete="auditComplete" />
    <SingleInspect
      ref="singleInspect"
      @auditComplete="auditComplete"
      @resetComplete="resetComplete"
    />
  </div>

</template>

<script>
import { getIpqcPage } from '@/api/wms/quality/ipqcInspectionExecute'
// import { updateAuditStatus } from '@/api/wms/quality/inspectionResultApproval'
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: 'IpqcInspectionResultApproval',
  components: {
    InspectDetail,
    SingleInspect
  },
  inject: ['paramsQuery'],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 提交遮罩层
      submitLoading: false,
      // 检验选择
      open: false,
      // 总条数
      total: 0,
      // 领料单列表
      list: [],
      //   用户列表
      users: [],
      // 弹出层标题
      title: '',
      //   是否显示查看弹出层
      viewOpen: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        auditStatus: 0,
        materialLots: null,
        inQty: null,
        funitId: null,
        id: null
      },
      excueParams: {},
      //   表单参数
      form: {
        noPassType: '0'
      },
      // 表单校验
      rules: {
        noPassType: [
          { 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: 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
        this.users = response.data.users
      }).finally(() => {
        this.loading = false
      })
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        wmsIncomingReceiptId: undefined,
        orderType: undefined,
        inQty: undefined,
        funitId: undefined,
        remark: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 查看检验详情
    handleDetails(row) {
      const { headerId, materialNo, auditStatus, auditor, creator, processNo, materialLots, queryType, inspectionId } = row
      // 表体参数
      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
      this.$refs.inspectDetail.queryParams.inspectId = inspectionId
      if (queryType === 'single' || queryType === 'first' || queryType === 'firstControl') {
        this.$refs.singleInspect.queryParams.headerId = materialLots
        this.$refs.singleInspect.queryParams.queryType = queryType
        this.$refs.singleInspect.queryParams.materialLots = materialLots
        this.$refs.singleInspect.queryParams.inspectId = inspectionId
        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

        this.$refs.singleInspect.visible = true
        this.$refs.inspectDetail.visible = false
      } else {
        this.$refs.inspectDetail.visible = true
        this.$refs.singleInspect.visible = false
      }
    },
    // 批核后刷新
    auditComplete() {
      this.getList()
      this.$emit('alreadyComplete')
    },
    // 驳回重新检验
    resetComplete() {
      this.getList()
      this.$emit('resetComplete')
    },
    // 获取显示隐藏列
    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>

