<template>
  <div class="app-container">

    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="储配站" prop="stationId">
        <el-select v-model="queryParams.stationId" placeholder="请选择储配站">
          <el-option
            v-for="item in stationList"
            :key="item.stationId"
            :label="item.stationName"
            :value="item.stationId"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          style="color: #1890ff"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getInfo"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bottleInfoList" show-summary :summary-method="getSummaries">
      <el-table-column label="储配站" align="center" prop="stationName" />
      <el-table-column label="气瓶数量" align="center" prop="totalNum" />
      <el-table-column label="正常数量" align="center" prop="normalNum"/>
      <el-table-column label="逾期数量" align="center" prop="overNum" />
      <el-table-column label="报废数量" align="center" prop="junkNum" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getInfo"
    />
  </div>
</template>

<script>
  import { bottleStatistics, bottleStatisticsExport} from "@/api/gasBottleTrack/gasBottleManage";
  import { gasStorageStationList } from "@/api/gasBottleTrack/gasHolderStationManage";
  export default {
    name: "index",
    data(){
      return{
        bottleInfoList: [],
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          stationId: null
        },
        total: 0,
        loading: false,
        // 显示搜索条件
        showSearch: true,
        stationList: [],
        exportLoading: false
      }
    },
    created(){
      this.getInfo();
      this.getStationInfo();
    },
    methods:{
      getInfo(){
        this.loading = true;
        bottleStatistics(this.queryParams).then(res =>{
          this.loading = false;
          if(res.code == 200){
               this.bottleInfoList = res.rows;
               this.total = res.total;
          }
        })
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getInfo();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      getStationInfo(){
        gasStorageStationList().then(res =>{
          if(res.code == 200){
            this.stationList = res.data;
          }
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有气瓶信息统计数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
        return bottleStatisticsExport(queryParams);
      }).then(response => {
          this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
      },
      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 === "totalNum" ||
          column.property === "normalNum" ||
          column.property === "overNum" ||
          column.property === "junkNum"
        ) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
          if (!isNaN(value)) {
            return prev + curr;
          } else {
            return prev;
          }
        }, 0);
          sums[index] = sums[index].toFixed(0);
        }
      });
        return sums;
      }
    }
  }
</script>

<style scoped>

</style>
