<template>
  <!--物流配送中心弹窗 -->
  <el-dialog center ref="dialog" :title="dataTitle" modal :show-close="false" :destory-on-close="true"
    class="dialogClass" :append-to-body="true" width="871px" custom-class="custom-dialog" v-model="isShow">

    <section class="button">
      <el-button @click="exportExcels" :loading="excelLoading">导出</el-button>
      <el-button v-if="popForm" @click="backbtn">返回</el-button>
      <el-button @click="closeDialog">关闭</el-button>
    </section>
    <div class="itemTrading" v-if="dataTitle == '物流配送中心'">
      <el-table show-summary v-loading="tableloading" stripe :summary-method="getSummaries" :key=tableKey
        ref="detailTable" height="450px" border :data="wlpszxList" id="out-table">
        <el-table-column property="orgname" label="企业名称" width="250"></el-table-column>
        <el-table-column property="deliveryNum" label="年配送量"></el-table-column>
        <el-table-column property="transferStation" label="中转站个数"></el-table-column>
        <el-table-column label="动力保障设备">
          <el-table-column property="boiler" label="锅炉总数"></el-table-column>
          <el-table-column property="vacuum" label="真空设备数量"></el-table-column>
          <el-table-column property="airPressure" label="空压设备数量"></el-table-column>
          <el-table-column property="refrigeration" label="制冷设备数量"></el-table-column>
          <el-table-column property="highPressure" label="高压设备数量"></el-table-column>
          <el-table-column property="transformer" label="变压器数量"></el-table-column>
          <el-table-column property="dynamo" label="发电机数量"></el-table-column>
        </el-table-column>
        <el-table-column label="卷烟配送">
          <el-table-column property="deliveryCar" label="配送车数量"></el-table-column>
        </el-table-column>
        <el-table-column label="操作" v-if="!popForm">
          <template #default="{ row }">
            <span class="operesee" @click="handleClick(row)" v-if="row.orgid.slice(0, 4) != '5200'"><el-icon>
                <View />
              </el-icon>详情</span>
            <span v-else>--</span>

          </template>
        </el-table-column>
      </el-table>
    </div>
  </el-dialog>
</template>
<script setup>
import { ref, defineProps, reactive, onMounted, defineEmits } from "vue";
import { getApi, postApi } from "@/api/tobAcco_api.js";
import { getSummaries } from "@/utils/tools";
import { exportXlsx } from "@/excel/Export2Excel.js";
const props = defineProps({
  title: String,
  isShow: Boolean
});
const excelLoading = ref(false)
const tableloading = ref(false)
const tableKey = ref(1)
const wlpszxList = ref([]);
const emit = defineEmits(['changeVi'])
const popForm = ref(false);
const isShow = ref(false);
const currentPage = ref("20");
const dataTitle = props.title;;
const detailTable = ref();
const listQuery = ref({
  orgcode: ''
});
//关闭弹窗的回调
function handleClose() {
  emit('changeVi', false)
  currentPage.value = 1;
}

onMounted(() => {
  popForm.value = false;
  isShow.value = true;
  getLogisticsData();
});

// 物流中心列表
const getLogisticsData = () => {
  wlpszxList.value = []
  tableloading.value = true
  getApi(listQuery.value, '/mosty-jcgl/basicData/logisticsListDisplay').then((res) => {
    wlpszxList.value = res ? res : []
  }).finally(() => {
    tableloading.value = false
  })
}
//详情按钮
const handleClick = (row) => {
  listQuery.value.orgcode = row.orgid
  popForm.value = true; //显示返回按钮
  getLogisticsData()
  tableKey.value = 2
};
//点击返回
const backbtn = () => {
  listQuery.value.orgcode = ''
  popForm.value = false;
  getLogisticsData()
  tableKey.value = 1
};
//关闭弹窗事件
function closeDialog() {
  emit('changeVi', false)
  popForm.value = false;
}

const exportExcels = () => {
  excelLoading.value = true
  let title = {
    orgname: '企业名称',
    deliveryNum: "年配送量",
    transferStation: "中转站个数",
    boiler: "锅炉总数",
    vacuum: "真空设备数量",
    airPressure: "空压设备数量",
    refrigeration: "制冷设备数量",
    highPressure: "高压设备数量",
    transformer: "变压器数量",
    dynamo: "发电机数量",
    deliveryCar: "配送车数量",
  };

  getApi(listQuery.value, "/mosty-jcgl/basicData/logisticsListDisplay").then((res) => {
    if (res) {
      let data = res;
      let obj = {
        orgname: '合计',
        deliveryNum: 0,
        transferStation: 0,
        boiler: 0,
        vacuum: 0,
        airPressure: 0,
        refrigeration: 0,
        highPressure: 0,
        transformer: 0,
        dynamo: 0,
        deliveryCar: 0,
      }
      if (data) {
        for (let i = 0; i < data.length; i++) {
          obj.deliveryNum += data[i].deliveryNum;
          obj.transferStation += data[i].transferStation;
          obj.boiler += data[i].boiler;
          obj.vacuum += data[i].vacuum;
          obj.airPressure += data[i].airPressure;
          obj.refrigeration += data[i].refrigeration;
          obj.highPressure += data[i].highPressure;
          obj.transformer += data[i].transformer;
          obj.dynamo += data[i].dynamo;
          obj.deliveryCar += data[i].deliveryCar;
        }
        data.push(obj)
        let tablename = "物流配送台账.xlsx";
        exportXlsx(title, data, '物流配送台账', tablename);
        excelLoading.value = false
      }
      excelLoading.value = false
    }
  }).catch(() => {
    excelLoading.value = false
  });
};
</script>
<style lang="scss" scoped>
@import "~@/assets/css/element-plus.scss";
</style>
