<template>
  <div class="content-box">
    <div class="left">
      <!-- <span class="cameratxt">住宿信息统计</span> -->
      <el-tabs v-model="activeName" class="demo-tabs" @tab-change="handleClick">
        <el-tab-pane label="床位分配统计" name="first"></el-tab-pane>
        <el-tab-pane label="床位入住统计" name="second"></el-tab-pane>
      </el-tabs>
    </div>

    <div class="scroll">
      <div class="middle">
        <div class="filter">
          <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
              clearable
              v-model="searchInput.sex"
              placeholder="请选择性别"
            >
              <el-option label="男" value="男" />
              <el-option label="女" value="女" />
            </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('accommodationStatisticsSetting1')"
            type="primary"
            color="rgba(48, 201, 191, 1)"
            @click="buildExportbtn"
            >导出</el-button
          >
        </div>
      </div>
      <div class="footer" v-loading="loading" v-if="activeName == 'first'">
        <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="college" label="学院名称" />
          <el-table-column align="center" prop="sex" label="性别">
            <template #default="{ row }">
              <span>{{ row.sex }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="total" label="总人数" />
          <el-table-column
            align="center"
            prop="assignedBed"
            label="已分配床位数"
          />
          <el-table-column
            align="center"
            show-overflow-tooltip
            prop="assignedDormitory"
            label="已分配寝室数"
          />
          <el-table-column
            align="center"
            prop="assignedBuild"
            label="已分配楼栋数"
          />
        </el-table>
      </div>
      <div class="footer" v-loading="loading" v-if="activeName == 'second'">
        <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="college" label="学院名称" />
          <el-table-column align="center" prop="sex" label="性别">
            <template #default="{ row }">
              <span>{{ row.sex }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="totalData" label="总人数" />
          <el-table-column align="center" prop="sleeper" label="已入住人数" />
          <el-table-column
            align="center"
            show-overflow-tooltip
            prop="unSleeper"
            label="未入住人数"
          />
          <el-table-column align="center" prop="totalBed" label="总床位数" />
          <el-table-column
            align="center"
            prop="sleepBed"
            label="已入住床位数"
          />
          <el-table-column
            align="center"
            prop="unSleepBed"
            label="未入住床位数"
          />
        </el-table>
      </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 activeName = ref("first");

// 表格数据
const loading = ref(false);
const tableData = reactive({
  list: [{}],
});

const searchInput = reactive({
  sex: "",
  collegeId: "",
}); // 搜索按钮数据

// 学院
const collegeData = ref([]);

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 getList = async () => {
  loading.value = true;
  let params = {
    collegeId: searchInput.collegeId,
    sex: searchInput.sex,
  };
  let res = null;
  if (activeName.value == "first") {
    res = await https.get(
      "/welcome/api/census/queryBedTotal",
      "params",
      params
    );
  } else if (activeName.value == "second") {
    res = await https.get(
      "/welcome/api/census/queryCheckTotal",
      "params",
      params
    );
  }

  console.log(res, "住宿信息统计");
  if (res.code == 200) {
    tableData.list = res.data;
    loading.value = false;
  } else {
    loading.value = false;
    ElMessage({
      type: "error",
      showClose: true,
      message: res.message,
      center: true,
    });
  }
};
const handleClick = (val) => {
  activeName.value = val;
  searchInput.sex = null;
  searchInput.collegeId = null;
  getList();
};
// 搜索功能
const searchBtn = lodash.debounce(async () => {
  getList();
}, 300);
const resetBtn = lodash.debounce(async () => {
  searchInput.sex = null;
  searchInput.collegeId = 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 buildExportbtn = async () => {
  let params = {
    sex: searchInput.sex,
    collegeId: searchInput.collegeId,
  };
  let res = null;
  let name = null;
  if (activeName.value == "first") {
    res = await https.getBlob(
      "/welcome/api/census/bedTotalExport",
      "params",
      params
    );
    name = `床位分配统计`;
  } else if (activeName.value == "second") {
    res = await https.getBlob(
      "/welcome/api/census/checkTotalExport",
      "params",
      params
    );
    name = `床位入住统计`;
  }
  console.log(res, "床位信息管理导出");

  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();
  collegeList();
});
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;
    :deep(.el-tabs) {
      .el-tabs__header {
        margin: 0;
      }
      .el-tabs__nav-wrap {
        &::after {
          display: none;
        }
        .el-tabs__item {
          color: #000;
          font-size: 18px;
          font-weight: 600;
        }
      }
    }
  }
  .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;
          .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>
