<template>
  <div class="app-container">
    <!-- 操作栏 -->
    <el-row :gutter="8" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['wms:return-work-order: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"
          show-summary
          :summary-method="getSummaries"
          :height="height"
          border
        >
          <el-table-column
            label="退料单号"
            align="center"
            prop="returnOrderNo"
            width="180"
            fixed="left"
          />
          <el-table-column
            label="领料单号"
            align="center"
            prop="workOrderBomNo"
            width="140"
          />
          <el-table-column
            label="生产订单"
            align="center"
            prop="workOrderNo"
            width="120"
          />
          <el-table-column
            label="发料单号"
            align="center"
            prop="sendOrderNo"
            width="130"
            show-overflow-tooltip
          />
          <el-table-column
            label="物料编码"
            align="center"
            prop="materialNo"
            width="150"
            show-overflow-tooltip
          />
          <el-table-column
            label="物料批次"
            align="center"
            prop="materialLots"
            width="130"
            show-overflow-tooltip
          />
          <el-table-column
            label="物料名称"
            align="center"
            prop="materialName"
            width="100"
            show-overflow-tooltip
          />
          <el-table-column
            label="物料条码"
            align="center"
            prop="qrCode"
            width="150"
            show-overflow-tooltip
          />
          <el-table-column
            label="采购订单"
            align="center"
            prop="purchaseOrderNo"
            width="130"
            show-overflow-tooltip
          />
          <el-table-column
            label="供应商编码"
            align="center"
            prop="supplierCode"
            width="120"
          />
          <el-table-column
            label="发料数量"
            align="center"
            prop="sendQty"
            width="100"
            sortable
          />
          <el-table-column
            label="退料数量"
            align="center"
            prop="returnQty"
            sortable
            width="100"
          />
          <el-table-column
            label="退料原因"
            align="center"
            prop="remark"
            width="120"
            show-overflow-tooltip
          />
          <el-table-column label="创建人" align="center" prop="creator" />
          <el-table-column
            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 { exportReturnWorkOrderExcel } from '@/api/wms/orders/productRejected'
import { getOutStockReturnPage } from '@/api/wms/inquirePurchase/complexDocuments'

export default {
  name: 'ProductionReturn',
  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,
        createTime: [],
        wmsResupplyOrderMasterId: undefined
      },
      // 表单参数
      form: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      const params = {
        ...this.queryParams,
        ...this.paramsQuery,
        ...this.advParams
      }
      getOutStockReturnPage(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()
    },
    // 小数点问题处理
    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 === 'sendQty' || column.property === 'returnQty') {
          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('是否确认导出所有生产退料单数据项?').then(() => {
        this.exportLoading = true
        return exportReturnWorkOrderExcel(params)
      }).then(response => {
        this.$download.excel(response, `生产退料单-${ new Date().toLocaleString()}.xls`)
        this.exportLoading = false
      }).catch(() => {})
    }
  }
}
</script>
