<template>
  <div class="scroll">
    <div class="middle">
      <div class="filter">
        <div class="condition">
          <span>报名人 :</span>
          <el-input
            clearable
            v-model.trim="searchInput.userName"
            class="w-50 m-2"
            placeholder="请输入发布人"
            style="width: 180px"
          />
        </div>
        <div class="condition">
          <span>活动名称 :</span>
          <el-input
            clearable
            v-model.trim="searchInput.theme"
            class="w-50 m-2"
            placeholder="请输入活动名称"
            style="width: 180px"
          />
        </div>
        <div class="condition">
          <span>所属组织 :</span>
          <!-- <el-select
            clearable
            v-model="searchInput.orgId"
            placeholder="请选择所属组织"
          >
            <el-option
              v-for="i in clubData"
              :key="i.id"
              :label="i.name"
              :value="i.id"
            />
          </el-select> -->
          <el-select
            v-model="searchInput.orgId"
            filterable
            clearable
            remote
            reserve-keyword
            placeholder="请搜索或选择所属组织"
            :remote-method="remoteMethod"
            :loading="clubLoading"
          >
            <el-option
              v-for="i in clubData"
              :key="i.id"
              :label="i.name"
              :value="i.id"
            />
          </el-select>
        </div>
        <div class="condition">
          <span>审核状态 :</span>
          <el-select
            clearable
            v-model="searchInput.isPass"
            placeholder="请选择审核状态"
          >
            <el-option
              v-for="i in categoryData"
              :key="i.id"
              :label="i.name"
              :value="i.id"
            />
          </el-select>
        </div>
        <div class="condition">
          <span>审核时间 :</span>
          <el-date-picker
            v-model="searchInput.createTime"
            unlink-panels
            type="datetimerange"
            range-separator="-"
            start-placeholder="起始时间"
            end-placeholder="结束时间"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择日期"
          />
        </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>
    <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 type="index" align="center" label="序号" width="60" />
        <el-table-column align="center" prop="userName" label="报名人" />
        <el-table-column align="center" prop="phone" label="联系电话" />
        <el-table-column align="center" prop="theme" label="活动名称" />
        <el-table-column align="center" prop="timeStr" label="活动时间">
          <template #default="{ row }">
            <span>{{ row.startTime }} ~ {{ row.endTime }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="school" label="报名截止时间">
          <template #default="{ row }">
            <span>{{ row.signsTime }} ~ {{ row.signeTime }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="orgName" label="所属组织" />
        <el-table-column align="center" prop="address" label="活动地点" />
        <el-table-column align="center" prop="createTime" label="报名时间" />
        <!-- <el-table-column width="100" align="center" label="组织城市">
          <template #default="{ row }">
            <span v-if="row.retentionState == 2">是</span>
            <span v-if="row.retentionState == 1">否</span>
          </template>
        </el-table-column> -->
        <el-table-column align="center" prop="statusName" label="审核状态" />
        <el-table-column align="center" prop="applyUserAdmin" label="审核人" />
        <el-table-column align="center" prop="passTime" label="审核时间" />
        <el-table-column align="center" label="操作" fixed="right" width="200">
          <template #default="{ row }">
            <el-button type="primary" @click="infoClick(row)" link
              >详情</el-button
            >
            <el-button
              type="primary"
              v-if="row.passName == '待审核'"
              @click="operationPassChange(row)"
              link
              >通过</el-button
            >
            <el-button
              type="danger"
              v-if="row.passName == '待审核'"
              @click="operationRefuseChange(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="540"
    :before-close="cancelAdd"
    destroy-on-close
    draggable
  >
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-width="90px"
      class="demo-ruleForm"
      :size="formSize"
      label-position="right"
      status-icon
    >
      <el-form-item
        label="审批意见 :"
        :prop="dialongTitle == '拒绝审批' ? 'passValue' : ''"
      >
        <el-input
          v-model.trim="ruleForm.passValue"
          placeholder="请输入审批意见"
          clearable
          :autosize="{ minRows: 4 }"
          type="textarea"
        />
      </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="infoVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    title="详情"
    align-center
    width="600"
    :before-close="cancelInfo"
    draggable
  >
    <div style="margin-bottom: 15px">
      <el-descriptions :column="1">
        <el-descriptions-item label="报名人 :">
          {{ infoData.userName }}
        </el-descriptions-item>
        <el-descriptions-item label="联系电话 :">
          {{ infoData.phone }}
        </el-descriptions-item>
        <el-descriptions-item label="活动名称 :">
          {{ infoData.theme }}
        </el-descriptions-item>
        <el-descriptions-item label="活动时间 :">
          {{ infoData.startTime }}~{{ infoData.endTime }}
        </el-descriptions-item>
        <el-descriptions-item label="报名截止时间 :">
          {{ infoData.signsTime }}~{{ infoData.signeTime }}
        </el-descriptions-item>
        <el-descriptions-item label="所属组织 :">
          {{ infoData.orgName }}
        </el-descriptions-item>
        <el-descriptions-item label="活动地点 :">
          {{ infoData.address }}
        </el-descriptions-item>
        <el-descriptions-item label="报名时间 :">
          {{ infoData.createTime }}
        </el-descriptions-item>
        <el-descriptions-item label="审核状态 :">
          {{ infoData.statusName }}
        </el-descriptions-item>
        <el-descriptions-item label="审核人 :">
          {{ infoData.applyUserAdmin }}
        </el-descriptions-item>
        <el-descriptions-item label="审核时间 :">
          {{ infoData.passTime }}
        </el-descriptions-item>
      </el-descriptions>
    </div>
  </el-dialog>
</template>

<script setup>
import {
  ref,
  watch,
  reactive,
  nextTick,
  onBeforeMount,
  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 {
  queryPassTypes,
  queryReportPages,
  toExamineReport,
  queryClubDatas,
} from "@/api/alumni-activity.js";

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

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

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

const searchInput = reactive({
  userName: "",
  theme: "",
  orgId: "",
  isPass: "",
  createTime: "",
}); // 搜索按钮数据

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

const addDialogVisible = ref(false); // 控制添加账号弹窗
const categoryData = ref();
const clubData = ref();
const clubLoading = ref(false); // 加载状态
const pageParams = reactive({
  pageNum: 1, // 当前页
  pageSize: 500, // 每页条数
  keyword: "", // 搜索关键词（可选）
});

// 表单数据
const formSize = ref("default");
const ruleFormRef = ref();
const ruleForm = reactive({
  passValue: "",
  id: "",
});
// 表单验证
const rules = reactive({
  passValue: [{ required: true, message: "审批意见不能为空", trigger: "blur" }],
});

const infoVisible = ref(false);
const infoData = ref({});

// 获取账户列表
const getList = async () => {
  loading.value = true;
  let params = {
    currentPage: currentPage.value, // 当前页
    pageCount: pageSize.value, // 一页数据条数
    userName: searchInput.userName,
    theme: searchInput.theme,
    orgId: searchInput.orgId,
    isPass: searchInput.isPass,
  };
  if (searchInput.createTime) {
    params.startTime = searchInput.createTime[0];
    params.endTime = searchInput.createTime[1];
  }
  queryReportPages(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.userName = "";
  searchInput.theme = "";
  searchInput.orgId = "";
  searchInput.createTime = null;
  searchInput.isPass = null;
  currentPage.value = 1;
  getList();
}, 300);

const deleteS = async (row) => {
  ElMessageBox.confirm("是否删除此数据?", "提示!!!", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      loading.value = true;
      let data = {
        dormitoryId: row.id, // 当前页
      };
    })
    .catch(() => {
      loading.value = false;
    });
};
// 通过
const operationPassChange = async (row) => {
  dialongTitle.value = "通过审批";
  addDialogVisible.value = true;
  ruleForm.passValue = null;
  ruleForm.id = row.id;
};
// 拒绝
const operationRefuseChange = async (row) => {
  dialongTitle.value = "拒绝审批";
  addDialogVisible.value = true;
  ruleForm.passValue = null;
  ruleForm.id = row.id;
};

// 确认添加员工
const submitAdd = lodash.debounce(async (formEl) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      let data = {
        id: ruleForm.id,
        isPass: dialongTitle.value == "通过审批" ? 2 : 3,
        passValue: ruleForm.passValue,
      };
      let res = await toExamineReport(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 infoClick = (row) => {
  console.log(row);

  infoVisible.value = true;
  infoData.value = row;
};

const cancelInfo = () => {
  infoVisible.value = false;
};

const categoryList = async () => {
  let res = await queryPassTypes();
  console.log(res, "获取审核状态下拉列表数据");
  if (res.code == 200) {
    categoryData.value = res.data;
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.message,
      center: true,
    });
  }
};
// 获取组织下拉列表数据
const clubList = async () => {
  let params = {
    currentPage: pageParams.pageNum,
    pageCount: pageParams.pageSize,
    keyword: pageParams.keyword,
  };
  let res = await queryClubDatas(params);
  console.log(res, "获取组织下拉列表数据");
  if (res.code == 200) {
    clubData.value = res.data.list;
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.message,
      center: true,
    });
  }
};
const remoteMethod = (query) => {
  console.log(query);
  pageParams.keyword = query;
  clubLoading.value = true;
  clubList().then(() =>{
    clubLoading.value = false;
  })
};

onBeforeMount(() => {
  getList();
  categoryList();
  clubList();
});
onUnmounted(() => {
  // document.removeEventListener("keyup", Enters);
});
</script>

<style scoped lang="scss">
.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: #2697ff;
          cursor: pointer;
        }
        .delete {
          color: #f56c6c;
          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-textarea {
      width: 400px;
    }
    .el-select {
      width: 400px;
    }
    .el-tree {
      width: 400px;
    }
  }
}
</style>
