<template>
  <!-- 列表 -->
  <div>
    <AutoResizer :given-height="250">
      <template #default="{height}">
        <el-table
          v-if="columnsList.length > 0"
          v-loading="loading"
          :height="height"
          :data="list"
          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[8].visible"
            :key="Math.random()"
            label="审核人"
            align="center"
            prop="auditor"
          />
          <el-table-column
            v-if="columnsList[7].visible"
            :key="Math.random()"
            label="审核时间"
            align="center"
            prop="auditTime"
            width="180"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.auditTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[4].visible"
            :key="Math.random()"
            label="审核状态"
            align="center"
            prop="auditStatus"
          >
            <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="180"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handleView(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" />
    <!-- 审批日志 -->
    <ApprovalLogs ref="approvalLogs" />
  </div>

</template>

<script>
import { getMaterialAdditionalPage } 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,
      // 显示搜索条件
      showSearch: true,
      // 检验选择
      open: false,
      // 总条数
      total: 0,
      // 领料单列表
      list: [],
      //   用户列表
      users: [],
      // 弹出层标题
      title: '',
      //   是否显示查看弹出层
      viewOpen: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        audited: 1,
        processNo: 'PURCHASE'
      },
      //   表单参数
      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.wmsMaterialAdditionalId = id
      // this.$refs.standardApprovalDetails.visible = true
      const { code, name, version, id, auditStatus, masterName, wmsMaterialItemMasterId, processNo } = row
      this.$refs.updateInspection.queryParams.materialNo = code
      this.$refs.updateInspection.queryParams.name = name
      this.$refs.updateInspection.queryParams.wmsMaterialAdditionalId = id
      this.$refs.updateInspection.queryParams.wmsMaterialItemMasterId = wmsMaterialItemMasterId
      this.$refs.updateInspection.queryParams.processNo = processNo

      // 检验类型
      this.$refs.updateInspection.queryParams.type = 0

      this.$refs.updateInspection.queryObj.version = version
      this.$refs.updateInspection.queryObj.name = masterName
      this.$refs.updateInspection.saveDisable = true
      this.$refs.updateInspection.commitDisable = true
      this.$refs.updateInspection.selectDisable = true
      this.$refs.updateInspection.refreshDisable = true
      this.$refs.updateInspection.inspectDisable = true
      if (auditStatus === 1) {
        this.$refs.updateInspection.copyShow = true
      }
      this.$refs.updateInspection.visible = true
    },
    // 审批日志
    handleLogs(row) {
      const { id } = row
      this.$refs.approvalLogs.queryParams.wmsMaterialAdditionalId = id
      this.$refs.approvalLogs.open = true
    },
    // 获取显示隐藏列
    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>

