<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="props.visible">
    <section class="button">
      <el-button @click="exportExcels" :loading="excelLoading">导出</el-button>
      <el-button @click="closeDialog">关闭</el-button>
    </section>
    <div class="itemTrading">

      <el-table id="out-table" v-loading="tableloading" stripe show-summary :summary-method="getSummaries"
        ref="detailTable" height="450px" border :data="sgsjdata">
        <el-table-column property="orgName" label="企业名称" width="250"></el-table-column>
        <el-table-column label='事故概况'>
          <el-table-column label='责任事故数量'>
            <el-table-column property="mainResp" sortable label="主责及以上"></el-table-column>
            <el-table-column property="equalResp" sortable label="等责数量"></el-table-column>
            <el-table-column property="secondaryResp" sortable label="次责数量"></el-table-column>
            <!-- <el-table-column property="total" sortable label="合计"></el-table-column> -->
          </el-table-column>
          <el-table-column property="die" sortable label="死亡人数"></el-table-column>
          <el-table-column property="injury" sortable label="重伤人数"></el-table-column>
          <el-table-column property="directLoss" sortable label="直接经济损失（万元）"></el-table-column>
        </el-table-column>
        <el-table-column label='交通事故'>
          <el-table-column property="transDie" sortable label="死亡人数"></el-table-column>
          <el-table-column property="transInjury" sortable label="重伤人数"></el-table-column>
          <el-table-column property="transSlight" sortable label="轻伤人数"></el-table-column>
          <el-table-column property="transDirectLoss" sortable label="直接经济损失（万元）"></el-table-column>
        </el-table-column>
      </el-table>
    </div>
  </el-dialog>
</template>

<script setup>
import { ref, defineProps, onMounted, defineEmits, nextTick } 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 props = defineProps({
  visible: false,
  dataTitle: {
    type: String,
    requried: true
  }
});

const sgsjdata = ref([
  {
    "orgName": "甘肃省烟草专卖局（公司）",
    "mainResp": 6,
    "equalResp": 8,
    "secondaryResp": 3,
    "die": 2,
    "injury": 6,
    "directLoss": 5,
    "transDie": 3,
    "transInjury": 5,
    "transSlight": 6,
    "transDirectLoss": 4
  },
  {
    "orgName": "兰州市烟草专卖局（公司）",
    "mainResp": 2,
    "equalResp": 8,
    "secondaryResp": 2,
    "die": 5,
    "injury": 2,
    "directLoss": 4,
    "transDie": 0,
    "transInjury": 6,
    "transSlight": 8,
    "transDirectLoss": 7
  },
  {
    "orgName": "天水市烟草专卖局（公司）",
    "injury": 9,
    "directLoss": 6,
    "mainResp": 2,
    "equalResp": 5,
    "secondaryResp": 3,
    "die": 5,
    "transDie": 2,
    "transInjury": 0,
    "transSlight": 5,
    "transDirectLoss": 5
  },
  {
    "orgName": "陇南市烟草专卖局（公司）",
    "injury": 4,
    "directLoss": 2,
    "mainResp": 8,
    "equalResp": 0,
    "secondaryResp": 5,
    "die": 4,
    "transDie": 9,
    "transInjury": 3,
    "transSlight": 4,
    "transDirectLoss": 7
  },
  {
    "orgName": "酒泉市烟草专卖局（公司）",
    "injury": 7,
    "directLoss": 6,
    "mainResp": 4,
    "equalResp": 4,
    "secondaryResp": 8,
    "die": 5,
    "transDie": 6,
    "transInjury": 1,
    "transSlight": 8,
    "transDirectLoss": 8
  },
  {
    "orgName": "嘉峪关市烟草专卖局（公司）",
    "injury": 1,
    "directLoss": 7,
    "mainResp": 7,
    "equalResp": 3,
    "secondaryResp": 2,
    "die": 8,
    "transDie": 9,
    "transInjury": 4,
    "transSlight": 3,
    "transDirectLoss": 0
  },
  {
    "orgName": "庆阳市烟草专卖局（公司）",
    "injury": 7,
    "directLoss": 2,
    "mainResp": 5,
    "equalResp": 4,
    "secondaryResp": 3,
    "die": 4,
    "transDie": 3,
    "transInjury": 3,
    "transSlight": 4,
    "transDirectLoss": 8
  },
  {
    "orgName": "白银市烟草专卖局（公司）",
    "injury": 6,
    "directLoss": 2,
    "mainResp": 5,
    "equalResp": 7,
    "secondaryResp": 0,
    "die": 8,
    "transDie": 3,
    "transInjury": 5,
    "transSlight": 8,
    "transDirectLoss": 4
  },
  {
    "orgName": "张掖市烟草专卖局（公司）",
    "injury": 7,
    "directLoss": 2,
    "mainResp": 4,
    "equalResp": 9,
    "secondaryResp": 2,
    "die": 9,
    "transDie": 7,
    "transInjury": 6,
    "transSlight": 7,
    "transDirectLoss": 7
  },
  {
    "orgName": "定西市烟草专卖局（公司）",
    "injury": 9,
    "directLoss": 9,
    "mainResp": 3,
    "equalResp": 1,
    "secondaryResp": 9,
    "die": 8,
    "transDie": 3,
    "transInjury": 9,
    "transSlight": 9,
    "transDirectLoss": 0
  },
  {
    "orgName": "金昌市烟草专卖局（公司）",
    "injury": 9,
    "directLoss": 6,
    "mainResp": 6,
    "equalResp": 0,
    "secondaryResp": 2,
    "die": 4,
    "transDie": 3,
    "transInjury": 8,
    "transSlight": 2,
    "transDirectLoss": 2
  },
  {
    "orgName": "平凉市烟草专卖局（公司）",
    "injury": 8,
    "directLoss": 1,
    "mainResp": 0,
    "equalResp": 7,
    "secondaryResp": 5,
    "die": 2,
    "transDie": 4,
    "transInjury": 7,
    "transSlight": 6,
    "transDirectLoss": 5
  },
  {
    "orgName": "武威市烟草专卖局（公司）",
    "injury": 6,
    "directLoss": 5,
    "mainResp": 9,
    "equalResp": 8,
    "secondaryResp": 8,
    "die": 2,
    "transDie": 6,
    "transInjury": 4,
    "transSlight": 1,
    "transDirectLoss": 6
  },
  {
    "orgName": "甘南州烟草专卖局（公司）",
    "injury": 7,
    "directLoss": 0,
    "mainResp": 2,
    "equalResp": 6,
    "secondaryResp": 0,
    "die": 8,
    "transDie": 5,
    "transInjury": 3,
    "transSlight": 7,
    "transDirectLoss": 2
  },
  {
    "orgName": "临夏州烟草专卖局（公司）",
    "injury": 4,
    "directLoss": 7,
    "mainResp": 3,
    "equalResp": 6,
    "secondaryResp": 0,
    "die": 8,
    "transDie": 8,
    "transInjury": 4,
    "transSlight": 1,
    "transDirectLoss": 0
  }
]);
onMounted(() => {
  // getAccidentData();
  popForm.value = false;

});

// 事件事故列表展示
const getAccidentData = () => {
  sgsjdata.value = []
  getApi({}, "/mosty-jcgl/sgsj/listDisplay").then((res) => {
    sgsjdata.value = res ? res : []
  })
}

//关闭弹窗事件
function closeDialog() {
  emit("changeVi", false)
  popForm.value = false;
}
const exportExcels = () => {
  excelLoading.value = true
  let title = {
    orgName: '企业名称',
    mainResp: "主责及以上",
    equalResp: "等责数量",
    secondaryResp: "次责数量",
    die: "事故死亡人数",
    injury: "事故重伤人数",
    directLoss: "事故直接经济损失（万元）",
    transDie: "交通事故死亡人数",
    transInjury: "交通事故重伤人数",
    transSlight: "交通事故轻伤人数",
    transDirectLoss: "交通事故直接经济损失（万元）",
  };
  // const param = listQuery.value
  let data = structuredClone(sgsjdata.value);
  let obj = {
    orgName: '合计',
    mainResp: 0,
    equalResp: 0,
    secondaryResp: 0,
    die: 0,
    injury: 0,
    directLoss: 0,
    transDie: 0,
    transInjury: 0,
    transSlight: 0,
    transDirectLoss: 0,
  }
  if (data) {
    for (let i = 0; i < data.length; i++) {
      obj.mainResp += data[i].mainResp;
      obj.equalResp += data[i].equalResp;
      obj.secondaryResp += data[i].secondaryResp;
      obj.die += data[i].die;
      obj.injury += data[i].injury;
      obj.directLoss += data[i].directLoss;
      obj.transDie += data[i].transDie;
      obj.transInjury += data[i].transInjury;
      obj.transSlight += data[i].transSlight;
      obj.transDirectLoss += data[i].transDirectLoss;
    }
    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>
