<template>
  <div class="app-container" v-loading="loading">
    <el-row>
      <!-- 筛选区 -->
      开始时间：<el-date-picker v-model="queryParams.startTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
        placeholder="选择开始时间"></el-date-picker>
      结束时间：<el-date-picker v-model="queryParams.endTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
        placeholder="选择结束时间"></el-date-picker>
      <el-button type="primary" style="margin-left: 10px;" @click="getList">查询</el-button>
      <el-button type="primary" @click="exportData">导出</el-button>
    </el-row>

    <el-descriptions title="统计信息" :column="3" border>
      <el-descriptions-item label="总钢卷数量">{{ summary.totalCount }}</el-descriptions-item>
      <el-descriptions-item label="总重">{{ summary.totalWeight }}t</el-descriptions-item>
      <el-descriptions-item label="均重">{{ summary.avgWeight }}t</el-descriptions-item>
    </el-descriptions>

    <el-descriptions title="明细信息" :column="3" border>
    </el-descriptions>
    <el-table :data="list" border  height="calc(100vh - 320px)">
      <el-table-column label="入场钢卷号" align="center" prop="enterCoilNo">
        <template slot-scope="scope">
          <coil-no :coil-no="scope.row.enterCoilNo"></coil-no>
        </template>
      </el-table-column>
      <el-table-column label="当前钢卷号" align="center" prop="currentCoilNo">
        <template slot-scope="scope">
          <coil-no :coil-no="scope.row.currentCoilNo"></coil-no>
        </template>
      </el-table-column>
      <el-table-column label="逻辑库位" align="center" prop="warehouseName" />
      <el-table-column label="实际库区" align="center" prop="actualWarehouseName" />
      <el-table-column label="产品类型" align="center" width="250">
        <template slot-scope="scope">
          <ProductInfo v-if="scope.row.itemType == 'product'" :product="scope.row.product" />
          <RawMaterialInfo v-else-if="scope.row.itemType === 'raw_material'" :material="scope.row.rawMaterial" />
        </template>
      </el-table-column>
      <el-table-column label="长度 (米)" align="center" prop="length" />
      <el-table-column label="发货时间" align="center" prop="exportTime" />
      <el-table-column label="更新人" align="center" prop="updateByName" />
    </el-table>
  </div>
</template>

<script>
import { listMaterialCoil } from "@/api/wms/coil";
import ProductInfo from "@/components/KLPService/Renderer/ProductInfo";
import RawMaterialInfo from "@/components/KLPService/Renderer/RawMaterialInfo";
import CoilNo from "@/components/KLPService/Renderer/CoilNo.vue";

export default {
  components: {
    ProductInfo,
    RawMaterialInfo,
    CoilNo,
  },
  data() {
    // 获取昨天0点到今天0点的时间范围
    const now = new Date()
    const yesterday = new Date(now.getTime() - 24 * 60 * 60 * 1000)
    const startTime = yesterday.toISOString().slice(0, 10) + ' 00:00:00'
    const endTime = now.toISOString().slice(0, 10) + ' 00:00:00'
    return {
      list: [],
      queryParams: {
        pageNum: 1,
        pageSize: 9999,
        status: 1,
        startTime: startTime,
        endTime: endTime,
      },
      loading: false,
    }
  },
  computed: {
    summary() {
      // 总钢卷数量、总重、均重
      const totalCount = this.list.length
      const totalWeight = this.list.reduce((acc, cur) => acc + parseFloat(cur.netWeight), 0)
      const avgWeight = totalCount > 0 ? (totalWeight / totalCount).toFixed(2) : 0
      return {
        totalCount,
        totalWeight,
        avgWeight,
      }
    }
  },
  methods: {
    getList() {
      this.loading = true
      listMaterialCoil({
        ...this.queryParams
      }).then(res => {
        this.list = res.rows
        this.loading = false
      })
    },
    // 导出
    exportData() {
      this.download('wms/materialCoil/export', {
        coilIds: this.list.map(item => item.coilId).join(',')
      }, `materialCoil_${new Date().getTime()}.xlsx`)
    },
  },
  mounted() {
    this.getList()
  }
}
</script>

<style scoped></style>
