<template>
  <el-dialog
    v-dialogDrag
    title="生产入库明细"
    :visible.sync="open"
    width="1200px"
    append-to-body
    @open="dialogOpen"
    @close="dialogClose"
  >
    <!-- 搜索工作栏 -->
    <el-form
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-width="68px"
    >
      <el-row :gutter="10">
        <el-col :span="5">
          <el-form-item label="生产订单" prop="workOrderNo">
            <el-input
              v-model="queryParams.workOrderNo"
              placeholder="请输入生产订单"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="产品编码" prop="productCode">
            <el-input
              v-model="queryParams.productCode"
              placeholder="请输入产品编码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="产品名称" prop="productName">
            <el-input
              v-model="queryParams.productName"
              placeholder="请输入产品名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="箱号" prop="caseNo">
            <el-input
              v-model="queryParams.caseNo"
              placeholder="请输入箱号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-button
            type="primary"
            icon="el-icon-search"
            size="small"
            @click="handleQuery"
          >搜索</el-button>
          <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
        </el-col>
      </el-row>

    </el-form>
    <!-- 列表 -->
    <el-table
      v-loading="loading"
      :data="list"
      border
      size="mini"
      :summary-method="getSummaries"
      show-summary
      :row-key="id"
    >
      <el-table-column
        label="入库单号"
        align="center"
        prop="warehousingOrderNo"
        width="150"
        show-overflow-tooltip
      />
      <el-table-column
        label="产品编码"
        align="center"
        prop="productCode"
        width="130"
        show-overflow-tooltip
      />
      <el-table-column
        label="产品名称"
        align="center"
        prop="productName"
        width="120"
        show-overflow-tooltip
      />
      <el-table-column label="BOM版本" align="center" prop="mesBomVersion" />
      <el-table-column label="计划数量" align="center" prop="planAmount" />
      <el-table-column
        label="生产订单编号"
        align="center"
        prop="workOrderNo"
        width="120"
        show-overflow-tooltip
      />
      <el-table-column
        label="箱号"
        align="center"
        prop="caseNo"
        width="130"
        show-overflow-tooltip
      />
      <el-table-column label="装箱数量" align="center" prop="inQty" />
    </el-table>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </el-dialog>
</template>

<script>
import { getIncomingProduceDetailPage, exportIncomingProduceDetailExcel } from '@/api/wms/incoming/production'
export default {
  name: 'IncomingProduceDetail',
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 入库业务 生产入库明细列表
      list: [],
      // 弹出层标题
      title: '',
      id: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 100,
        masterId: null,
        warehousingOrderNo: null,
        productCode: null,
        planAmount: null,
        workOrderNo: null,
        caseNo: null,
        inQty: null,
        stockCode: null,
        areaCode: null,
        locationCode: null,
        createTime: [],
        id: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        masterId: [{ required: true, message: '主表ID不能为空', trigger: 'blur' }],
        productCode: [{ required: true, message: '产品编码不能为空', trigger: 'blur' }],
        workOrderNo: [{ required: true, message: '生产订单编号不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    // this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      getIncomingProduceDetailPage(this.queryParams).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,
        masterId: undefined,
        warehousingOrderNo: undefined,
        productCode: undefined,
        productName: null,
        planAmount: undefined,
        workOrderNo: undefined,
        caseNo: undefined,
        inQty: undefined,
        stockCode: undefined,
        areaCode: undefined,
        locationCode: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      // this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      const params = { ...this.queryParams }
      params.pageNo = undefined
      params.pageSize = undefined
      this.$modal.confirm('是否确认导出所有入库业务 生产入库明细数据项?').then(() => {
        this.exportLoading = true
        return exportIncomingProduceDetailExcel(params)
      }).then(response => {
        this.$download.excel(response, '入库业务 生产入库明细.xls')
        this.exportLoading = false
      }).catch(() => {})
    },
    dialogOpen() {
      this.$nextTick(() => {
        this.getList()
      })
    },
    dialogClose() {
      this.getList()
    },
    // 小数点问题处理
    getHandleTotalNumber(val) {
      return Math.round((parseFloat(val) * 10000)) / 10000
    },
    // 总计
    getSummaries(param) {
      const { columns } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '总计'
          return
        }
        const values = this.list.map(item => Number(item[column.property])) || []
        if (column.property === 'inQty') {
          sums[index] = values.reduce((acc, cur) => {
            const value = Number(cur)
            if (!isNaN(value)) {
              return this.getHandleTotalNumber(acc + cur)
            } else {
              return this.getHandleTotalNumber(acc)
            }
          }, 0)
          sums[index]
        }
      })
      return sums
    }
  }
}
</script>

