<template>
  <!-- 建筑物弹窗 -->
  <el-dialog center ref="dialog" :title="dataTitle" :show-close="false" modal :destory-on-close="true"
    class="dialogClass" :append-to-body="true" width="90%" custom-class="custom-dialog" v-model="props.visible">
    <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">
      <el-table :key="tableKey" v-loading="tableloading" stripe show-summary :summary-method="getSummaries"
        height="450px" border :data="dataObjone" id="out-table" ref="detailTable">
        <el-table-column property="orgname" label="企业名称" width="250"></el-table-column>
        <el-table-column property="area" label="总面积"></el-table-column>
        <el-table-column property="buildingNum" label="建筑物数量"></el-table-column>
        <el-table-column property="officeBuildingNum" label="办公楼数量"></el-table-column>
        <el-table-column property="othersBuildingNum" label="其他楼楼数量"></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, onMounted, reactive, watch, defineEmits } from "vue";
import { getSummaries } from "@/utils/tools";
import { getApi, postApi } from "@/api/tobAcco_api.js";
import { exportXlsx } from "@/excel/Export2Excel.js";
const currentPage = ref("20");
const popForm = ref(false);
const tableloading = ref(false)
const detailTable = ref();
const emit = defineEmits(['changeVi'])
const listQuery = ref({
  orgcode: ''
})
const excelLoading = ref(false)
const tableKey = ref(1)
const props = defineProps({
  visible: false,
  dataTitle: {
    type: String,
    requried: true
  }
});
const dataObjone = ref([])

onMounted(() => {
  getBuildingsData();
  popForm.value = false;
});

// 获取建筑列表
const getBuildingsData = () => {
  dataObjone.value = []
  tableloading.value = true
  getApi(listQuery.value, "/mosty-jcgl/basicData/buildingList").then((res) => {
    dataObjone.value = res ? res : []
  }).finally(() => {
    tableloading.value = false
  })
}



//关闭弹窗事件
function closeDialog() {
  emit("changeVi", false)
  popForm.value = false;
}

//详情按钮
const handleClick = (row) => {
  listQuery.value.orgcode = row.orgid
  popForm.value = true; //显示返回按钮
  getBuildingsData()
  tableKey.value = 2
};


//点击返回
const backbtn = () => {
  listQuery.value.orgcode = ''
  popForm.value = false;
  getBuildingsData()
  tableKey.value = 1
};


const exportExcels = () => {
  excelLoading.value = true
  let title = {
    orgname: '企业名称',
    area: "总面积",
    buildingNum: "建筑物数量",
    officeBuildingNum: "办公楼数量",
    othersBuildingNum: "其他楼楼数量",
  };
  getApi(listQuery.value, "/mosty-jcgl/basicData/buildingList").then((res) => {
    if (res) {
      let data = res;
      let obj = {
        orgname: '合计',
        area: 0,
        buildingNum: 0,
        officeBuildingNum: 0,
        othersBuildingNum: 0,
      }
      if (data) {
        for (let i = 0; i < data.length; i++) {
          obj.area += data[i].area;
          obj.buildingNum += data[i].buildingNum;
          obj.officeBuildingNum += data[i].officeBuildingNum;
          obj.othersBuildingNum += data[i].othersBuildingNum;
        }
        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";

// 死固定住企业名称宽
::v-deep .el-table_4_column_18.is-leaf.el-table__cell {
  width: 250px !important;
}
</style>
