<template>
  <!-- 运行控制数据 -->
  <div class="pubDataBox">

    <!-- 运行控制弹窗 -->
    <el-dialog center ref="dialog" :title="props.title" modal :show-close="false" :destory-on-close="true"
      class="dialogClass" :append-to-body="true" width="90%" custom-class="custom-dialog" v-model="isShow">
      <section class="button">
        <el-button @click="exportExcels">导出</el-button>
        <el-button v-if="popForm" @click="backbtn">返回</el-button>
        <el-button @click="closeDialog">关闭</el-button>
      </section>

      <!-- 应急演练 -->
      <!-- <div class="binBox" v-else-if="dataTitle == '应急演练'">
        <BinManyBar :barTitle="yaBartitle" :barNum="yaBarnum" :loadingshow="binshow" />
      </div> -->

      <!-- 月报事故 -->
      <div>
        <el-table v-if="!popForm" stripe height="450px" border id="out-table" :data="controldata"
          @cell-dblclick="editData" show-summary :summary-method="getSummaries">
          <el-table-column property="comName" label="单位名称" width="250"></el-table-column>
          <el-table-column property="ysb" label="已上报"></el-table-column>
          <el-table-column property="wsb" label="未上报"></el-table-column>
          <!-- 临时注释 start -->
          <!-- <el-table-column label="操作" v-if="!popForm" fixed="right">
            <template #default="scope">
              <span class="operesee" @click="handleClick(scope.$index, scope.row)"><el-icon>
                  <View />
                </el-icon>详情</span>
            </template>
</el-table-column> -->
          <!-- 临时注释 end -->
        </el-table>

        <!-- 详情 -->
        <el-table v-if="popForm" stripe height="450px" border id="out-table" :data="detailData"
          @cell-dblclick="editData" show-summary :summary-method="getSummaries">
          <el-table-column property="comName" label="单位名称" width="250"></el-table-column>
          <el-table-column property="ysb" label="已上报"></el-table-column>
          <el-table-column property="wsb" label="未上报"></el-table-column>
          <el-table-column property="bz" label="备注"></el-table-column>
          <el-table-column property="sbsj" label="上报时间"></el-table-column>
        </el-table>
        <!-- <el-table v-else-if="listQuery.num == 1"
        stripe height="450px" border id="out-table"
        :data="controldata" @cell-dblclick="editData">
        <el-table-column property="name" label="单位名称"></el-table-column>
        <el-table-column label="报告名称">{{ "烟草" }}</el-table-column>
        <el-table-column property="sl" label="状态"></el-table-column>
        <el-table-column property="sl" label="安全状态"></el-table-column>
        <el-table-column label="上报事件">{{ "烟草" }}</el-table-column>
        <el-table-column label="操作" fixed="right">
          <template #default="scope">
            <el-button v-if="!popForm" size="mini" @click="handleClick(scope.$index, scope.row)">详情</el-button>
            <el-button disabled size="mini" v-else>详情</el-button>
          </template>
        </el-table-column>
      </el-table> -->
      </div>

    </el-dialog>
  </div>
</template>

<script setup>
import BinManyBar from "../../charts/binManyBar.vue";
import { getApi, postApi } from "@/api/tobAcco_api.js";
import { exportExcel } from "@/utils/tools";
import { ref, defineProps, reactive, onMounted, nextTick, defineEmits } from "vue";
import { exportXlsx } from "@/excel/Export2Excel.js";
import { fakeSgypData } from "../../fakeData.js"

const props = defineProps({
  title: String,
  isShow: Boolean,
  num: Number
});
//运行控制的数据
const controldata = ref([]);
const detailData = ref([])
const emit = defineEmits(['changeVi'])
// const datalist = ref([]);
const binshow = ref(false);
//饼状图数据

// 应急演练柱状图
const yaBartitle = ref(["专项", "综合", "现场处置", "应急处置卡"]);
const yaBarnum = ref([
  {
    data: [120, 132, 101, 134],
    color: "#7BA87B"
  }
]);
const listQuery = ref({
  code: ''
});

//分公司表格数据
const popForm = ref(false);
const isShow = ref(false);
/** 合计数组 */
const sumData = ref([])

const exportExcels = () => {
  const titleObj = {
    comName: '单位名称',
    ysb: '已上报',
    wsb: '未上报'
  }
  const sumObj = {
    comName: '合计',
    ysb: sumData.value[1],
    wsb: sumData.value[2]
  }
  const data = structuredClone([...controldata.value, sumObj])
  exportXlsx(titleObj, data, '事故月报', '事故月报.xlsx')
}
// 获取事故月报列表
const getAccidentData = () => {
  controldata.value = []
  getApi({}, '/mosty-jcgl/sgyb/getSgList').then((res) => {
    controldata.value = res ? res : []
  })
}

// 获取事故月报详情
const getDetails = () => {
  detailData.value = []
  getApi(listQuery.value, '/mosty-jcgl/sgyb/getSgDetails/{cityCode}').then((res) => {
    detailData.value = res.list ? res.list : []
  })
}

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

//点击返回
const backbtn = () => {
  popForm.value = false;
};
onMounted(() => {

  // 设置假数据1 start=====
  controldata.value = fakeSgypData()
  isShow.value = true;
  return
  // 设置假数据1 end =====

  isShow.value = true;
  binshow.value = true;
  popForm.value = false;
  getAccidentData();
});


//公司名称双击事件
function editData(row, column) {
  //双击单位名称获取城市编号
  if (column.property == 'comName') {
  }
  // popForm.value = true; //显示返回按钮
}

//关闭弹窗事件
function closeDialog() {
  emit("changeVi", false)
  popForm.value = false;
}
//表格合计
function getSummaries(param) {
  const { columns, data } = param;
  const sums = [];
  columns.forEach((column, index) => {
    if (index === 0) {
      sums[index] = "甘肃省局(合计)";
      return;
    }
    const values = data.map((item) => Number(item[column.property]));
    if (!values.every((value) => isNaN(value))) {
      sums[index] = values.reduce((prev, curr) => {
        const value = Number(curr);
        if (!isNaN(value)) {
          return prev + curr;
        } else {
          return prev;
        }
      }, 0);
    } else {
      sums[index] = "N/A";
    }
  });
  sumData.value = sums // 合计数据方便导出用
  return sums;
}

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

.binBox .right {
  width: 50%;
  position: relative;

  .rate {
    position: absolute;
    top: 50%;
    left: 70%;
    color: #fff;
    font-size: 20px;
  }
}

.el-dialog--center .el-dialog__body {
  padding: 0px;
}

.excel-btn {
  position: absolute;
  top: 19px;
  right: 5%;
  background: #025c29;
  color: #0ecd0f;
  border-color: #025c29;
}

.square {
  width: 180px;
  height: 180px;
  background-color: rgb(43, 100, 62);
  border-radius: 20px;
  text-align: center;
  color: #fff;

  h3 {
    font-size: 18px;
    line-height: 1;
    padding: 0;
  }

  h1 {
    font-size: 100px;
    line-height: 1;
    margin: 0;
    padding: 0;
  }
}
</style>
