<template>
  <div>
    <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb10">
      <el-col class="text-right">
        <el-button
          v-hasPermi="['wms:incoming-produce-detail:query']"
          type="primary"
          plain
          icon="el-icon-upload"
          size="mini"
          @click="handleCase()"
        >入库方案</el-button>
      </el-col>
    </el-row>
    <!-- 列表 -->
    <AutoResizer :given-height="260">
      <template #default="{height}">
        <el-table
          v-if="columnsList.length > 0"
          v-loading="loading"
          :data="list"
          border
          :height="height"
          size="mini"
          @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="scrapNo"
            width="180"
            fixed="left"
          />
          <el-table-column
            v-if="columnsList[1].visible"
            :key="Math.random()"
            label="产品条码"
            align="center"
            prop="productBarcode"
            width="150"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[2].visible"
            :key="Math.random()"
            label="物料编码"
            align="center"
            prop="productCode"
          />
          <el-table-column
            v-if="columnsList[3].visible"
            :key="Math.random()"
            label="物料名称"
            align="center"
            prop="productName"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[4].visible"
            :key="Math.random()"
            label="生产订单"
            align="center"
            prop="workOrderNo"
          />
          <el-table-column
            v-if="columnsList[5].visible"
            :key="Math.random()"
            label="生产工单"
            align="center"
            prop="workOrderId"
          />
          <el-table-column
            v-if="columnsList[6].visible"
            :key="Math.random()"
            label="产线"
            align="center"
            prop="productionLine"
            width="100"
          />
          <el-table-column
            v-if="columnsList[7].visible"
            :key="Math.random()"
            label="工序"
            align="center"
            prop="productionProcesses"
            width="100"
          />
          <el-table-column
            v-if="columnsList[8].visible"
            :key="Math.random()"
            label="生产人员"
            align="center"
            prop="productionPerson"
          />
          <el-table-column
            v-if="columnsList[9].visible"
            :key="Math.random()"
            label="产出时间"
            align="center"
            prop="outputTime"
            width="160"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.outputTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[10].visible"
            :key="Math.random()"
            label="不良代码"
            align="center"
            prop="badCode"
            width="120"
          />
          <el-table-column
            v-if="columnsList[11].visible"
            :key="Math.random()"
            label="登记人员"
            align="center"
            prop="registrant"
          />
          <el-table-column
            v-if="columnsList[12].visible"
            :key="Math.random()"
            label="登记时间"
            align="center"
            prop="registerTime"
            width="160"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.registerTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[13].visible"
            :key="Math.random()"
            label="异常说明"
            align="center"
            prop="exceptionRemark"
          />
          <el-table-column
            v-if="columnsList[14].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"
        fixed="right"
        width="120"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['wms:incoming-produce-detail:query']"
            type="text"
            icon="el-icon-upload"
            size="mini"
            @click="handleCase(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"
    />
    <!-- 入库方案 -->
    <ScrapStockPlan ref="scrapStockPlan" :list="currentRows" @complete="scrapComplete" />
  </div>

</template>

<script>
// import { getIncomingScrapPage } from '@/api/wms/incoming/scrap'
import { getIncomingScrapTostockDetailPage } from '@/api/wms/incoming/scrap'
import ScrapStockPlan from './ScrapStockPlan.vue'
import { getFormColumnsList } from '@/api/system/saveColumns/index'
import { mapGetters } from 'vuex'
import { isArray } from 'min-dash'

export default {
  name: 'AwaitScrapStock',
  components: {
    ScrapStockPlan
  },
  props: {
    'activeName': {
      type: String,
      default: ''
    },
    'queryParamspro': {
      type: Object,
      default: () => {}
    }
  },
  inject: ['paramsQuery'],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 领料单列表
      list: [],
      currentRows: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      //   是否显示查看弹出层
      viewOpen: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10
      },
      WarehousingList: [],
      //   表单参数
      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
      }
      getIncomingScrapTostockDetailPage(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.scrapStockPlan.open = true
    },
    scrapComplete() {
      this.getList()
      this.$emit('alreadyScrapComplete')
    },
    // 表格选中
    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>

