<template>
  <!-- 应急演练弹窗 -->
  <el-dialog center ref="dialog" :title="dataTitle" :show-close="false" modal :destory-on-close="true"
    class="dialogClass" :append-to-body="true" width="871px" 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" id="out-table" show-summary stripe
        :summary-method="getSummaries" ref="detailTable" height="450px" border :data="sgsjdata">
        <el-table-column property="orgName" label="企业名称" width="250"></el-table-column>
        <el-table-column property="participantsNum" sortable label="参与人数">
          <template #default="scope">{{ scope.row.participantsNum === null ? 0 : scope.row.participantsNum
          }}</template>
        </el-table-column>
        <el-table-column property="drillCount" sortable label="演练总数"></el-table-column>
        <!-- <el-table-column label="操作" v-if="!popForm">
          <template #default="scope">
            <span class="operesee" @click="handleClick(scope.$index, scope.row)"><el-icon>
                <View />
              </el-icon>详情</span>
          </template>
        </el-table-column> -->
      </el-table>
    </div>

  </el-dialog>
</template>

<script setup>
import { ref, defineProps, onMounted, reactive, watch, defineEmits } from "vue";
import { getApi } from "@/api/tobAcco_api.js";
import { getSummaries } from "@/utils/tools";
import { exportXlsx } from "@/excel/Export2Excel.js";
const tableloading = ref(false)
const excelLoading = ref(false)
const popForm = ref(false);
const detailTable = ref();
const emit = defineEmits(['changeVi'])
const listQuery = ref({
  code: ''
});
const tableKey = ref(1)
const props = defineProps({
  visible: false,
  dataTitle: {
    type: String,
    requried: true
  }
});

const sgsjdata = ref([
  {
    "orgName": "甘肃省烟草专卖局（公司）",
    "participantsNum": 47,
    "drillCount": 98
  },
  {
    "orgName": "兰州市烟草专卖局（公司）",
    "participantsNum": 17,
    "drillCount": 23
  },
  // {
  //     "orgName": "天水市烟草专卖局（公司）",
  //     "participantsNum": 52,
  //     "drillCount": 73
  // },
  // {
  //     "orgName": "陇南市烟草专卖局（公司）",
  //     "participantsNum": 90,
  //     "drillCount": 10
  // },
  // {
  //     "orgName": "酒泉市烟草专卖局（公司）",
  //     "participantsNum": 8,
  //     "drillCount": 98
  // },
  // {
  //     "orgName": "嘉峪关市烟草专卖局（公司）",
  //     "participantsNum": 58,
  //     "drillCount": 58
  // },
  // {
  //     "orgName": "庆阳市烟草专卖局（公司）",
  //     "participantsNum": 6,
  //     "drillCount": 23
  // },
  // {
  //     "orgName": "白银市烟草专卖局（公司）",
  //     "participantsNum": 27,
  //     "drillCount": 17
  // },
  // {
  //     "orgName": "定西市烟草专卖局（公司）",
  //     "participantsNum": 84,
  //     "drillCount": 20
  // },
  // {
  //     "orgName": "金昌市烟草专卖局（公司）",
  //     "participantsNum": 2,
  //     "drillCount": 14
  // },
  // {
  //     "orgName": "平凉市烟草专卖局（公司）",
  //     "participantsNum": 3,
  //     "drillCount": 15
  // },
  // {
  //     "orgName": "武威市烟草专卖局（公司）",
  //     "participantsNum": 42,
  //     "drillCount": 38
  // },
  // {
  //     "orgName": "甘南州烟草专卖局（公司）",
  //     "participantsNum": 14,
  //     "drillCount": 93
  // },
  // {
  //     "orgName": "临夏州烟草专卖局（公司）",
  //     "participantsNum": 56,
  //     "drillCount": 2
  // },
  // {
  //     "orgName": "贵州烟叶复烤有限责任公司",
  //     "participantsNum": 80,
  //     "drillCount": 43
  // }
]);

onMounted(() => {
  getReverData(); //应急演练列表
  popForm.value = false;


});
// 获取应急演练列表
const getReverData = () => {
  sgsjdata.value = []
  getApi({}, "/mosty-jcgl/yjyl/dataListDisplay").then((res) => {
    sgsjdata.value = res ? res : []
  })
}
// 获取应急演练详情
const getDetails = () => {
  sgsjdata.value = []
  getApi(listQuery.value, "/mosty-jcgl/yjyl/dataDetails").then((res) => {
    sgsjdata.value = res.list ? res.list : []
  })
}


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

//详情按钮
const handleClick = (index, row) => {
  var val = row.cityCode;
  popForm.value = true; //显示返回按钮
  listQuery.value.code = val
  getDetails();
  tableKey.value = 2
};

//单元格双击事件
function editData(e) {
  var val = e.name;
  popForm.value = true; //显示返回按钮
}
//点击返回
const backbtn = () => {
  popForm.value = false;
  getReverData(); //应急演练列表
  tableKey.value = 1
};
const exportExcels = () => {
  excelLoading.value = true
  let title = {
    orgName: '企业名称',
    participantsNum: "参与人数",
    drillCount: "演练总数",
  };
  const param = listQuery.value
  let data = structuredClone(sgsjdata.value);
  let obj = {
    orgName: '合计',
    participantsNum: 0,
    drillCount: 0,
    yellowRisk: 0,
  }
  if (data) {
    for (let i = 0; i < data.length; i++) {
      obj.participantsNum += data[i].participantsNum;
      obj.drillCount += data[i].drillCount;
    }
    data.push(obj)
    let tablename = "应急演练台账.xlsx";
    exportXlsx(title, data, '应急演练台账', tablename);
    excelLoading.value = false
  }
  // getApi(param, "/mosty-jcgl/basicData/tzsbList").then((res) => {
  //   if (res) {

  //     excelLoading.value = false
  //   }
  // }).catch(() => {
  //   excelLoading.value = false
  // });
};
</script>

<style lang="scss" scoped>
@import "~@/assets/css/element-plus.scss";
</style>
