<template>
  <!-- 列表 -->
  <div>
    <el-col :span="24" class="text-right">
      <el-button
        type="primary"
        plain
        icon="el-icon-upload"
        size="mini"
        class="mb10"
        @click="handleCase"
      >入库方案</el-button>
    </el-col>

    <AutoResizer :given-height="260">
      <template #default="{height}">
        <el-table
          v-if="columnsList.length > 0"
          v-loading="loading"
          :data="list"
          border
          :height="height"
          @selection-change="tableSelected"
        >
          <el-table-column
            type="selection"
            width="55"
            fixed="left"
            align="center"
          />
          <el-table-column
            v-if="columnsList[0].visible"
            :key="Math.random()"
            label="检验单号"
            align="center"
            prop="inspectionNo"
            width="170"
          />
          <el-table-column
            v-if="columnsList[1].visible"
            :key="Math.random()"
            label="物料批次"
            align="center"
            prop="materialLots"
            width="120"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[2].visible"
            :key="Math.random()"
            label="采购订单"
            align="center"
            prop="purchaseOrderNo"
            width="120"
          />
          <el-table-column
            v-if="columnsList[3].visible"
            :key="Math.random()"
            label="物料编码"
            align="center"
            prop="materialNo"
            width="120"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[4].visible"
            :key="Math.random()"
            label="物料名称"
            align="center"
            prop="materialName"
            width="120"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[5].visible"
            :key="Math.random()"
            label="到货数量"
            align="center"
            prop="receiptQty"
          />
          <el-table-column
            v-if="columnsList[6].visible"
            :key="Math.random()"
            label="合格数量"
            align="center"
            prop="qualifiedQty"
          />
          <el-table-column
            v-if="columnsList[7].visible"
            :key="Math.random()"
            label="检验状态"
            align="center"
            prop="testStatus"
          >
            <template slot-scope="scope">
              <dict-tag :type="DICT_TYPE.WMS_TEST_STATUS" :value="scope.row.testStatus" />
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[8].visible"
            :key="Math.random()"
            label="单位"
            align="center"
            prop="unitCode"
          />
          <el-table-column
            v-if="columnsList[9].visible"
            :key="Math.random()"
            label="供应商"
            align="center"
            prop="supplierName"
            width="120"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[10].visible"
            :key="Math.random()"
            label="存储条件"
            align="center"
            prop="storageEnvironment"
            width="120"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[11].visible"
            :key="Math.random()"
            label="登记时间"
            align="center"
            prop="receiptTime"
            width="160"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.receiptTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[12].visible"
            :key="Math.random()"
            label="登记人"
            align="center"
            prop="receiptPerson"
          />
          <el-table-column
            v-if="columnsList[13].visible"
            :key="Math.random()"
            label="处理结果"
            align="center"
            prop="auditStatus"
          >
            <template slot-scope="scope">
              <dict-tag :type="DICT_TYPE.WMS_IQC_APPROVAL_STATUS" :value="scope.row.auditStatus" />
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[14].visible"
            :key="Math.random()"
            label="检验结果"
            align="center"
            prop="testResult"
          >
            <template slot-scope="scope">
              <dict-tag :type="DICT_TYPE.WMS_TEST_RESULT" :value="scope.row.testResult" />
            </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"
    />
    <!-- 入库方案 -->
    <WareHousingPlan ref="wareHousingPlan" :current-rows="currentRows" @getList="getList" />

  </div>

</template>

<script>
import { getIncomingToStockPage } from '@/api/wms/incoming/wareHousing'
import WareHousingPlan from './WareHousingPlan.vue'
import { getFormColumnsList } from '@/api/system/saveColumns/index'
import { mapGetters } from 'vuex'
import { isArray } from 'min-dash'

export default {
  name: 'AwaitWare',
  components: {
    WareHousingPlan
  },
  props: {
    'activeName': {
      type: String,
      default: ''
    },
    'queryParamspro': {
      type: Object,
      default: () => {}
    }
  },
  inject: ['paramsQuery'],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 领料单列表
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      //   是否显示查看弹出层
      viewOpen: false,
      // 高级查询
      advParams: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10
      },
      currentRows: [],
      //   表单参数
      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 },
        { key: 15, label: '来料入库单号', visible: true },
        { key: 16, label: '入库时间', visible: true },
        { key: 17, label: '操作人', visible: true },
        { key: 18, label: '入库数量', visible: true }
      ],
      // 显示隐藏持久化参数
      columnsList: []
    }
  },
  // 获取当前登录用户id
  computed: {
    ...mapGetters([
      'userId'
    ])
  },
  created() {
    // 获取显隐列列表
    this.getColumnsList()
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      const params = {
        ...this.paramsQuery,
        ...this.queryParams,
        ...this.advParams
      }
      getIncomingToStockPage(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()
    },
    // 入库方案
    handleCase() {
      if (this.currentRows.length === 0) {
        this.$modal.msgWarning('请选择你要入库的数据')
        return
      }
      this.$refs.wareHousingPlan.open = true
      this.$refs.wareHousingPlan.title = '入库方案'
    },
    // 表格选中
    tableSelected(val) {
      this.currentRows = val
    },
    // 获取显示隐藏列
    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>
