<template>
  <div class="content-box">
    <div class="left">
      <!-- <el-icon :size="23" class="camera"><VideoCameraFilled /></el-icon> -->
      <span class="cameratxt">校友信息管理</span>
    </div>
    <div class="scroll">
      <div class="middle">
        <div class="filter">
          <div class="condition">
            <span>姓名 :</span>
            <el-input
              clearable
              v-model.trim="searchInput.name"
              class="w-50 m-2"
              placeholder="请输入姓名"
              style="width: 180px"
            />
          </div>
          <div class="condition">
            <span>院系 :</span>
            <el-select
              clearable
              v-model="searchInput.collegeId"
              placeholder="请选择院系"
              @change="collegeChange"
            >
              <el-option
                v-for="i in collegeData"
                :key="i.id"
                :label="i.name"
                :value="i.id"
              />
            </el-select>
          </div>
          <div class="condition">
            <span>入学年 :</span>
            <el-select
              clearable
              v-model="searchInput.periodId"
              placeholder="请选择入学年"
              @change="periodChange"
            >
              <el-option
                v-for="i in periodData"
                :key="i.id"
                :label="i.name"
                :value="i.id"
              />
            </el-select>
          </div>

          <div class="condition">
            <span>专业 :</span>
            <el-select
              clearable
              v-model="searchInput.majorId"
              placeholder="请选择专业"
              @change="majorChange"
            >
              <el-option
                v-for="i in majorData"
                :key="i.id"
                :label="i.name"
                :value="i.id"
              />
            </el-select>
          </div>
          <div class="condition">
            <span>班级 :</span>
            <el-select
              clearable
              v-model="searchInput.classId"
              placeholder="请选择班级"
            >
              <el-option
                v-for="i in classData"
                :key="i.id"
                :label="i.name"
                :value="i.id"
              />
            </el-select>
          </div>
          <div class="condition">
            <span>身份认证 :</span>
            <el-select
              clearable
              v-model="searchInput.identityType"
              placeholder="请选择身份认证"
            >
              <el-option
                v-for="i in identityData"
                :key="i.id"
                :label="i.name"
                :value="i.id"
              />
            </el-select>
          </div>

          <el-button
            style="margin-left: 20px"
            color="rgba(38, 151, 255, 1)"
            type="primary"
            class="search"
            @click="searchBtn"
            ><span>查询</span></el-button
          >
          <el-button @click="resetBtn" plain color="rgba(43, 153, 255, 1)"
            >重置</el-button
          >
        </div>
        <!-- 按钮列表 -->
        <div class="gongneng">
          <el-button
            type="primary"
            color="rgba(38, 151, 255, 1)"
            @click="importBtn"
            v-if="menuList.canImport"
            >导入</el-button
          >
          <el-button
            type="primary"
            style="margin-left: 0"
            color="rgba(48, 201, 191, 1)"
            @click="exportbtn"
            v-if="menuList.canExport"
            >导出</el-button
          >
          <el-button
            type="primary"
            style="margin-left: 0"
            color="rgba(38, 151, 255, 1)"
            @click="addlist"
            v-if="menuList.canInsert"
            >新增</el-button
          >
        </div>
      </div>
      <div class="footer" v-loading="loading">
        <el-table
          :row-class-name="tableRowClassName"
          :data="tableData.list"
          @selection-change="handleSelectionChange"
          style="width: 100%"
          :header-cell-style="{
            background: 'rgba(240, 243, 247, 1)',
            height: '50px',
            border: 0,
          }"
        >
          <!-- <el-table-column type="selection" align="center" width="55" /> -->
          <el-table-column align="center" prop="userName" label="姓名" />
          <el-table-column
            align="center"
            prop="identityTypeStr"
            width="100"
            label="身份认证"
          />
          <el-table-column
            align="center"
            prop="cardNumber"
            width="190"
            label="身份证号"
          />
          <el-table-column width="100" align="center" label="性别">
            <template #default="{ row }">
              <span>{{ row.genderStr }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="periodName" label="入学年" />
          <el-table-column align="center" prop="graduateName" label="毕业年" />
          <el-table-column
            align="center"
            prop="collegeName"
            width="130"
            label="学院"
          />
          <el-table-column
            align="center"
            prop="majorName"
            width="150"
            label="专业"
          />
          <el-table-column
            align="center"
            prop="className"
            width="130"
            label="班级"
          />
          <el-table-column
            align="center"
            prop="phone"
            width="170"
            label="手机号码"
          />
          <el-table-column align="center" prop="employer" label="工作单位" />
          <el-table-column align="center" prop="status" label="工作地区">
            <template #default="{ row }">
              <span>{{ row.workloca }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="position" label="职务" />
          <el-table-column
            align="center"
            prop="mailbox"
            label="邮箱"
            width="200"
          />
          <el-table-column
            align="center"
            label="操作"
            fixed="right"
            width="200"
          >
            <template #default="{ row }">
              <el-button
                v-if="menuList.canUpdate"
                type="primary"
                @click="updateS(row)"
                link
                >编辑</el-button
              >
              <el-button
                v-if="menuList.canDeleted"
                type="danger"
                @click="deleteS(row)"
                link
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <div class="pageSize">
          <span></span>
          <el-pagination
            background
            :current-page="currentPage"
            :page-size="pageSize"
            :page-sizes="[10, 20, 30, 40]"
            layout="total,sizes, prev, pager, next, jumper, slot"
            :total="total"
            @size-change="handleSizeChange"
            @update:current-page="handleCurrentChange"
          />
        </div>
      </div>
    </div>
    <!-- 添加账号弹窗 -->
    <el-dialog
      class="addStaff"
      v-model="addDialogVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :title="dialongTitle"
      align-center
      width="560"
      :before-close="cancelAdd"
      destroy-on-close
      draggable
    >
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
        :size="formSize"
        label-position="right"
        status-icon
      >
        <el-form-item label="姓名 :" prop="userName">
          <el-input
            clearable
            v-model.trim="ruleForm.userName"
            class="w-50 m-2"
            placeholder="请输入姓名"
          />
        </el-form-item>
        <el-form-item label="性别 :" prop="gender">
          <el-select
            clearable
            v-model="ruleForm.gender"
            placeholder="请选择性别"
          >
            <el-option label="男" :value="`男,1`" />
            <el-option label="女" :value="`女,2`" />
          </el-select>
        </el-form-item>
        <el-form-item label="身份认证 :" prop="identityType">
          <el-select
            clearable
            v-model="ruleForm.identityType"
            placeholder="请选择身份认证"
          >
            <el-option
              v-for="i in identityData"
              :key="i.id"
              :label="i.name"
              :value="`${i.name},${i.id}`"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="身份证号 :" prop="cardNumber">
          <el-input
            clearable
            v-model.trim="ruleForm.cardNumber"
            class="w-50 m-2"
            placeholder="请输入身份证号"
          />
        </el-form-item>
        <el-form-item label="毕业年 :" prop="graduateId">
          <el-select
            clearable
            v-model="ruleForm.graduateId"
            placeholder="请选择毕业年"
          >
            <el-option
              v-for="i in graduateData"
              :key="i.id"
              :label="i.name"
              :value="`${i.name},${i.id}`"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="所属学院 :" prop="collegeId">
          <el-select
            @change="collegeRuleChange"
            v-model="ruleForm.collegeId"
            clearable
            placeholder="请选择所属学院"
          >
            <el-option
              v-for="i in collegeData"
              :key="i.id"
              :label="i.name"
              :value="`${i.name},${i.id}`"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="入学年 :" prop="periodId">
          <el-select
            @change="periodRuleChange"
            v-model="ruleForm.periodId"
            clearable
            placeholder="请选择入学年"
          >
            <el-option
              v-for="i in periodRuleData"
              :key="i.id"
              :label="i.name"
              :value="`${i.name},${i.id}`"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="专业 :" prop="majorId">
          <el-select
            clearable
            v-model="ruleForm.majorId"
            placeholder="请选择专业"
            @change="majorRuleChange"
          >
            <el-option
              v-for="i in majorRuleData"
              :key="i.id"
              :label="i.name"
              :value="`${i.name},${i.id}`"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="班级 :" prop="classId">
          <el-select
            clearable
            v-model="ruleForm.classId"
            placeholder="请选择班级"
          >
            <el-option
              v-for="i in classRuleData"
              :key="i.id"
              :label="i.name"
              :value="`${i.name},${i.id}`"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="手机号码 :" prop="phone">
          <el-input
            clearable
            v-model.trim="ruleForm.phone"
            class="w-50 m-2"
            placeholder="请输入手机号码"
          />
        </el-form-item>
        <el-form-item label="工作单位 :" prop="employer">
          <el-input
            v-model.trim="ruleForm.employer"
            placeholder="请输入工作单位"
            clearable
          />
        </el-form-item>
        <el-form-item label="工作地区 :" prop="workloca">
          <el-input
            v-model.trim="ruleForm.workloca"
            placeholder="请输入工作地区"
            clearable
          />
        </el-form-item>
        <el-form-item label="职务 :" prop="position">
          <el-input
            v-model.trim="ruleForm.position"
            placeholder="请输入职务"
            clearable
          />
        </el-form-item>
        <el-form-item label="邮箱 :" prop="mailbox">
          <el-input
            v-model.trim="ruleForm.mailbox"
            placeholder="请输入邮箱"
            clearable
          />
        </el-form-item>
        <el-form-item class="options">
          <el-button @click="cancelAdd">取消</el-button>
          <el-button
            color="rgba(0, 97, 255, 1)"
            class="queding"
            type="primary"
            @click="submitAdd(ruleFormRef)"
          >
            确定
          </el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 项目导入 -->
    <el-dialog
      class="projectImport"
      v-model="buildImportVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      title="校友信息导入"
      align-center
      width="500"
      :before-close="cancelProjectImport"
    >
      <!-- <p class="title">当前只支持项目类型为“非装配式建筑项目”的项目导入</p> -->
      <p class="down">
        <span>校友信息导入模板下载</span
        ><span @click="templateDown">模板下载</span>
      </p>
      <el-upload
        class="avatar-uploader"
        action="#"
        :auto-upload="false"
        :on-remove="projectImportRemove"
        :on-change="projectImportChange"
        :before-upload="beforeAvatarProImport"
        ref="buildRef"
        :limit="1"
        :on-exceed="projectImportExceed"
      >
        <template #trigger>
          <el-button class="queding" type="primary">
            &nbsp;&nbsp;导入文件&nbsp;&nbsp;
          </el-button>
        </template>
      </el-upload>
      <div class="options">
        <el-button
          color="rgba(9, 101, 98, 1)"
          class="queding"
          type="primary"
          @click="projectImportConfirm(buildRef)"
          :loading="buildImportLoading"
        >
          &nbsp;&nbsp;确认导入&nbsp;&nbsp;
        </el-button>
        <el-button @click="cancelProjectImport"
          >&nbsp;&nbsp;取消导入&nbsp;&nbsp;</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import {
  ref,
  watch,
  reactive,
  nextTick,
  onBeforeMount,
  onMounted,
  onUnmounted,
} from "vue";
import { useRouter } from "vue-router";
import { genFileId, ElMessage, ElMessageBox } from "element-plus";
import { dayjs } from "element-plus";
import lodash from "lodash";

import { storeToRefs } from "pinia";
import { useCounterStore } from "@/stores/index";

import {
  queryUsersPage,
  insertUserData,
  updateUserData,
  deleteUserDataById,
  downloadUserExcel,
  importUserExcel,
  alumniUserExport,
  queryGraduate,
  queryColleges,
  queryPeriods,
  queryMajors,
  queryClasses,
  queryIdentitys,
} from "@/api/alumni-info.js";

const router = useRouter();
const store = useCounterStore();

// 为避免解构时失去响应性
const { name, age, isCollapse, realAge, collegeRole } = storeToRefs(store);

const menuList = ref({
  canAll: null,
  canApply: null,
  canDeleted: null,
  canDetail: null,
  canExport: null,
  canImport: null,
  canInsert: null,
  canTop: null,
  canUpdate: null,
  canView: null,
});

// 表格数据
const loading = ref(false);
const tableData = reactive({
  list: [{}],
});
const dialongTitle = ref("新增账号"); // 弹窗标题

const searchInput = reactive({
  name: "",
  collegeId: null,
  periodId: null,
  majorId: null,
  classId: null,
  identityType: "",
}); // 搜索按钮数据

const currentPage = ref(1); // 当前页
const pageSize = ref(10);
const total = ref(0); // 当前总数
const selectIds = ref([]); // 勾选的全部数据

const addDialogVisible = ref(false); // 控制添加账号弹窗

// 表单数据
const formSize = ref("default");
const ruleFormRef = ref();
const ruleForm = reactive({
  userName: "", //
  cardNumber: "", // 身份证号
  gender: "", // 性别ID
  identityType: "", // 身份认证ID
  graduateId: "", // 毕业年ID
  collegeId: "", // 学院ID
  periodId: "", // 入学年ID
  majorId: "", //专业ID
  classId: "", // 班级ID
  phone: "", // 手机号码
  employer: "", // 工作单位
  workloca: "", // 工作地区
  position: "", // 职务
  mailbox: "", // 邮箱
  id: "",
});
// 表单验证
const rules = reactive({
  userName: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
  cardNumber: [
    { required: true, message: "身份证号不能为空", trigger: "blur" },
  ],
  gender: [{ required: true, message: "性别不能为空", trigger: "blur" }],
  identityType: [
    { required: true, message: "身份认证不能为空", trigger: "blur" },
  ],
  graduateId: [{ required: true, message: "毕业年不能为空", trigger: "blur" }],
  collegeId: [{ required: true, message: "所属学院不能为空", trigger: "blur" }],
  periodId: [{ required: true, message: "入学年不能为空", trigger: "blur" }],
  majorId: [{ required: true, message: "专业不能为空", trigger: "blur" }],
  classId: [{ required: true, message: "班级不能为空", trigger: "blur" }],
  phone: [{ required: true, message: "手机号码不能为空", trigger: "blur" }],
  employer: [{ required: true, message: "工作单位不能为空", trigger: "blur" }],
  workloca: [{ required: true, message: "工作地区不能为空", trigger: "blur" }],
  position: [{ required: true, message: "职务不能为空", trigger: "blur" }],
  mailbox: [{ required: true, message: "邮箱不能为空", trigger: "blur" }],
});

// 导入
const buildImportVisible = ref(false);
const buildRef = ref();
const buildFile = ref();
const buildImportLoading = ref(false);

// 学院
const collegeData = ref([]);
// 入学年
const periodData = ref([]);
// 专业
const majorData = ref([]);
// 班级
const classData = ref([]);

// 入学年
const periodRuleData = ref([]);
// 专业
const majorRuleData = ref([]);
// 班级
const classRuleData = ref([]);

// 身份认证
const identityData = ref();
// 毕业年份下拉
const graduateData = ref();

// 获取账户列表
const getList = async () => {
  loading.value = true;
  let params = {
    currentPage: currentPage.value, // 当前页
    pageCount: pageSize.value, // 一页数据条数
    name: searchInput.name,
    collegeId: searchInput.collegeId,
    periodId: searchInput.periodId,
    majorId: searchInput.majorId,
    classId: searchInput.classId,
    identityType: searchInput.identityType,
  };

  queryUsersPage(params).then((res) => {
    console.log(res, "校友分页列表数据");
    if (res.code == 200) {
      tableData.list = res.data.list;
      total.value = res.data.totalCount;
      loading.value = false;
    } else {
      loading.value = false;
      ElMessage({
        type: "error",
        showClose: true,
        message: res.message,
        center: true,
      });
    }
  });
};

// 搜索功能
const searchBtn = lodash.debounce(async () => {
  currentPage.value = 1;
  getList();
}, 300);
const resetBtn = lodash.debounce(async () => {
  searchInput.name = "";
  searchInput.collegeId = "";
  searchInput.periodId = "";
  periodData.value=[];
  searchInput.majorId = "";
  majorData.value=[];
  searchInput.classId = "";
  classData.value=[];
  searchInput.identityType = "";
  currentPage.value = 1;
  getList();
}, 300);

// 添加账号
const addlist = () => {
  dialongTitle.value = "新增校友信息";
  addDialogVisible.value = true;
  // 逐个清空所有字段（统一设为空字符串，保持类型一致）
  ruleForm.userName = "";
  ruleForm.cardNumber = "";
  ruleForm.gender = "";
  ruleForm.identityType = "";
  ruleForm.graduateId = "";
  ruleForm.collegeId = "";
  ruleForm.periodId = "";
  ruleForm.majorId = "";
  ruleForm.classId = "";
  ruleForm.phone = "";
  ruleForm.employer = "";
  ruleForm.workloca = "";
  ruleForm.position = "";
  ruleForm.mailbox = "";
  ruleForm.id = "";
  // 清空表单校验状态（可选，需先定义 ruleFormRef）
  if (ruleFormRef.value) {
    ruleFormRef.value.clearValidate();
  }
};
// 添加账号
const updateS = async (row) => {
  console.log(row);
  dialongTitle.value = "编辑校友信息";
  addDialogVisible.value = true;
  ruleForm.userName = row.userName || "";
  ruleForm.cardNumber = row.cardNumber || "";
  ruleForm.gender = `${row.genderStr},${row.gender}`;
  ruleForm.identityType = `${row.identityTypeStr},${row.identityType}`;
  ruleForm.graduateId = `${row.graduateName},${row.graduateId}`;
  ruleForm.collegeId = `${row.collegeName},${row.collegeId}`;
  // 入学年
  {
    if (row.collegeId) {
      let params = {
        collegeId: row.collegeId,
      };
      let res = await queryPeriods(params);
      if (res.code == 200) {
        periodRuleData.value = res.data;
        ruleForm.periodId = `${row.periodName},${row.periodId}`;
      } else {
        ElMessage({
          type: "error",
          showClose: true,
          message: res.message,
          center: true,
        });
      }
    }
  }
  // 专业
  {
    if (row.periodId) {
      let params = {
        periodId: row.periodId,
      };
      let res = await queryMajors(params);
      console.log(res, "专业下拉列表数据");
      if (res.code == 200) {
        majorRuleData.value = res.data;
        ruleForm.majorId = `${row.majorName},${row.majorId}`;
      } else {
        ElMessage({
          type: "error",
          showClose: true,
          message: res.message,
          center: true,
        });
      }
    }
  }
  // 班级
  {
    if (row.majorId) {
      let params = {
        majorId: row.majorId,
      };
      let res = await queryClasses(params);
      console.log(res, "班级下拉列表数据");
      if (res.code == 200) {
        classRuleData.value = res.data;
        ruleForm.classId = `${row.className},${row.classId}`;
      } else {
        ElMessage({
          type: "error",
          showClose: true,
          message: res.message,
          center: true,
        });
      }
    }
  }

  ruleForm.phone = row.phone || "";
  ruleForm.employer = row.employer || "";
  ruleForm.workloca = row.workloca || "";
  ruleForm.position = row.position || "";
  ruleForm.mailbox = row.mailbox || "";
  ruleForm.id = row.id || "";
};
const deleteS = async (row) => {
  ElMessageBox.confirm("是否删除此数据?", "提示!!!", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      let params = {
        id: row.id,
      };
      let res = await deleteUserDataById(params);
      if (res.code == 200) {
        getList();
        ElMessage({
          type: "success",
          showClose: true,
          message: res.message,
          center: true,
        });
      } else {
        ElMessage({
          type: "error",
          showClose: true,
          message: res.message,
          center: true,
        });
      }
    })
    .catch(() => {
      loading.value = false;
    });
};

// 确认添加员工
const submitAdd = lodash.debounce(async (formEl) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      let data = {
        userName: ruleForm.userName,
        cardNumber: ruleForm.cardNumber,
        gender: ruleForm.gender.split(",")[1],
        genderStr: ruleForm.gender.split(",")[0],
        identityType: ruleForm.identityType.split(",")[1],
        identityTypeStr: ruleForm.identityType.split(",")[0],
        graduateId: ruleForm.graduateId.split(",")[1],
        graduateName: ruleForm.graduateId.split(",")[0],
        collegeId: ruleForm.collegeId.split(",")[1],
        collegeName: ruleForm.collegeId.split(",")[0],
        periodId: ruleForm.periodId.split(",")[1],
        periodName: ruleForm.periodId.split(",")[0],
        majorId: ruleForm.majorId.split(",")[1],
        majorName: ruleForm.majorId.split(",")[0],
        classId: ruleForm.classId.split(",")[1],
        className: ruleForm.classId.split(",")[0],
        phone: ruleForm.phone,
        employer: ruleForm.employer,
        workloca: ruleForm.workloca,
        position: ruleForm.position,
        mailbox: ruleForm.mailbox,
      };
      let res = "";
      if (dialongTitle.value == "新增校友信息") {
        res = await insertUserData(data);
      } else {
        data.id = ruleForm.id;
        res = await updateUserData(data);
      }
      if (res.code == 200) {
        addDialogVisible.value = false;
        ElMessage({
          type: "success",
          showClose: true,
          message: res.message,
          center: true,
        });
        getList();
      } else {
        ElMessage({
          type: "error",
          showClose: true,
          message: res.message,
          center: true,
        });
      }
    } else {
      console.log("error submit!", fields);
    }
  });
}, 1000);

const cancelAdd = () => {
  addDialogVisible.value = false;
};

// 多选框功能
const handleSelectionChange = (val) => {
  // console.log(val);
  selectIds.value = val.map((i) => i.id);
  console.log(selectIds.value);
};

// 表格斑马纹颜色修改
const tableRowClassName = ({ row, rowIndex }) => {
  if (rowIndex % 2 === 0) {
    return "even";
  } else if (rowIndex % 2 !== 0) {
    return "odd";
  }
  return "";
};
// 每页显示条数
const handleSizeChange = (value) => {
  console.log(value, "每页显示条数");
  pageSize.value = value;
  getList();
};
// 分页
const handleCurrentChange = (value) => {
  // console.log(value);
  currentPage.value = value;
  getList();
};

// 楼栋导出
const exportbtn = async () => {
  try {
    const params = {
      name: searchInput.name,
      collegeId: searchInput.collegeId,
      periodId: searchInput.periodId,
      majorId: searchInput.majorId,
      classId: searchInput.classId,
      identityType: searchInput.identityType,
    };

    // 1. 请求接口获取流数据
    const res = await alumniUserExport(params);
    console.log(res, "校友信息管理导出");

    // 2. 处理流数据：转为 Blob 对象（判断是否是已解析的 Blob，兼容不同封装）
    const blob =
      res instanceof Blob
        ? res
        : new Blob([res], {
            type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // Excel MIME 类型
          });

    // 3. 提取后端返回的文件名（从响应头获取，若无则用默认名）
    let fileName = "校友信息导出.xlsx";
    // 如果后端在响应头返回了文件名（如 Content-Disposition），解析它
    if (res.headers?.["content-disposition"]) {
      const disposition = res.headers["content-disposition"];
      fileName = decodeURIComponent(
        disposition.split("filename=")[1] || fileName,
      );
    }

    // 4. 创建临时下载链接并触发下载
    const url = window.URL.createObjectURL(blob);
    const link = document.createElement("a");
    link.href = url;
    link.download = fileName; // 设置下载文件名
    document.body.appendChild(link);
    link.click(); // 触发点击下载

    // 5. 清理临时资源，避免内存泄漏
    document.body.removeChild(link);
    window.URL.revokeObjectURL(url);

    ElMessage.success("导出成功，文件已开始下载");
  } catch (error) {
    console.error("导出失败：", error);
    ElMessage.error("导出失败，请重试");
  }
};
// 楼栋导入 (--------------------------------------------------)
const importBtn = () => {
  buildImportVisible.value = true;
  buildFile.value = "";
  if (buildRef.value) {
    buildRef.value.clearFiles();
  }
};
// 模板下载
const templateDown = async () => {
  let res = await downloadUserExcel();
  console.log(res, "模板下载");
  if (res.code == 200) {
    window.location.href = res.data;
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.message,
      center: true,
    });
  }
};
// 移出照片
const projectImportRemove = (uploadFile, uploadFiles) => {
  console.log(uploadFile, uploadFiles);
  buildFile.value = "";
};
const projectImportChange = async (file, fileLists) => {
  console.log(file, "1111");
  buildFile.value = file.raw;
};
const projectImportExceed = (files) => {
  buildRef.value.clearFiles();
  const file = files[0];
  file.uid = genFileId();
  buildRef.value.handleStart(file);
};

// 导入项目
const projectImportConfirm = async () => {
  buildImportLoading.value = true;
  console.log(buildFile.value, "2222");
  let data = new FormData();
  data.set("file", buildFile.value);
  let res = await importUserExcel(data);
  console.log(res, "导入校友信息");
  if (res.code == 200) {
    buildImportVisible.value = false;
    buildImportLoading.value = false;
    getList();
    ElMessage({
      type: "success",
      showClose: true,
      message: res.message,
      center: true,
    });
  } else {
    buildImportLoading.value = false;
    ElMessage({
      type: "error",
      showClose: true,
      message: res.message,
      center: true,
    });
  }
};
const cancelProjectImport = () => {
  buildImportVisible.value = false;
  buildImportLoading.value = false;
  if (buildRef.value) {
    buildRef.value.clearFiles();
    buildFile.value = null;
  }
};

// 学院下拉列表数据
const collegeList = async () => {
  let res = await queryColleges();
  console.log(res, "学院下拉列表数据");
  if (res.code == 200) {
    collegeData.value = res.data;
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.message,
      center: true,
    });
  }
};

// 入学年下拉列表数据
const collegeChange = async (val) => {
  let params = {
    collegeId: val,
  };
  let res = await queryPeriods(params);
  console.log(res, "入学年下拉列表数据");
  if (res.code == 200) {
    periodData.value = res.data;

    searchInput.periodId = ""; // 入学年
    majorData.value = []; // 专业
    searchInput.majorId = ""; // 专业
    classData.value = []; // 班级
    searchInput.classId = ""; // 班级
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.message,
      center: true,
    });
  }
};

// 专业下拉列表数据
const periodChange = async (val) => {
  let params = {
    periodId: val,
  };
  let res = await queryMajors(params);
  console.log(res, "专业下拉列表数据");
  if (res.code == 200) {
    majorData.value = res.data;

    searchInput.majorId = ""; // 专业
    classData.value = []; // 班级
    searchInput.classId = ""; // 班级
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.message,
      center: true,
    });
  }
};

// 班级下拉列表数据
const majorChange = async (val) => {
  let params = {
    majorId: val,
  };
  let res = await queryClasses(params);
  console.log(res, "班级下拉列表数据");
  if (res.code == 200) {
    classData.value = res.data;
    searchInput.classId = ""; // 班级
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.message,
      center: true,
    });
  }
};

// 入学年下拉列表数据
const collegeRuleChange = async (val) => {
  let params = {
    collegeId: val.split(",")[1],
  };
  let res = await queryPeriods(params);
  console.log(res, "入学年下拉列表数据");
  if (res.code == 200) {
    periodRuleData.value = res.data;

    ruleForm.periodId = ""; // 入学年
    majorRuleData.value = []; // 专业
    ruleForm.majorId = ""; // 专业
    classRuleData.value = []; // 班级
    ruleForm.classId = ""; // 班级
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.message,
      center: true,
    });
  }
};

// 专业下拉列表数据
const periodRuleChange = async (val) => {
  let params = {
    periodId: val.split(",")[1],
  };
  let res = await queryMajors(params);
  console.log(res, "专业下拉列表数据");
  if (res.code == 200) {
    majorRuleData.value = res.data;

    ruleForm.majorId = ""; // 专业
    classRuleData.value = []; // 班级
    ruleForm.classId = ""; // 班级
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.message,
      center: true,
    });
  }
};

// 班级下拉列表数据
const majorRuleChange = async (val) => {
  let params = {
    majorId: val.split(",")[1],
  };
  let res = await queryClasses(params);
  console.log(res, "班级下拉列表数据");
  if (res.code == 200) {
    classRuleData.value = res.data;
    ruleForm.classId = ""; // 班级
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.message,
      center: true,
    });
  }
};

// 身份认证下拉列表数据
const identityList = async (val) => {
  let res = await queryIdentitys();
  console.log(res, "身份认证下拉列表数据");
  if (res.code == 200) {
    identityData.value = res.data;
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.message,
      center: true,
    });
  }
};

// 毕业年份下拉列表数据
const graduateList = async () => {
  let res = await queryGraduate();
  console.log(res, "毕业年份下拉列表数据");
  if (res.code == 200) {
    graduateData.value = res.data;
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.message,
      center: true,
    });
  }
};

onMounted(() => {
  getList();
  collegeList();
  identityList();
  graduateList();
  let list = JSON.parse(sessionStorage.getItem("roleList"));
  console.log(list);
  list.forEach((i) => {
    if (i.menuName == "校友信息管理") {
      i.childs.forEach((item) => {
        if (item.menuName == "校友信息管理") {
          menuList.value = item;
        }
      });
    }
  });
  console.log(menuList.value);
});
onUnmounted(() => {
  // document.removeEventListener("keyup", Enters);
});
</script>

<style scoped lang="scss">
.content-box {
  width: calc(100% - 40px);
  height: calc(100% - 105px);
  margin: 20px auto;
  background-color: #fff;
  color: #000;
  display: flex;
  flex-direction: column;

  .svg {
    width: 22px;
    height: 22px;
  }

  .left {
    width: calc(100% - 60px);
    height: 60px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
    color: #000;
    font-size: 18px;
    font-weight: 600;
    .camera {
      margin-right: 15px;
      color: #4392f7;
    }
  }
  .scroll {
    width: calc(100% - 60px);
    height: calc(100% - 61px);
    margin: 0 auto;
    display: flex;
    flex-direction: column;

    .middle {
      width: calc(100%);
      color: #000;
      .filter {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        .search {
          margin-left: 0 !important;
          color: #fff;
        }
        .condition {
          display: flex;
          align-items: center;
          margin: 10px 30px 10px 0;
          :deep(.el-input .el-input__inner) {
            font-size: 14px;
          }
          .el-select {
            width: 220px;
          }
          span {
            margin: 0 10px 0 0;
          }
        }
      }
      .gongneng {
        margin: 10px 0;
        .el-button {
          color: #fff;
          margin-right: 15px;
        }
      }
    }
    .footer {
      width: calc(100%);
      flex: 1;
      margin: 0 auto;
      overflow: auto;

      .el-table--fit {
        height: calc(100% - 60px);
        :deep(.el-table__header-wrapper) {
          background-color: #000;
          font-size: 15px;
          color: #000;
          .cell {
            color: #000;
          }
        }
        :deep(.el-table__row) {
          height: 50px;
          font-size: 15px;
          color: #000;
        }
        :deep(.el-table__row td) {
          padding: 0;
          border: 0;
        }

        .el-button--primary {
          margin-left: 5px;
        }
        :deep(.el-table__body .even) {
          background-color: #fff;
        }
        :deep(.el-table__body .odd) {
          background-color: rgba(240, 243, 247, 1);
        }
      }

      .pageSize {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin: 0 30px;
        height: 60px;

        span {
          color: #000;
        }

        .el-pagination {
          // width: 1600px;
          :deep(.el-pagination__total) {
            color: #000;
          }

          :deep(.el-pagination__goto) {
            color: #000;
          }

          :deep(.el-pagination__classifier) {
            color: #000;
          }

          :deep(.el-input__wrapper) {
            border: 1px solid rgba(0, 0, 0, 1);
            border-radius: 5px;
            box-shadow: none;
          }

          :deep(.el-pager li) {
            margin: 0 5px;
            border: 1px solid rgba(0, 0, 0, 1);
            border-radius: 5px;
            background-color: transparent;
          }

          :deep(.el-pager li.is-active) {
            // background-color: rgba(0, 97, 255, 0.8);
            border: 1px solid rgba(0, 97, 255, 1);
            color: rgba(0, 97, 255, 1);
          }

          :deep(.btn-prev) {
            margin-right: 5px;
            border: 1px solid rgba(0, 0, 0, 1);
            border-radius: 5px;
            background-color: transparent;
          }

          :deep(.btn-next) {
            margin-left: 5px;
            border: 1px solid rgba(0, 0, 0, 1);
            border-radius: 5px;
            background-color: transparent;
          }
        }
      }
    }
  }

  // 添加员工弹窗样式
  :deep(.addStaff) {
    .el-dialog__body {
      padding: 20px 20px 10px 20px;
      .el-input {
        width: 400px;
        .el-input__suffix-inner {
          color: rgba(61, 81, 232, 1);
        }
      }
      .el-select {
        width: 400px;
      }
      .el-tree {
        width: 400px;
      }
    }
  }

  // 导入
  :deep(.projectImport) {
    .el-dialog__body {
      padding: 20px 30px 0 30px;
      .title {
        color: red;
      }
      .down {
        span:nth-child(1) {
          color: rgba(127, 127, 127, 1);
        }
        span:nth-child(2) {
          margin-left: 25px;
          color: rgba(2, 167, 240, 1);
          cursor: pointer;
        }
      }
      .options {
        margin: 50px 0 30px 0;
        display: flex;
        flex-direction: row-reverse;
        .queding {
          margin-left: 20px;
          background: linear-gradient(
            90deg,
            rgba(38, 151, 255, 1) 0%,
            rgba(102, 181, 255, 1) 100%
          );
          border: none;
        }
        .quxiao {
          border: 1px solid rgba(43, 151, 252, 1);
          color: rgba(43, 151, 252, 1);
        }
      }
    }
  }
}
</style>
