<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-select
              clearable
              @change="schoolChange"
              v-model="searchInput.schoolId"
              placeholder="请选择校区名称"
            >
              <el-option
                v-for="i in schoolData"
                :key="i.id"
                :label="i.school"
                :value="i.id"
              />
            </el-select>
          </div>
          <div class="condition">
            <span>楼栋名称 :</span>
            <el-select
              clearable
              v-model="searchInput.buildId"
              placeholder="请选择楼栋名称"
              @change="buildChange"
            >
              <el-option
                v-for="i in buildData"
                :key="i.id"
                :label="i.build"
                :value="i.id"
              />
            </el-select>
          </div>
          <div class="condition">
            <span>寝室号 :</span>
            <el-select
              v-model="searchInput.dormitoryId"
              placeholder="请选择寝室号"
              clearable
            >
              <el-option
                v-for="i in dormitoryData"
                :key="i.id"
                :label="i.dormitory"
                :value="i.id"
              />
            </el-select>
          </div>
          <div class="condition">
            <span>所属学院 :</span>
            <el-select
              @change="collegeChange"
              v-model="searchInput.collegeId"
              placeholder="请选择所属学院"
              clearable
            >
              <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
              @change="majorChange"
              clearable
              v-model="searchInput.majorId"
              placeholder="请选择专业"
            >
              <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.classstrId"
              placeholder="请选择所属班级"
            >
              <el-option
                v-for="i in classstrData"
                :key="i.id"
                :label="i.name"
                :value="i.id"
              />
            </el-select>
          </div>
          <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.yearId"
              placeholder="请选择年份"
            >
              <el-option
                v-for="i in yearData"
                :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
            v-if="store.BtnRole('studentAccommodationSetting1')"
            type="primary"
            style="margin-left: 0"
            color="rgba(48, 201, 191, 1)"
            @click="buildExportbtn"
            >导出</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
            width="80"
            align="center"
            label="序号"
            type="index"
            index="1"
          />
          <el-table-column
            align="center"
            prop="cardNum"
            width="200"
            label="录取号"
          />
          <el-table-column
            align="center"
            prop="name"
            width="120"
            label="学生姓名"
          />
          <el-table-column
            align="center"
            prop="school"
            width="120"
            label="校区名称"
          />
          <el-table-column
            align="center"
            prop="build"
            width="140"
            label="楼栋名称"
          />
          <el-table-column
            align="center"
            prop="dormitory"
            width="120"
            label="寝室号"
          />
          <el-table-column align="center" prop="number" label="床位号" />
          <el-table-column align="center" prop="sex" width="100" label="性别" />
          <el-table-column
            align="center"
            prop="college"
            width="160"
            label="所属学院"
          />
          <el-table-column align="center" prop="major" width="220" label="专业">
          </el-table-column>
          <el-table-column
            align="center"
            prop="classstr"
            width="220"
            label="所属班级"
          />

          <el-table-column
            align="center"
            prop="instructor"
            width="120"
            label="辅导员"
          />
        </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>
  </div>
</template>

<script setup>
import {
  ref,
  watch,
  reactive,
  nextTick,
  onBeforeMount,
  onUnmounted,
} from "vue";
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { dayjs } from "element-plus";
import lodash from "lodash";
import { https } from "@/utils/request"; // 绝对路径

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

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

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

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

const searchInput = reactive({
  schoolId: null,
  buildId: null,
  dormitoryId: null,
  name: null,
  majorId: null,
  collegeId: null,
  classstrId: null,
  yearId: null,
}); // 搜索按钮数据

const currentPage = ref(1); // 当前页
const pageSize = ref(10);
const total = ref(); // 当前总数
const selectIds = ref([]);

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

// 表单数据
const formSize = ref("default");
const ruleFormRef = ref();
const ruleForm = reactive({
  school: "男", //校区名称
  build: "1", //楼栋名称
  dormitory: "", //宿舍名称
  sex: "", //床位性别
  college: "",
  major: "",
  classstr: "",
  number: "", //床位号
  isCheck: "",
  cardNum: "",
  name: "",
  remark: "",
  id: "",
});
// 表单验证
const rules = reactive({
  school: [{ required: true, message: "校区名称不能为空", trigger: "blur" }],
  build: [{ required: true, message: "楼栋名称不能为空", trigger: "blur" }],
  dormitory: [{ required: true, message: "寝室号不能为空", trigger: "blur" }],
  sex: [{ required: true, message: "床位性别不能为空", trigger: "blur" }],
  // college: [{ required: true, message: "所属学院不能为空", trigger: "blur" }],
  // major: [{ required: true, message: "专业不能为空", trigger: "blur" }],
  // classstr: [{ required: true, message: "所属班级不能为空", trigger: "blur" }],
  number: [{ required: true, message: "床位号不能为空", trigger: "blur" }],
});

// 校区数据
const schoolData = ref([]);
// 楼栋数据
const buildData = ref([]);
// 寝室号数据
const dormitoryData = ref([]);
// 学院
const collegeData = ref([]);
// 专业
const majorData = ref([]);
// 班级
const classstrData = ref([]);
// 年份
const yearData = ref([]);

const schoolList = async () => {
  let res = await https.get("/welcome/api/welcome-build/schoolGroup", "params");
  console.log(res, "校区数据");
  if (res.code == 200) {
    schoolData.value = res.data;
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.message,
      center: true,
    });
  }
};
const buildList = async (flag) => {
  let params = {
    schoolId: searchInput.schoolId,
    accountId: sessionStorage.getItem("accountId"),
  };
  let res = await https.get(
    // "/welcome/api/welcome-build/buildGroup",
    "/welcome/api/welcome-dormitory/saveBedBuildGroup",
    "params",
    params,
  );
  console.log(res, "楼栋数据");
  if (res.code == 200) {
    buildData.value = res.data;
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.message,
      center: true,
    });
  }
};
const dormitoryList = async (flag) => {
  let params = {
    schoolId: searchInput.schoolId,
    buildId: searchInput.buildId,
  };
  let res = await https.get(
    "/welcome/api/welcome-dormitory/dormitoryGroup",
    "params",
    params,
  );
  console.log(res, "寝室号数据");
  if (res.code == 200) {
    dormitoryData.value = res.data;
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.message,
      center: true,
    });
  }
};
const collegeList = async () => {
  collegeData.value = collegeRole.value;
  // let res = await https.get("/welcome/api/welcomeOrg/getColleges", "params");
  // console.log(res, "学院数据");
  // if (res.code == 200) {
  //   collegeData.value = res.data;
  // } else {
  //   ElMessage({
  //     type: "error",
  //     showClose: true,
  //     message: res.message,
  //     center: true,
  //   });
  // }
};
const majorList = async (flag) => {
  let params = {
    collegeId: searchInput.collegeId,
  };
  let res = await https.get(
    "/welcome/api/welcomeOrg/getMajors",
    "params",
    params,
  );
  console.log(res, "专业数据");
  if (res.code == 200) {
    majorData.value = res.data;
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.message,
      center: true,
    });
  }
};
const classstrList = async (flag) => {
  let params = {
    majorId: searchInput.majorId,
  };
  let res = await https.get(
    "/welcome/api/welcomeOrg/getClasss",
    "params",
    params,
  );
  console.log(res, "班级数据");
  if (res.code == 200) {
    classstrData.value = res.data;
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.message,
      center: true,
    });
  }
};
const yearList = async () => {
  let res = await https.get("/welcome/api/systemSetting/yearDatas", "params");
  console.log(res, "年份数据");
  if (res.code == 200) {
    yearData.value = res.data;
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.message,
      center: true,
    });
  }
};

const schoolChange = async (val) => {
  console.log(val);
  searchInput.buildId = null;
  searchInput.dormitoryId = null;
  if (!val) {
    buildData.value = null;
    dormitoryData.value = null;
    return;
  } else {
    buildData.value = null;
    dormitoryData.value = null;
  }
  buildList();
};

const buildChange = async (val) => {
  searchInput.dormitoryId = null;
  if (!val) {
    dormitoryData.value = null;
    return;
  } else {
    dormitoryData.value = null;
  }
  dormitoryList();
};

const collegeChange = async (val) => {
  console.log(val);
  searchInput.majorId = null;
  searchInput.classstrId = null;
  if (!val) {
    majorData.value = null;
    classstrData.value = null;
    return;
  } else {
    majorData.value = null;
    classstrData.value = null;
  }
  majorList();
};

const majorChange = async (val) => {
  console.log(val);
  searchInput.classstrId = null;
  if (!val) {
    classstrData.value = null;
    return;
  } else {
    classstrData.value = null;
  }
  classstrList();
};

// 获取账户列表
const getList = async () => {
  loading.value = true;
  let params = {
    accountId: sessionStorage.getItem("accountId"),
    currentPage: currentPage.value, // 当前页
    pageCount: pageSize.value, // 一页数据条数
    schoolId: searchInput.schoolId,
    buildId: searchInput.buildId,
    dormitoryId: searchInput.dormitoryId,
    collegeId: searchInput.collegeId,
    majorId: searchInput.majorId,
    classstrId: searchInput.classstrId,
    name: searchInput.name,
    yearId: searchInput.yearId,
  };

  let res = await https.get(
    "/welcome/api/welcomeBed/studentAccommodationPage",
    "params",
    params,
  );
  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 () => {
  getList();
}, 300);

const resetBtn = lodash.debounce(async () => {
  searchInput.schoolId = null;
  searchInput.buildId = null;
  buildData.value = [];
  searchInput.dormitoryId = null;
  dormitoryData.value = [];
  searchInput.name = null;
  searchInput.collegeId = null;
  searchInput.majorId = null;
  majorData.value = [];
  searchInput.classstrId = null;
  classstrData.value = [];
  searchInput.yearId=null
  getList();
}, 300);

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

// 表格斑马纹颜色修改
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 buildExportbtn = async () => {
  let params = {
    accountId: sessionStorage.getItem("accountId"),
    schoolId: searchInput.schoolId,
    buildId: searchInput.buildId,
    dormitoryId: searchInput.dormitoryId,
    name: searchInput.name,
    collegeId: searchInput.collegeId,
    majorId: searchInput.majorId,
    classstrId: searchInput.classstrId,
    yearId: searchInput.yearId,
  };
  let res = await https.getBlob(
    "/welcome/api/welcomeBed/studentAccommodationListExport",
    "params",
    params,
  );
  console.log(res, "学生住宿信息");
  let name = `学生住宿信息`;
  var content = res;
  var datas = new Blob([content]);
  var downloadUrl = window.URL.createObjectURL(datas);
  var anchor = document.createElement("a");
  anchor.href = downloadUrl;
  anchor.download = name + ".xlsx";
  anchor.click();
  window.URL.revokeObjectURL(datas);
  ElMessage({
    type: "success",
    showClose: true,
    message: "导出成功",
    center: true,
  });
};

onBeforeMount(() => {
  getList();
  schoolList();
  collegeList();
  yearList()
});
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);
        }
        :deep(.options) {
          display: flex;
          justify-content: center;
          align-items: center;
          .open {
            color: #2697ff;
            cursor: pointer;
            margin: 0 15px 0 0;
          }
          .edit {
            margin: 0 15px 0 0;
            color: rgba(0, 186, 173, 1);
            cursor: pointer;
          }
          .delete {
            color: rgba(212, 48, 48, 1);
            cursor: pointer;
          }
        }
      }

      .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;
      }
    }
  }
}
</style>
