<template>
  <div class="scroll">
    <div class="middle">
      <div class="filter">
        <div class="condition">
          <span>分类名称 :</span>
          <el-select
            clearable
            v-model="searchInput.year"
            placeholder="请选择分类名称"
          >
            <el-option
              v-for="i in yearData"
              :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-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 class="gongneng">
        <el-button
          type="primary"
          style="margin-left: 0"
          color="rgba(38, 151, 255, 1)"
          @click="addlist"
          >创建分类</el-button
        >
      </div>
    </div>
    <div class="footer" v-loading="loading">
      <div class="card_all">
        <el-space wrap>
          <el-card
            style="max-width: 480px"
            v-for="i in tableData.list"
            :key="i.id"
          >
            <template #header>
              <div class="card-header">
                <span>{{ i.name }}</span>
                <span><span class="number">1 </span>篇新闻</span>
              </div>
            </template>
            <p>创建时间: {{ i.createTime }}</p>
            <p>创建人: {{ i.createUser }}</p>
            <template #footer>
              <div class="card-footer">
                <el-button type="primary" @click="updateS(i)" link>编辑</el-button>
                <span class="line">|</span>
                <el-button type="danger" @click="deleteS(i)" link>删除</el-button>
              </div>
            </template>
          </el-card>
        </el-space>
      </div>
      <!-- 分页组件 -->
      <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="450"
    :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="name">
        <el-input
          clearable
          v-model.trim="ruleForm.name"
          class="w-50 m-2"
          placeholder="请输入分类名称"
        />
      </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>
</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 {
  queryCategoryNewsPage,
  insertCategoryNews,
  updateCategoryNews,
  deleteCategoryNews,
  queryCategoryNewss,
  queryAlumniNewsPage,
  insertAlumniNews,
  updateAlumniNews,
  deleteAlumniNews,
} from "@/api/alumni-new.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({
  name: "",
  year: null,
  college: null,
  major: null,
  class: null,
}); // 搜索按钮数据

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({
  name: "",
  id: "",
});
// 表单验证
const rules = reactive({
  name: [{ required: true, message: "分类名称不能为空", trigger: "blur" }],
});

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

// 搜索功能
const searchBtn = lodash.debounce(async () => {}, 300);
const resetBtn = lodash.debounce(async () => {}, 300);

// 添加账号
const addlist = () => {
  dialongTitle.value = "创建分类";
  addDialogVisible.value = true;
  ruleForm.name = null;
};
// 添加账号
const updateS = (row) => {
  console.log(row);
  dialongTitle.value = "编辑分类";
  addDialogVisible.value = true;
  ruleForm.name = row.name;
  ruleForm.id = row.id;
};
const deleteS = async (row) => {
  ElMessageBox.confirm("是否删除此数据?", "提示!!!", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      let params = {
        id: row.id,
      };
      let res = await deleteCategoryNews(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 = {
        name: ruleForm.name,
      };
      let res = "";
      if (dialongTitle.value == "创建分类") {
        res = await insertCategoryNews(data);
      } else {
        data.id = ruleForm.id;
        res = await updateCategoryNews(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;
};
// 分页
const handleCurrentChange = (value) => {
  // console.log(value);
  currentPage.value = value;
};

const cancelProjectImport = () => {
  buildImportVisible.value = false;
  if (buildRef.value) {
    buildRef.value.clearFiles();
    buildFile.value = null;
  }
};

onBeforeMount(() => {
  getList();
});
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;

    .card_all {
      height: calc(100% - 60px);
      overflow: auto;
      // display: flex;

      .el-card {
        width: 360px !important;
        height: 184px;
      }
      :deep(.el-card__header) {
        padding: 10px 20px 0;
        border: 0;
        font-size: 20px;
        .card-header {
          display: flex;
          justify-content: space-between;
          align-items: center;
          font-weight: 800;
          .number {
            font-size: 26px;
          }
        }
      }
      :deep(.el-card__body) {
        padding: 0 20px 10px;
        font-size: 13px;
      }
      :deep(.el-card__footer) {
        padding: 10px 20px;
        .card-footer {
          display: flex;
          justify-content: space-around;
          .line {
            color: #ebeaea;
          }
        }
      }
    }
    /* WebKit 内核浏览器（Chrome、Edge、Safari）的滚动条样式 */
    .card_all::-webkit-scrollbar {
      /* 滚动条宽度 */
      width: 6px;
      height: 6px;
    }

    /* 滚动条轨道 */
    .card_all::-webkit-scrollbar-track {
      background: #f5f7fa;
      border-radius: 3px;
    }

    /* 滚动条滑块 */
    .card_all::-webkit-scrollbar-thumb {
      background: #c0c4cc;
      border-radius: 3px;
      /* 鼠标悬停时滑块变色 */
      transition: background 0.3s;
    }

    .card_all::-webkit-scrollbar-thumb:hover {
      background: #909399;
    }

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

// 添加员工弹窗样式
.addStaff {
  .options {
    // border: 1px solid red;
    margin-top: 40px;
  }
}
</style>
