<template>
  <div>
    <!-- 列表 -->
    <AutoResizer :given-height="220">
      <template #default="{height}">
        <el-table
          v-if="columnsList.length > 0"
          v-loading="loading"
          :data="list"
          border
          size="mini"
          :height="height"
          show-summary
          :summary-method="getSummaries"
        >
          <el-table-column
            v-if="columnsList[4].visible"
            label="物料批次"
            align="center"
            width="150"
            prop="materialBatch"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[0].visible"
            :key="Math.random()"
            label="退料单号"
            align="center"
            prop="rejectNo"
            width="160"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[5].visible"
            :key="Math.random()"
            label="入库仓库"
            align="center"
            prop="erpStockId"
            width="130"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[6].visible"
            :key="Math.random()"
            label="入库区域"
            align="center"
            prop="wmsStoreAreaId"
            width="120"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[7].visible"
            :key="Math.random()"
            label="入库货位"
            align="center"
            prop="wmsStoreId"
            width="120"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[8].visible"
            :key="Math.random()"
            label="物料名称"
            align="center"
            width="120"
            prop="materialName"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[9].visible"
            :key="Math.random()"
            label="供应商"
            align="center"
            prop="supplierCode"
            width="130"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[10].visible"
            :key="Math.random()"
            label="物料编码"
            align="center"
            prop="materialNo"
            width="120"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[11].visible"
            :key="Math.random()"
            label="领料单号"
            align="center"
            prop="receiveOrderNo"
            width="120"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[12].visible"
            :key="Math.random()"
            label="发料单号"
            align="center"
            prop="sendOrderNo"
            width="150"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[13].visible"
            :key="Math.random()"
            label="发料数量"
            align="center"
            prop="sendQty"
          />
          <el-table-column
            v-if="columnsList[14].visible"
            :key="Math.random()"
            label="退料数量"
            align="center"
            prop="rejectOrderNo"
          />
          <el-table-column
            v-if="columnsList[15].visible"
            :key="Math.random()"
            label="入库数量"
            align="center"
            prop="inQty"
          />
          <el-table-column
            v-if="columnsList[16].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>
      </template>
    </AutoResizer>

    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看明细 -->
    <AlreadyRejectStockDetail ref="alreadyRejectStockDetail" />
  </div>

</template>

<script>
import { getStockedPageFromOut } from '@/api/wms/incoming/reject'
import { getIncomingRejectDetailStockedPage } from '@/api/wms/incoming/reject'
import AlreadyRejectStockDetail from './AlreadyRejectStockDetail.vue'
import { getFormColumnsList } from '@/api/system/saveColumns/index'
import { mapGetters } from 'vuex'
import { isArray } from 'min-dash'

export default {
  name: 'AlreadyRejectStock',
  components: {
    AlreadyRejectStockDetail
  },
  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
      },
      //   表单参数
      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: 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 }
      ],
      // 显示隐藏持久化参数
      columnsList: []
    }
  },
  // 获取当前登录用户id
  computed: {
    ...mapGetters([
      'userId'
    ])
  },
  created() {
    this.getColumnsList()
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      const params = {
        ...this.paramsQuery,
        ...this.queryParams,
        ...this.advParams
      }
      if (this.$route.query.receiveOrderNo) {
        this.queryParams.receiveOrderNo = this.$route.query.receiveOrderNo
        const params = {
          ...this.paramsQuery,
          ...this.queryParams,
          ...this.advParams
        }
        getStockedPageFromOut(params).then(res => {
          this.list = res.data.list
          this.total = res.data.total
        }).finally(() => {
          this.loading = false
        })
      } else {
        getIncomingRejectDetailStockedPage(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 { rejectNo } = row
      this.$refs.alreadyRejectStockDetail.queryParams.rejectNo = rejectNo
      this.$refs.alreadyRejectStockDetail.open = true
    },
    // 总计
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '总计'
          return
        }

        const values = data.map(item => Number(item[column.property]))
        if (column.property === 'inQty') {
          sums[index] = values.reduce((acc, cur) => {
            const value = Number(cur)
            if (!isNaN(value)) {
              return acc + cur
            } else {
              return acc
            }
          }, 0)
          sums[index]
        }
      })
      return sums
    },
    // 获取显示隐藏列
    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)
      })
    }
    // 单据打印
    // handlePrint() {
    //   this.$modal.msgWarning('该功能暂未开放~')
    // }
  }
}
</script>
  <style>
  </style>

