<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="code"
            width="150"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[1].visible"
            :key="Math.random()"
            label="物料名称"
            align="center"
            prop="name"
            width="150"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[2].visible"
            :key="Math.random()"
            label="版本号"
            align="center"
            prop="version"
          />
          <el-table-column
            v-if="columnsList[3].visible"
            :key="Math.random()"
            label="提交人"
            align="center"
            prop="updater"
            width="100"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[4].visible"
            :key="Math.random()"
            label="审核状态"
            align="center"
            prop="auditStatus"
            width="100"
          >
            <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[5].visible"
            :key="Math.random()"
            label="启用标志"
            align="center"
            prop="status"
          >
            <template slot-scope="scope">
              <dict-tag :type="DICT_TYPE.WMS_ENABLE_FLAG" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[6].visible"
            :key="Math.random()"
            label="创建时间"
            align="center"
            prop="createTime"
            width="180"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
            width="240"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdateStandard(scope.row)"
              >修改检验标准</el-button>
              <el-button
                size="mini"
                type="text"
                class="text-danger"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >删除</el-button>
              <el-button
                v-hasPermi="['wms:material-additional-log:query']"
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handleLogs(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"
    />
    <!-- 明细 -->
    <StandardApprovalDetails ref="standardApprovalDetails" />
    <!-- 修改检验标准 -->
    <UpdateInspection ref="updateInspection" @addComplete="addComplete" @saveComplete="getList" />
    <!-- 审核日志 -->
    <ApprovalLogs ref="approvalLogs" />
  </div>

</template>

<script>
import { getMaterialAdditionalPage, ifChangeMasterAdditional, deleteMaterialAdditional } from '@/api/wms/quality/inspectStandardApproval'
import StandardApprovalDetails from './StandardApprovalDetails.vue'
import UpdateInspection from '../components/UpdateInspection.vue'
import ApprovalLogs from './ApprovalLogs.vue'
import { getFormColumnsList } from '@/api/system/saveColumns/index'
import { mapGetters } from 'vuex'
import { isArray } from 'min-dash'

export default {
  name: 'AwaitApproval',
  components: {
    StandardApprovalDetails,
    UpdateInspection,
    ApprovalLogs
  },
  props: {
    'activeName': {
      type: String,
      default: ''
    },
    'queryParamspro': {
      type: Object,
      default: () => {}
    }
  },
  inject: ['paramsQuery'],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 检验选择
      open: false,
      // 总条数
      total: 0,
      // 领料单列表
      list: [],
      //   用户列表
      users: [],
      // 弹出层标题
      title: '',
      //   是否显示查看弹出层
      viewOpen: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        auditStatus: -1,
        status: 0,
        verion: 0,
        processNo: 'PURCHASE'
      },
      excueParams: {},
      //   表单参数
      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 }
      ],
      // 显示隐藏持久化参数
      columnsList: []
    }
  },
  computed: {
    ...mapGetters([
      'userId'
    ])
  },
  created() {
    // 获取显隐列列表
    this.getColumnsList()
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      const params = {
        ...this.queryParams,
        ...this.paramsQuery
      }
      // 执行查询
      getMaterialAdditionalPage(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()
    },
    // 查看明细
    handleView(row) {
      const { id } = row
      this.$refs.standardApprovalDetails.queryParams.wmsMaterialAdditionalIdNew = id
      this.$refs.standardApprovalDetails.visible = true
    },
    // 修改检验标准
    handleUpdateStandard(row) {
      const { id, code, name, version, masterName, wmsMaterialItemMasterId, processNo } = row
      const changeParams = {
        id,
        code
      }
      // iqc调用时间校验
      if (processNo === 'PURCHASE') {
        this.$refs.updateInspection.iqcInspectShow = true
        ifChangeMasterAdditional(changeParams).then(res => {
          if (res.data === false) {
            this.$refs.updateInspection.refreshDisable = true
          } else {
            this.$modal.notifyWarning('该方案或已被他人修改，如您需要最新方案，请点击刷新按钮获取！')
            this.$refs.updateInspection.refreshDisable = false
          }
        })
      }
      this.$refs.updateInspection.queryParams.materialNo = code
      this.$refs.updateInspection.queryParams.name = name
      this.$refs.updateInspection.queryParams.processNo = processNo
      this.$refs.updateInspection.queryParams.wmsMaterialAdditionalId = id
      this.$refs.updateInspection.queryParams.type = 0
      this.$refs.updateInspection.wmsMaterialAdditionalId = id
      this.$refs.updateInspection.queryParams.wmsMaterialItemMasterId = wmsMaterialItemMasterId
      this.$refs.updateInspection.queryObj.version = version
      this.$refs.updateInspection.queryObj.name = masterName
      this.$refs.updateInspection.visible = true
      this.$refs.updateInspection.selectDisable = true
    },
    // 提交刷新
    addComplete() {
      this.getList()
      this.$emit('approvalList')
    },
    // 审批日志
    handleLogs(row) {
      const { id } = row
      this.$refs.approvalLogs.queryParams.wmsMaterialAdditionalId = id
      this.$refs.approvalLogs.open = true
    },
    // 删除未提交得检验标准
    handleDelete(row) {
      const { id, masterCode } = row
      this.$modal.confirm('是否确认删除物料编码为"' + masterCode + '"的数据项?').then(function() {
        return deleteMaterialAdditional(id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    // 获取显示隐藏列
    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>

