<template>
  <div class="app-container">
    <!-- 操作栏 -->
    <el-row :gutter="8" class="mb8">
      <el-col :span="1.5">
        <el-button
          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"
          show-summary
          :summary-method="getSummaries"
          border
        >
          <el-table-column
            label="生产领料单"
            align="center"
            prop="workOrderBomNo"
            width="160"
            fixed="left"
          />
          <el-table-column
            label="生产订单"
            align="center"
            prop="workOrderNo"
            width="160"
            show-overflow-tooltip
          />
          <el-table-column
            label="物料批次"
            align="center"
            prop="materialLots"
            width="160"
            show-overflow-tooltip
          />
          <el-table-column
            label="签收状态"
            align="center"
            prop="signStatus"
            width="120"
          >
            <template slot-scope="scope">
              <dict-tag :type="DICT_TYPE.WMS_SIGN_STATUS" :value="scope.row.signStatus" />
            </template>
          </el-table-column>
          <el-table-column
            label="供应商"
            align="center"
            prop="supplierCode"
            width="160"
            show-overflow-tooltip
          />
          <el-table-column
            label="仓库"
            align="center"
            prop="wmsStockId"
            width="120"
          />
          <el-table-column label="区域" align="center" prop="wmsStockAreaId" />
          <el-table-column label="货位" align="center" prop="wmsStockLocationId" />
          <el-table-column
            label="批次数量"
            align="center"
            prop="batchQty"
            sortable
            width="120"
            show-overflow-tooltip
          />
          <el-table-column label="发料人" align="center" prop="creator" />
          <el-table-column
            label="发料单号"
            align="center"
            prop="outStockNo"
            width="160"
            show-overflow-tooltip
          />
          <el-table-column
            label="物料条码"
            align="center"
            prop="wmsLabelMasterId"
            width="170"
            show-overflow-tooltip
          />
          <el-table-column
            label="物料编码"
            align="center"
            prop="erpMaterialId"
            width="160"
          />
          <el-table-column
            label="物料名称"
            align="center"
            prop="materialName"
            width="150"
            show-overflow-tooltip
          />
          <el-table-column label="单位" align="center" prop="unitCode" />
          <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 { getOutStockMainDetailPage, exportOutStockDetail } from '@/api/wms/inquirePurchase/complexDocuments'

export default {
  name: 'ProductionRecieve',
  components: {
  },
  inject: ['paramsQuery'],
  data() {
    return {
      // 生产日期
      produceDate: '',
      // 遮罩层
      loading: true,
      sureLoading: false,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // WMS 入库业务-来料登记主列表
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 高级查询参数
      advParams: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        areaTest: null,
        materialLots: null,
        purchaseOrderNo: null,
        materialNo: null,
        receiptQty: null,
        testStatus: null,
        createTime: [],
        materialName: null
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      const params = {
        ...this.queryParams,
        ...this.paramsQuery,
        ...this.advParams
      }
      getOutStockMainDetailPage(params).then(response => {
        this.list = response.data.list
        this.total = response.data.total
      }).finally(() => {
        this.loading = false
      })
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.toggle = !this.toggle
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        areaTest: undefined,
        materialLots: undefined,
        purchaseOrderNo: undefined,
        materialNo: undefined,
        receiptQty: undefined,
        testStatus: undefined,
        produceDate: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    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 === 'batchQty') {
          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 exportOutStockDetail(params)
      }).then(response => {
        this.$download.excel(response, `生产领料单-${ new Date().toLocaleString()}.xls`)
        this.exportLoading = false
      }).catch(() => {})
    }

  }
}
</script>

