<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 v-if="popForm" @click="backbtn">返回</el-button>
      <el-button @click="closeDialog">关闭</el-button>
    </section>


    <div class="itemTrading">
      <!-- @cell-dblclick="editData" -->
      <el-table :key="tableKey" v-loading="tableloading" id="out-table" 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 property="contPlanTotal" sortable label="应急预案总数"></el-table-column>
        <el-table-column property="overallPlan" sortable label="综合应急预案"></el-table-column>
        <el-table-column property="special" sortable label="专项应急预案"></el-table-column>
        <el-table-column property="emergencyDisposal" sortable label="应急处置方案"></el-table-column>
        <el-table-column property="siteDeal" v-if="popForm" 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, postApi } 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 tableKey = ref(1)
const popForm = ref(false);
const detailTable = ref();
const emit = defineEmits(['changeVi'])
const listQuery = ref({
  code: ''
});
const props = defineProps({
  visible: false,
  dataTitle: {
    type: String,
    requried: true
  }
});

const sgsjdata = ref(
  [
    {
      "orgName": "甘肃省烟草专卖局（公司）",
      "contPlanTotal": 59,
      "overallPlan": 4,
      "special": 50,
      "emergencyDisposal": 16
    },
    {
      "orgName": "兰州市烟草专卖局（公司）",
      "contPlanTotal": 48,
      "overallPlan": 65,
      "special": 21,
      "emergencyDisposal": 62
    },
    {
      "orgName": "天水市烟草专卖局（公司）",
      "contPlanTotal": 28,
      "overallPlan": 45,
      "special": 73,
      "emergencyDisposal": 38
    },
    {
      "orgName": "陇南市烟草专卖局（公司）",
      "contPlanTotal": 12,
      "overallPlan": 25,
      "special": 80,
      "emergencyDisposal": 52
    },
    {
      "orgName": "酒泉市烟草专卖局（公司）",
      "contPlanTotal": 56,
      "overallPlan": 18,
      "special": 74,
      "emergencyDisposal": 11
    },
    {
      "orgName": "嘉峪关市烟草专卖局（公司）",
      "contPlanTotal": 97,
      "overallPlan": 29,
      "special": 60,
      "emergencyDisposal": 18
    },
    {
      "orgName": "庆阳市烟草专卖局（公司）",
      "contPlanTotal": 65,
      "overallPlan": 17,
      "special": 89,
      "emergencyDisposal": 30
    },
    {
      "orgName": "白银市烟草专卖局（公司）",
      "contPlanTotal": 24,
      "overallPlan": 63,
      "special": 74,
      "emergencyDisposal": 39
    },
    {
      "orgName": "定西市烟草专卖局（公司）",
      "contPlanTotal": 59,
      "overallPlan": 66,
      "special": 80,
      "emergencyDisposal": 19
    },
    {
      "orgName": "金昌市烟草专卖局（公司）",
      "contPlanTotal": 49,
      "overallPlan": 44,
      "special": 1,
      "emergencyDisposal": 69
    },
    {
      "orgName": "平凉市烟草专卖局（公司）",
      "contPlanTotal": 76,
      "overallPlan": 94,
      "special": 70,
      "emergencyDisposal": 61
    },
    {
      "orgName": "武威市烟草专卖局（公司）",
      "contPlanTotal": 37,
      "overallPlan": 67,
      "special": 79,
      "emergencyDisposal": 97
    },
    {
      "orgName": "甘南州烟草专卖局（公司）",
      "contPlanTotal": 78,
      "overallPlan": 7,
      "special": 1,
      "emergencyDisposal": 30
    },
    {
      "orgName": "临夏州烟草专卖局（公司）",
      "contPlanTotal": 49,
      "overallPlan": 51,
      "special": 57,
      "emergencyDisposal": 55
    },
  ]
);

onMounted(() => {
  // getPreviewData();
  popForm.value = false;

});

// 应急预案列表展示
const getPreviewData = () => {
  sgsjdata.value = []
  getApi({}, "/mosty-jcgl/yabz/listDisplay").then((res) => {
    sgsjdata.value = res ? res : []
  })
}


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

// 获取详情列表数据
const getDetails = () => {
  sgsjdata.value = []
  getApi(listQuery.value, "/mosty-jcgl/yabz/details").then((res) => {
    sgsjdata.value = res.list ? res.list : []
  })
}
//详情按钮
const handleClick = (index, row) => {
  tableKey.value = 2
  popForm.value = true
  // document.getElementById('out-table').scrollLeft=0
  var val = row.code;
  listQuery.value.code = val
  popForm.value = true; //显示返回按钮
  getDetails()
};
//单元格双击事件
function editData(e) {
  var val = e.name;
  popForm.value = true; //显示返回按钮
}
//点击返回
const backbtn = () => {
  tableKey.value = 1
  popForm.value = false;
  getPreviewData()
};
const exportExcels = () => {
  excelLoading.value = true
  let title = {
    orgName: '企业名称',
    contPlanTotal: "应急预案总数",
    overallPlan: "综合应急预案",
    special: "专项应急预案",
    emergencyDisposal: '应急处置方案'
  };
  const param = listQuery.value
  let data = structuredClone(sgsjdata.value);
  let obj = {
    orgName: '合计',
    contPlanTotal: 0,
    overallPlan: 0,
    special: 0,
    emergencyDisposal: 0
  }
  if (data) {
    for (let i = 0; i < data.length; i++) {
      obj.contPlanTotal += data[i].contPlanTotal;
      obj.overallPlan += data[i].overallPlan;
      obj.special += data[i].special;
      obj.emergencyDisposal += data[i].emergencyDisposal;
    }
    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>
