<template>
  <!--暂降 -->
  <el-dialog
    :close-on-click-modal="false"
    draggable
    v-model="machineVisible"
    :title="title"
    width="1000px"
  >
    <div class="formBox">
      <el-form label-width="100px" :model="form" inline>
        <el-form-item label="关键字查询">
          <el-input
            v-model="form.searchValue"
            clearable
            size="small"
            placeholder="请输入关键字查询"
          />
        </el-form-item>
        <el-form-item label="是否监测">
          <el-select
            v-model="form.runFlag"
            clearable
            size="small"
            style="width: 160px"
          >
            <el-option label="是" value="0" />
            <el-option label="否" value="1" />
          </el-select>
        </el-form-item>
      </el-form>
      <div class="mt5">
        <el-button
          type="primary"
          :icon="Search"
          size="small"
          @click="getTableData"
          >查询</el-button
        ><el-button
          type="primary"
          :icon="Download"
          @click="exportTable"
          size="small"
          >导出
        </el-button>
      </div>
    </div>
    <div class="tableBox">
      <el-table
        :scrollbar-always-on="true"
        :data="tableData"
        height="400px"
        size="small"
        v-loading="loading"
        element-loading-background="#343849c7"
        :header-cell-style="{ textAlign: 'center' }"
        border
      >
        <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="stationName" align="center" label="变电站名称" />
        <el-table-column prop="gdName" align="center" label="供电公司" />
        <el-table-column
          prop="stationVoltageLevel"
          align="center"
          label="电压等级"
          width="150"
        />
        <el-table-column
          prop="runFlag"
          align="center"
          label="是否监测"
          width="130"
        >
          <template v-slot="scope">
            <el-tag
              v-if="scope.row.runFlag == 0"
              size="small"
              type="primary"
              effect="dark"
              >是</el-tag
            >
            <el-tag v-else size="small" type="warning" effect="dark">否</el-tag>
          </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>
</template>
<script lang="ts" setup>
import { ref, reactive, inject, onMounted } from "vue";
import { ElMessage } from "element-plus";
import { Search, Download } from "@element-plus/icons-vue";
import { stationPage } from "@/api/manage_wx";
import { useStore } from "vuex";
import * as XLSX from "xlsx";
const store = useStore();
const machineVisible = ref(false);
const title = ref("");
const formRef = ref();
const loading = ref(false);
const form = reactive({
  searchValue: "",
  pageNum: 1,
  pageSize: 20,
  deptId: "",
  runFlag: "",
  // startTime: "",
  // endTime: "",
});
const total = ref(0); // 假设总条数为100
const tableData = ref([]);
//form表单校验规则
const open = (text: string, data?: any) => {
  title.value = text;
  machineVisible.value = true;
  form.searchValue = "";
  form.pageNum = 1;
  form.pageSize = 20;
  form.runFlag = "";
  getTableData();
};

const onTableSizeChange = (size: number) => {
  form.pageSize = size;
  // 重新加载数据
  getTableData();
};
const onTableCurrentChange = (page: number) => {
  form.pageNum = page;
  // 重新加载数据
  getTableData();
};

const getTableData = async () => {
  loading.value = true;
  form.deptId = store.state.deptId;
  // form.startTime = store.state.timeValue[0];
  // form.endTime = store.state.timeValue[1];
  const res: any = await stationPage(form);

  if (res.code == "A0000") {
    tableData.value = res.data.records;
    total.value = res.data.total;
  }
  loading.value = false;
};
// 导出
const exportTable = async () => {
  // 示例数据

  let columnExpor: any = [["变电站名称", "供电公司", "电压等级", "是否监测"]];

  let list = [];

  await stationPage({
    ...form,
    pageNum: 1,
    pageSize: total.value,
  }).then((res) => {
    let data = res.data.records.map((item) => {
      return [
        item.stationName,
        item.gdName,
        item.stationVoltageLevel,
        item.runFlag == 0 ? "是" : "否",
      ];
    });
    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, title.value);

    // 写出文件
    XLSX.writeFile(workbook, title.value + ".xlsx");
  });
};

// onMounted(() => {
//   getTableData();
// });

defineExpose({ open });
</script>
<style lang="scss" scoped>
@use "@/assets/scss/index.scss";
.tableBox {
  padding: 0px !important;
}
.formBox {
  display: flex;
  justify-content: space-between;
}
</style>
