<template>
  <div class="app-container">
    <!-- 操作栏 -->
    <el-row :gutter="8" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['wms:resupply-order-master:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
      </el-col>
    </el-row>

    <!-- 列表 -->
    <AutoResizer :given-height="250">
      <template #default="{height}">
        <el-table
          v-loading="loading"
          :data="list"
          :height="height"
          border
        >
          <el-table-column
            :key="Math.random()"
            label="补料单号"
            align="center"
            prop="ebillNo"
            width="180"
          />
          <el-table-column
            :key="Math.random()"
            label="用料清单号"
            align="center"
            prop="workOrderBomNo"
            width="130"
          />
          <el-table-column
            :key="Math.random()"
            label="生产订单号"
            align="center"
            prop="workOrderNo"
            width="130"
          />
          <el-table-column
            :key="Math.random()"
            label="产品编号"
            align="center"
            prop="productCode"
            width="120"
            show-overflow-tooltip
          />
          <el-table-column
            :key="Math.random()"
            label="产品名称"
            align="center"
            prop="productName"
            width="160"
            show-overflow-tooltip
          />
          <el-table-column
            :key="Math.random()"
            label="物料编码"
            align="center"
            prop="materialNo"
            width="150"
            show-overflow-tooltip
          />
          <el-table-column
            :key="Math.random()"
            label="物料名称"
            align="center"
            prop="materialName"
            width="100"
            show-overflow-tooltip
          />
          <el-table-column
            :key="Math.random()"
            label="补领数量"
            align="center"
            prop="reApplyQty"
            sortable
            width="100"
          />
          <el-table-column
            :key="Math.random()"
            label="已领数量"
            align="center"
            prop="rePickedQty"
            sortable
            width="100"
          />
          <el-table-column
            :key="Math.random()"
            label="审批状态"
            align="center"
            prop="auditStatus"
            width="100"
          >
            <template slot-scope="scope">
              <dict-tag :type="DICT_TYPE.MES_RESUPPLY_ORDER_APPROVAL_STATUS" :value="scope.row.auditStatus" />
            </template>
          </el-table-column>
          <el-table-column
            :key="Math.random()"
            label="创建时间"
            align="center"
            prop="createTime"
            width="160"
          >
            <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"
    />

  </div>
</template>

<script>
import { getMainAndDetailPage, exportResupplyOrderMasterExcel } from '@/api/wms/inquirePurchase/complexDocuments'

export default {
  name: 'ProductionReplenishment',
  components: {
  },
  inject: ['paramsQuery'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // WMS补料申请单主列表
      list: [],
      // 高级查询参数
      advParams: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        eBillNo: null,
        billTppe: null,
        wmsProductionWorkOrderBomId: null,
        auditStatus: 2,
        createTime: [],
        wmsResupplyOrderMasterId: undefined
      },
      // 表单参数
      form: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      const params = {
        ...this.queryParams,
        ...this.paramsQuery,
        ...this.advParams
      }
      getMainAndDetailPage(params).then(response => {
        this.list = response.data.list
        this.total = response.data.total
      }).finally(() => {
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 合并
    arraySpanMethod({ row, column, rowIndex, columnIndex }) {
      const newData = this.list.map((item, index) => {
        const filterData = this.list.filter(ele => {
          return item.ebillNo === ele.ebillNo
        })
        const dataIndex = this.list.findIndex(eleIndex => {
          return item.ebillNo === eleIndex.ebillNo
        })
        if (index === dataIndex) {
          item.rowspan = filterData.length
        } else {
          item.rowspan = 0
        }
        return newData
      })
      if (columnIndex === 5 || columnIndex === 6 || columnIndex === 7 || columnIndex === 8) {
        return false
      } else {
        return {
          rowspan: row.rowspan,
          colspan: 1
        }
      }
    },
    // 小数点问题处理
    getHandleNumber(val) {
      return Math.round((parseFloat(val) * 100000000)) / 100000000
    },
    // 总计
    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 === 'reApplyQty' || column.property === 'rePickedQty') {
          sums[index] = values.reduce((acc, cur) => {
            const value = Number(cur)
            if (!isNaN(value)) {
              return this.getHandleNumber(acc + cur)
            } else {
              return acc
            }
          }, 0)
          sums[index]
        }
      })
      return sums
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      const params = { ...this.queryParams }
      params.pageNo = undefined
      params.pageSize = undefined
      this.$modal.confirm('是否确认导出所有WMS补料申请单主数据项?').then(() => {
        this.exportLoading = true
        return exportResupplyOrderMasterExcel(params)
      }).then(response => {
        this.$download.excel(response, `WMS补料申请单主-${ new Date().toLocaleString()}.xls`)
        this.exportLoading = false
      }).catch(() => {})
    }
  }
}
</script>
