<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">

      <!-- @cell-dblclick="editData" -->
      <el-table :key="tableKey" v-loading="tableloading" id="out-table" stripe show-summary
        :summary-method="getSummaries" ref="detailTable" border :data="hchfxData">
        <el-table-column property="orgName" label="企业名称" width="250"></el-table-column>
        <el-table-column property="redRisk" sortable label="重大风险（红区）数量（个）"></el-table-column>
        <el-table-column property="orangeRisk" sortable label="较大风险（橙区）数量（个）"></el-table-column>
        <el-table-column property="yellowRisk" 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 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 hchfxData = ref([
  {
    "orgName": "甘肃省烟草专卖局（公司）",
    "redRisk": 0,
    "orangeRisk": 0,
    "yellowRisk": 0
  },
  {
    "orgName": "兰州市烟草专卖局（公司）",
    "redRisk": 3,
    "orangeRisk": 3,
    "yellowRisk": 4
  },
  // {
  //     "orgName": "天水市烟草专卖局（公司）",
  //     "redRisk": 2,
  //     "orangeRisk": 1,
  //     "yellowRisk": 3
  // },
  // {
  //     "orgName": "陇南市烟草专卖局（公司）",
  //     "redRisk": 0,
  //     "orangeRisk": 4,
  //     "yellowRisk": 1
  // },
  // {
  //     "orgName": "酒泉市烟草专卖局（公司）",
  //     "redRisk": 1,
  //     "orangeRisk": 1,
  //     "yellowRisk": 2
  // },
  // {
  //     "orgName": "嘉峪关市烟草专卖局（公司）",
  //     "redRisk": 0,
  //     "orangeRisk": 2,
  //     "yellowRisk": 1
  // },
  // {
  //     "orgName": "庆阳市烟草专卖局（公司）",
  //     "redRisk": 1,
  //     "orangeRisk": 0,
  //     "yellowRisk": 2
  // },
  // {
  //     "orgName": "白银市烟草专卖局（公司）",
  //     "redRisk": 2,
  //     "orangeRisk": 4,
  //     "yellowRisk": 1
  // },
  // {
  //     "orgName": "定西市烟草专卖局（公司）",
  //     "redRisk": 0,
  //     "orangeRisk": 2,
  //     "yellowRisk": 1
  // },
  // {
  //     "orgName": "金昌市烟草专卖局（公司）",
  //     "redRisk": 1,
  //     "orangeRisk": 4,
  //     "yellowRisk": 1
  // },
  // {
  //     "orgName": "平凉市烟草专卖局（公司）",
  //     "redRisk": 2,
  //     "orangeRisk": 0,
  //     "yellowRisk": 3
  // },
  // {
  //     "orgName": "武威市烟草专卖局（公司）",
  //     "redRisk": 3,
  //     "orangeRisk": 0,
  //     "yellowRisk": 1
  // },
  // {
  //     "orgName": "甘南州烟草专卖局（公司）",
  //     "redRisk": 4,
  //     "orangeRisk": 3,
  //     "yellowRisk": 0
  // },
  // {
  //     "orgName": "临夏州烟草专卖局（公司）",
  //     "redRisk": 2,
  //     "orangeRisk": 2,
  //     "yellowRisk": 4
  // },
  // {
  //     "orgName": "贵州烟叶复烤有限责任公司",
  //     "redRisk": 1,
  //     "orangeRisk": 1,
  //     "yellowRisk": 4
  // }
]);


onMounted(() => {
  getReoryeData();
  popForm.value = false;

});

// 获取红黄橙数据
const getReoryeData = () => {
  hchfxData.value = []
  getApi({}, "/mosty-jcgl/bmbs/royRiskListDisplay").then((res) => {
    hchfxData.value = res ? res : []
  })
}

// 红橙黄详情数据
const getDetails = () => {
  hchfxData.value = []
  getApi(listQuery.value, "/mosty-jcgl/bmbs/royRiskDetails").then((res) => {
    hchfxData.value = res.list ? res.list : []
  })
}
//详情按钮
const handleClick = (index, row) => {
  tableKey.value = 2
  var val = row.cityCode;
  listQuery.value.code = val
  popForm.value = true; //显示返回按钮
  getDetails()
};

//关闭弹窗事件
function closeDialog() {
  emit("changeVi", false)
  popForm.value = false;
}
//单元格双击事件
function editData(e) {
  var val = e.name;
  popForm.value = true; //显示返回按钮
}

//点击返回
const backbtn = () => {
  getReoryeData();
  tableKey.value = 1
  popForm.value = false;
};
const exportExcels = () => {
  excelLoading.value = true
  let title = {
    orgName: '企业名称',
    redRisk: "重大风险（红区）数量（个）",
    orangeRisk: "较大风险（橙区）数量（个）",
    yellowRisk: "一般风险（黄区）数量（个）",
  };
  const param = listQuery.value
  let data = structuredClone(hchfxData.value);
  let obj = {
    orgName: '合计',
    redRisk: 0,
    orangeRisk: 0,
    yellowRisk: 0,
  }
  if (data) {
    for (let i = 0; i < data.length; i++) {
      obj.redRisk += data[i].redRisk;
      obj.orangeRisk += data[i].orangeRisk;
      obj.yellowRisk += data[i].yellowRisk;
    }
    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";

::v-deep .el-table__body-wrapper {
  height: 400px !important;
}
</style>
