<template>
  <el-dialog
    v-dialogDrag
    title="收发明细"
    :visible.sync="open"
    width="1200px"
    append-to-body
    :before-close="beforeClose"
    @open="dialogOpen"
  >

    <!-- 表头 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-width="68px"
      disabled
    >
      <SearchBar :show-more="false" :collapse-span="6">
        <template #header>
          <el-col :span="6">
            <el-form-item label="物料编码" prop="materialNo">
              <el-input
                v-model="queryParams.materialNo"
                placeholder="请输入物料编码"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="物料名称" prop="materialName">
              <el-input
                v-model="queryParams.materialName"
                placeholder="请输入物料名称"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item
              label="库存单位"
              prop="status"
              label-width="98px"
            >
              <el-input
                v-model="queryParams.unitCode"
                placeholder="请输入库存单位"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
        </template>
      </SearchBar>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['wms:inventory-general:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table
      ref="elTable"
      v-loading="loading"
      :data="list"
      border
      size="mini"
      max-height="600px"
      :summary-method="getSummaries"
      show-summary
    >
      <el-table-column
        label="日期"
        align="center"
        prop="createTime"
        fixed="left"
        min-width="160"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="单据编号"
        align="center"
        prop="orderNo"
        width="180"
      />
      <el-table-column
        label="单据名称"
        align="center"
        prop="orderName"
        width="120"
      />
      <el-table-column
        label="仓库名称"
        align="center"
        prop="stockName"
        width="140"
      />
      <el-table-column
        label="期初"
        align="center"
        prop="openingBalance"
        width="120"
      />
      <el-table-column
        label="收入"
        align="center"
        prop="periodInQty"
        width="100"
      />
      <el-table-column
        label="发出"
        align="center"
        prop="periodOutQty"
        width="120"
      />
      <el-table-column
        label="结存"
        align="center"
        prop="periodBalance"
      />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </el-dialog>
</template>

<script>
import { getGeneralDetailPage, exportDetailGeneralExcel } from '@/api/wms/inquirePurchase/inventoryGeneral'

export default {
  components:
    {
    },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      exportLoading: false,
      // WMS 生产发料 主列表
      list: [],
      // 弹出层标题
      title: '',
      showSearch: true,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 100,
        materialNo: null

      }
    }
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      getGeneralDetailPage(this.queryParams).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()
    },
    dialogOpen() {
      this.$nextTick(() => {
        this.getList()
      })
    },
    beforeClose(done) {
      this.list = []
      this.total = 0
      done && done()
    },
    // 小数处理
    getHandleNumber(val) {
      return Math.round((parseFloat(val) * 10000000)) / 10000000
    },
    // 表尾计算
    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 === 'periodInQty' || column.property === 'periodOutQty') {
          sums[index] = values.reduce((acc, cur) => {
            const value = Number(cur)
            if (!isNaN(value)) {
              return this.getHandleNumber(acc + cur)
            } else {
              return acc
            }
          }, 0)
          sums[index]
        } else if (column.property === 'periodBalance') {
          if (this.list.length > 0) {
            sums[index] = this.list[this.list.length - 1].periodBalance
          }
        }
      })
      return sums
    },
    // 导出明细
    handleExport() {
      // 处理查询参数
      const params = { ...this.queryParams }
      params.pageNo = undefined
      params.pageSize = undefined
      this.$modal.confirm('是否确认导出所有即时库存明细数据项?').then(() => {
        this.exportLoading = true
        return exportDetailGeneralExcel(params)
      }).then(response => {
        this.$download.excel(response, '即时库存明细.xls')
        this.exportLoading = false
      }).catch(() => {})
    }
  }
}
</script>

<style scoped>

</style>

