<template>
  <!--暂降 -->
  <el-dialog
    :close-on-click-modal="false"
    draggable
    v-model="machineVisible"
    title="完整性不足详情"
    :before-close="handleClose"
    width="1000px"
  >
    <div class="formBox">
      <el-form label-width="70px" :model="form" inline>
        <el-form-item label="关键字">
          <el-input
            style="width: 150px"
            v-model="form.searchValue"
            placeholder="关键字查询"
            clearable
            size="small"
          />
        </el-form-item>
      </el-form>
      <div>
        <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"
        :header-cell-style="{ textAlign: 'center' }"
        v-loading="loading"
        element-loading-background="#343849c7"
        border
      >
        <el-table-column prop="userNo" align="center" label="户号" />
        <el-table-column prop="userName" align="center" label="用户名" />
        <el-table-column
          prop="lineNo"
          align="center"
          label="测量点局号"
          width="180"
        />
        <el-table-column
          prop="updateTime"
          align="center"
          label="日期"
          width="180"
        />
        <el-table-column
          prop="integrity"
          align="center"
          label="完整性(%)"
          width="130"
        >
          <template #default="{ row }">
            {{ Math.floor(row.integrity * 10000) / 100 }}
          </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 { Search, Download } from "@element-plus/icons-vue";
import { userDataIntegrityList } from "@/api/manage_wx";
import { useStore } from "vuex";
import * as XLSX from "xlsx";
const emits = defineEmits(["close"]);
const store = useStore();
const machineVisible = ref(false);
const form = reactive({
  searchValue: "",
  pageNum: 1,
  pageSize: 20,
  userDataId: "",
});
const loading = ref(false);
const total = ref(0); // 假设总条数为100
const tableData = ref([]);
//form表单校验规则
const open = (row) => {
  machineVisible.value = true;
  form.searchValue = "";
  form.pageNum = 1;
  form.pageSize = 20;
  form.userDataId = row.id;

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

const getTableData = async () => {
  loading.value = true;
  const res: any = await userDataIntegrityList(form);

  if (res.code == "A0000") {
    tableData.value = res.data.records;
    total.value = res.data.total;
  }
  loading.value = false;
};
const handleClose = () => {
  machineVisible.value = false;
  emits("close");
};

// 导出
const exportTable = async () => {
  let columnExpor: any = [
    ["户号", "用户名", "测量点局号", "日期", "完整性(%)"],
  ];

  let list = [];

  await userDataIntegrityList({
    ...form,
    pageNum: 1,
    pageSize: total.value,
  }).then((res) => {
    let data = res.data.records.map((item) => {
      return [
        item.userNo,
        item.userName,
        item.lineNo,
        item.updateTime,
        Math.floor(item.integrity * 10000) / 100,
      ];
    });
    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");
  });
};

onMounted(() => {});

defineExpose({ open });
</script>
<style lang="scss" scoped>
@use "@/assets/scss/index.scss";

.tableBox {
  padding: 0px !important;
}

.formBox {
  display: flex;
  justify-content: space-between;
}
</style>
