<template>
  <el-dialog
    v-dialogDrag
    :title="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"
    >
      <SearchBar :show-span="24" :collapse-span="6">
        <template #header>
          <el-col :span="6">
            <el-form-item label="物料编码" prop="materialNo">
              <el-input v-model="queryParams.materialNo" placeholder="请输入物料编码" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="物料名称" prop="materialName">
              <el-input v-model="queryParams.materialName" placeholder="请输入物料名称" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="仓库" prop="erpStockId">
              <StockSelect
                :ref="`stock1`"
                v-model="queryParams.erpStockId"
                filterable
                @change="($event) => { stockChange($event) }"
              />
            </el-form-item>
          </el-col>
        </template>
        <template #body>
          <el-col :span="6">
            <el-form-item label="区域" prop="wmsStoreAreaId">
              <AreaSelect
                :ref="`area1`"
                v-model="queryParams.wmsStoreAreaId"
                filterable
                @change="($event) => { areaChange($event) }"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="货位" prop="wmsStoreId">
              <LocationSelect :ref="`location1`" v-model="queryParams.wmsStoreId" filterable />
            </el-form-item>
          </el-col>
        </template>
        <template #buttons>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </template>
      </SearchBar>
    </el-form>

    <div class="itemTrading">
      <el-table
        ref="elTable"
        v-loading="loading"
        :data="list"
        border
        :max-height="500"
        size="mini"
        :summary-method="getSummaries"
        show-summary
      >
        <el-table-column
          label="物料批次"
          align="center"
          prop="materialLots"
          width="160"
          fixed="left"
        />
        <el-table-column
          label="物料条码"
          align="center"
          prop="qrCode"
          width="160"
        />
        <el-table-column
          label="物料编码"
          align="center"
          prop="materialNo"
          width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="仓库"
          align="center"
          prop="erpStockId"
          width="120"
        />
        <el-table-column
          label="区域"
          align="center"
          prop="wmsStoreAreaId"
          width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="货位"
          align="center"
          prop="wmsStoreId"
          width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="长度(m)"
          align="center"
          prop="length"
          width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="宽度(mm)"
          align="center"
          prop="width"
          width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="厚度(mm)"
          align="center"
          prop="thickness"
          width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="供应商"
          align="center"
          prop="supplierName"
          width="120"
        />
        <el-table-column
          label="物料批次数量"
          align="center"
          prop="qty"
          width="120"
        />
        <el-table-column
          label="货位物料剩余总数"
          width="130"
          align="center"
          prop="remainderStorageQty"
        />
        <el-table-column label="单位" align="center" prop="unitCode" />
        <el-table-column
          label="采购订单"
          align="center"
          prop="purchaseOrderNo"
          width="120"
        />
        <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-column label="检验结果" align="center" prop="testResult">
          <template slot-scope="scope">
            <dict-tag :type="DICT_TYPE.WMS_TEST_RESULT" :value="scope.row.testResult" />
          </template>
        </el-table-column>
        <el-table-column label="处理结果" align="center" prop="auditStatus">
          <template slot-scope="scope">
            <dict-tag :type="DICT_TYPE.WMS_IQC_APPROVAL_STATUS" :value="scope.row.auditStatus" />
          </template>
        </el-table-column>
      </el-table>
    </div>

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

<script>
import { getMaterialStockDetailPage } from '@/api/wms/inquirePurchase/inventoryInquire'
import StockSelect from '@/views/wms/incoming/wareHousing/components/StockSelect.vue'
import AreaSelect from '@/views/wms/incoming/wareHousing/components/AreaSelect.vue'
import LocationSelect from '@/views/wms/incoming/wareHousing/components/LocationSelect.vue'

export default {
  components: {
    StockSelect,
    AreaSelect,
    LocationSelect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // WMS 生产发料 主列表
      list: [],
      // 弹出层标题
      title: '',
      showSearch: true,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 100,
        materialNo: null,
        erpStockId: null,
        wmsStoreAreaId: null,
        wmsStoreId: null,
        width: null,
        length: null,
        thickness: null
      }
    }
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      getMaterialStockDetailPage(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()
    },
    stockChange(item) {
      if (item) {
        const { id } = item
        this.$refs[`area1`].options = []
        this.$refs[`area1`].fetchAreaList(id)
      }
    },
    areaChange(item) {
      if (item) {
        const { id } = item
        this.$refs[`location1`].options = []
        this.$refs[`location1`].fetchLocationList(id)
      }
    },
    // 小数问题处理
    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 === 'qty') {
          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
    }
  }
}
</script>

<style scoped>

</style>

