<template>
  <ContainerQuery>
    <div
      v-if="$accessCheck($Access.inventoryStatisticsSearchInventoryStatistics)"
      slot="more"
    >
      <el-form inline size="small">
        <el-form-item>
          <el-input
            v-model="keywords"
            placeholder="商品名称"
            clearable
            style="width: 220px"
            @keyup.enter.native="pageChange(1)"
            @clear="pageChange(1)"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="pageChange(1)"
            ></el-button>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="materielCode"
            placeholder="商品编码"
            clearable
            style="width: 220px"
            @keyup.enter.native="pageChange(1)"
            @clear="pageChange(1)"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="pageChange(1)"
            ></el-button>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="warehouseId"
            filterable
            placeholder="请选择仓库"
            @change="pageChange(1)"
          >
            <el-option
              v-for="item in warehouse_list"
              :key="item.id"
              :label="item.warehouseName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="time_val"
            type="daterange"
            :unlink-panels="true"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="searchTimeChange"
          ></el-date-picker>
        </el-form-item>
      </el-form>
    </div>
    <div slot="left" class="all-handel-btn">
      <el-button
        v-if="$accessCheck($Access.inventoryStatistics)"
        size="small"
        type="primary"
        @click="inventoryStatistics(1)"
      >
        导出
      </el-button>
    </div>
    <!-- 表格 -->
    <el-table :data="summary_data" :span-method="objectSpanMethod">
      <el-table-column
        prop="materielCode"
        label="商品名称"
        align="center"
        min-width="150"
      >
        <template slot-scope="scope">
          <div style="text-align: left">
            <p>{{ scope.row.materielName }}</p>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="materielCode"
        label="商品编码"
        align="center"
        min-width="150"
      >
        <template slot-scope="scope">
          <div style="text-align: left">
            <p>{{ scope.row.materielCode }}</p>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="unitName"
        label="商品规格"
        min-width="100"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.unitName }}</span>
          <span v-if="scope.row.skuName">_{{ scope.row.skuName }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="上期结存">
        <el-table-column
          prop="topEndNum"
          align="center"
          label="数量"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ scope.row.topEndNum - 0 }}
          </template>
        </el-table-column>
        <el-table-column
          v-if="$accessCheck($Access.moneyshowinventoryStatisticsMoney)"
          prop="topEndAmount"
          label="金额"
          align="center"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ $_common.formattedNumber(scope.row.topEndAmount) }}
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column align="center" label="本期收入">
        <el-table-column
          prop="selfInNum"
          label="数量"
          align="center"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ scope.row.selfInNum - 0 }}
          </template>
        </el-table-column>
        <el-table-column
          v-if="$accessCheck($Access.moneyshowinventoryStatisticsMoney)"
          prop="selfInAmount"
          label="金额"
          align="center"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ $_common.formattedNumber(scope.row.selfInAmount) }}
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column align="center" label="本期发出">
        <el-table-column
          prop="selfOutNum"
          label="数量"
          align="center"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ scope.row.selfOutNum - 0 }}
          </template>
        </el-table-column>
        <el-table-column
          v-if="$accessCheck($Access.moneyshowinventoryStatisticsMoney)"
          prop="selfOutAmount"
          align="center"
          label="金额"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ $_common.formattedNumber(scope.row.selfOutAmount) }}
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column align="center" label="本期结存">
        <el-table-column
          prop="selfEndNum"
          label="数量"
          align="center"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ scope.row.selfEndNum - 0 }}
          </template>
        </el-table-column>
        <el-table-column
          v-if="$accessCheck($Access.moneyshowinventoryStatisticsMoney)"
          prop="selfEndAmount"
          align="center"
          label="金额"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ $_common.formattedNumber(scope.row.selfEndAmount) }}
          </template>
        </el-table-column>
      </el-table-column>
    </el-table>
    <FooterPage
      :page-size="pageSize"
      :total-page.sync="total"
      :current-page.sync="page"
      @pageChange="pageChange"
      @sizeChange="sizeChange"
    ></FooterPage>
  </ContainerQuery>
</template>

<script>
  import {
    inventoryStatistics,
    exportsInventoryStatistics,
    getAllWarehouse,
  } from "@/api/Stock";
  import { mapGetters } from "vuex";
  export default {
    name: "WarehouseStockSummary",
    data() {
      return {
        summary_data: [],
        pageSize: 10,
        page: 1,
        total: 1,
        warehouse_list: [],
        warehouseId: "",
        endTime: "",
        startTime: "",
        keywords: "",
        materielCode: "",
        time_val: [],
        spanArr: [],
        merchantId: "", // 多商户商户ID
      };
    },
    computed: {
      ...mapGetters({ storeData: "MUser/storeData" }),
    },
    async created() {
      if (this.systemType === 3) {
        this.merchantId = this.storeData.merchantData.id;
      }
      let start = new Date(this.$_common.showMonthFirstDay());
      let end = new Date();
      this.endTime = parseInt(end.getTime() / 1000);
      this.startTime = parseInt(start.getTime() / 1000);
      this.time_val = [start, end];
      await this.getAllWarehouse();
      await this.inventoryStatistics();
    },
    activated() {
      if (this.$_isInit()) return;
      this.inventoryStatistics();
    },
    methods: {
      objectSpanMethod({ row, column, rowIndex, columnIndex }) {
        if (["商品"].includes(column.label)) {
          const _row = this.spanArr[rowIndex];
          const _col = _row > 0 ? 1 : 0;
          return {
            rowspan: _row,
            colspan: _col,
          };
        }
      },
      async inventoryStatistics(exports) {
        let params = {
          merchantId: this.merchantId,
          warehouseId: this.warehouseId,
          hideAmount: this.$accessCheck(
            this.$Access.moneyshowinventoryStatisticsMoney
          )
            ? 0
            : 1,
          start: this.startTime,
          end: this.endTime,
          page: this.page,
          pageSize: this.pageSize,
          search: this.keywords,
          materielCode: this.materielCode,
        };
        if (exports) {
          params.export = 1;
          const target = await exportsInventoryStatistics(params);
        } else {
          const { data, pageTotal } = await inventoryStatistics(params);

          this.summary_data = [];
          data.forEach((item) => {
            item.Details.forEach((itemD) => {
              this.summary_data.push({
                materielCode: item.materielCode,
                materielId: item.materielId,
                materielName: item.materielName,
                warehouseId: item.warehouseId,
                ...itemD,
              });
            });
          });
          console.log(this.summary_data);
          this.total = pageTotal;
          // 合并单元格
          const getSpanArr = this.$_common.getSpanArr(
            this.summary_data,
            "materielId"
          );
          this.spanArr = getSpanArr.spanArr;
        }
      },
      //  选择仓库
      async getAllWarehouse() {
        const { data } = await getAllWarehouse({
          page: 1,
          pageSize: 999,
        });

        this.warehouse_list = data;
        this.warehouseId = data[0].id;
      },
      //  选择时间
      searchTimeChange(val) {
        // 时间组件导出数据
        if (val && val.length) {
          this.startTime = val[0] / 1000;
          this.endTime = val[1] / 1000 + 86399;
        } else {
          this.startTime = "";
          this.endTime = "";
        }
        this.pageChange(1);
      },
      pageChange(page) {
        this.page = page;
        this.inventoryStatistics();
      },
      sizeChange(size) {
        this.pageSize = size;
        this.pageChange(1);
      },
    },
  };
</script>

<style scoped></style>
