<template>
  <!--暂降 -->
  <el-dialog
    :close-on-click-modal="false"
    draggable
    v-model="machineVisible"
    title="暂降事件"
    width="1200px"
  >
    <div class="tableBox">
      <div style="display: flex;justify-content: flex-end;">
        <el-button
          type="primary"
          :icon="Download"
          @click="exportTable"
          size="small"
          >导出
        </el-button>
      </div>
      <el-table
        :scrollbar-always-on="true"
        :data="tableData"
        height="443px"
        size="small"
        v-loading="loading"
        element-loading-background="#343849c7"
        :header-cell-style="{ textAlign: 'center' }"
        border
        style="margin-top: 13px;"
      >
        <!-- <el-table-column type="index" align="center" label="序号" width="70" /> -->
          <el-table-column label="序号" align="center" type="index" width="70">
          <template #default="scope">
            <span>{{
              (form.pageNum - 1) * form.pageSize + scope.$index + 1
              }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="startTime"
          align="center"
          label="发生时间"
          show-overflow-tooltip
          width="160"
        />
        <el-table-column prop="stationName" align="center" label="变电站" />
        <el-table-column prop="lineName" align="center" label="监测点" />
        <el-table-column
          prop="objName"
          label="用户"
          align="center"
          show-overflow-tooltip
        >
          <template v-slot="scope">
            <span>{{ scope.row.objName ? scope.row.objName : "/" }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="eventType"
          align="center"
          label="触发类型"
          width="80"
        />
        <el-table-column
          prop="featureAmplitude"
          align="center"
          label="残余电压(%)"
          width="100"
        >
          <template v-slot="scope">
            <span>{{ (scope.row.featureAmplitude * 100).toFixed(2) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="duration"
          align="center"
          label="持续时间(s)"
          width="100"
        />
        <el-table-column fixed="right" label="操作" width="80" align="center">
          <template #default="scope">
            <el-button
              link
              type="primary"
              size="small"
              @click.stop="trendCharts(scope.row)"
              >波形</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      size="small"
      style="margin-top: 10px"
      :currentPage="form.pageNum"
      :page-size="form.pageSize"
      :page-sizes="[10, 20, 50, 100, 200]"
      background
      :layout="'sizes,total, ->, prev, pager, next, jumper'"
      :total="total"
      @size-change="onTableSizeChange"
      @current-change="onTableCurrentChange"
    ></el-pagination>
  </el-dialog>
  <!-- 波形图 -->
  <el-dialog
    :close-on-click-modal="false"
    draggable
    v-model="trendVisible"
    v-if="trendVisible"
    title="波形"
    width="70%"
    @close="handleCloseTrend"
  >
    <waveForm ref="waveFormRef" />
  </el-dialog>
</template>
<script lang="ts" setup>
import { ref, reactive, inject } from "vue";
import { eventListByLineId } from "@/api/manage_wx";
import waveForm from "@/components/BX/waveForm.vue";
import * as XLSX from "xlsx";
import { Download } from "@element-plus/icons-vue";

const machineVisible = ref(false);
const form = reactive({
  lineId: "",
  pageNum: 1,
  pageSize: 20,
  searchBeginTime: "",
  searchEndTime: "",
});
const total = ref(0); // 假设总条数为100
const loading = ref(false);
const tableData = ref([]);
import { useStore } from "vuex";

const store = useStore();

const trendVisible = ref(false);

const waveFormRef = ref();
//点击波形图
const trendCharts = (row: any) => {
  row.eventdetail_index = row.eventId;
  machineVisible.value = false;
  trendVisible.value = true;
  setTimeout(() => {
    waveFormRef.value?.open({
      ...row,
      bdname: row.stationName,
      pointname: row.lineName,
      timeid: row.startTime,
      eventvalue: row.featureAmplitude,
      persisttime: row.duration,
    });
  }, 500);
};

// 关闭波形图
const handleCloseTrend = () => {
  trendVisible.value = false;
  machineVisible.value = true;
};
const open = async (id: any) => {
  form.pageNum = 1;
  form.pageSize = 20;
  form.lineId = id;
  machineVisible.value = true;
  await init();
};

const init = async () => {
  loading.value = true;
  eventListByLineId({
    lineId: form.lineId,
    searchBeginTime: store.state.timeValue[0],
    searchEndTime: store.state.timeValue[1],
  }).then((res) => {
    tableData.value = res.data.records;
    total.value = res.data.total;
    loading.value = false;
  });
};

const onTableSizeChange = (size: number) => {
  form.pageSize = size;
  init();
};
const onTableCurrentChange = (page: number) => {
  form.pageNum = page;
  init();
};

// 暂降溯源导出
const exportTable = async () => {
  let columnExpor: any = [
    [
      "发生时间",
      "变电站",
      "监测点",
      "用户",
      "触发类型",
      "残余电压(%)",
      "持续时间(s)",
    ],
  ];

  let list = [];

  await eventListByLineId({
   lineId: form.lineId,
    searchBeginTime: store.state.timeValue[0],
    searchEndTime: store.state.timeValue[1],
    pageNum: 1,
    pageSize: total.value,
  }).then((res) => {
    let data = res.data.records.map((item) => {
      return [
        item.startTime,
        item.stationName,
        item.lineName,
        item.objName,
        item.eventType,
        (item.featureAmplitude * 100).toFixed(2),
        item.duration,
      ];
    });
    list = [...columnExpor, ...data];
    // 创建工作表
    const worksheet = XLSX.utils.aoa_to_sheet(list);

    worksheet["!cols"] = list.map((col) => ({ wch: 20 }));

    // 创建工作簿
    const workbook = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(workbook, worksheet, "暂降事件");

    // 写出文件
    XLSX.writeFile(workbook, "暂降事件" + ".xlsx");
  });
};

defineExpose({ open });
</script>
<style lang="scss" scoped>
@use "@/assets/scss/index.scss";
.tableBox {
  padding: 0px !important;
}
.formBox {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}
.formLeft {
  display: flex;
  align-items: center;
  color: #fff;
}
</style>
