<template>
  <div class="app-container">
    <Filter
      v-model="filterData"
      :config="filterConfig"
      @handleAction="handleFilterData"
    />
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          >添加</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          @click="handleDelete"
          :disabled="multiple"
          >批量删除</el-button
        >
      </el-col>
    </el-row>
    <!-- 表格 -->
    <Table
      :xbTable="xbTable"
      @tablePageChange="handleTablePageChange"
      @selectionChange="handleSelectionChange"
    >
      <template v-slot:operation="scope">
        <el-button
          link
          type="primary"
          icon="Edit"
          @click="handleUpdate(scope.row)"
          >编辑</el-button
        >
        <el-button
          link
          type="danger"
          icon="Delete"
          @click="handleDelete(scope.row)"
          >删除</el-button
        >
      </template>
    </Table>
    <!-- 修改对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      width="600px"
      append-to-body
      :before-close="cancel"
    >
      <el-form ref="diaRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item
          v-if="useUserStore().showAllTenant"
          label="学校名称"
          prop="tenantId"
        >
          <SchoolSelect v-model="form.tenantId" />
        </el-form-item>
        <el-form-item label="年级名称" prop="gradeName">
          <el-input
            v-model="form.gradeName"
            placeholder="请输入"
            maxlength="50"
          />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" placeholder="请输入" maxlength="50" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number
            v-model="form.sort"
            :min="0"
            :precision="0"
            controls-position="right"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="gradeManage">
import Filter from "@/views/smartCampus/components/Filter/index.vue";
import Table from "@/views/smartCampus/components/Table/index.vue";
import {
  getGradeListApi,
  addGradeApi,
  updateGradeApi,
  deleteGradeApi,
  gradeDetailsApi,
} from "@/api/smartCampus/index";
import useUserStore from "@/store/modules/user";
const { proxy } = getCurrentInstance();
const title = ref(""); //弹窗title
const open = ref(false);
const filterData = ref({}); //过滤
const filterConfig = computed(() => {
  return [
    {
      comName: "admin",
      label: "学校名称",
      prop: "tenantId",
      placeholder: "请选择",
      clearable: true,
    },
  ];
});
const ids = ref([]); //批量删除ids
const multiple = ref(true); //批量删除按钮
const xbTable = ref({
  loading: false,
  columns: [
    { type: "selection" },
    { prop: "gradeName", label: "年级名称" },
    { prop: "sort", label: "排序" },
    { prop: "remark", label: "备注" },
    { label: "操作", slot: "operation" },
  ],
  tableData: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
  },
});
const data = reactive({
  form: {},
  rules: {
    tenantId: [
      { required: true, message: "学校名称不能为空", trigger: "change" },
    ],
    gradeName: [
      { required: true, message: "年级名称不能为空", trigger: "blur" },
    ],
    sort: [{ required: true, message: "排序不能为空", trigger: "blur" }],
  },
});

const { form, rules } = toRefs(data);
// 过滤按钮
function handleFilterData(type) {
  switch (type.value) {
    case "search":
      getList();
      break;
    case "reset":
      xbTable.value.pagination.pageNum = 1;
      getList();
      break;
  }
}
/** 查询列表 */
function getList() {
  xbTable.value.loading = true;
  getGradeListApi({ ...xbTable.value.pagination, ...filterData.value }).then(
    (res) => {
      xbTable.value.tableData = res.rows;
      xbTable.value.loading = false;
      xbTable.value.pagination.total = res.total;
    }
  );
}
// 表格勾选
function handleSelectionChange(val) {
  ids.value = val.map((item) => item.id);
  multiple.value = !val.length;
}
// 表格分页
function handleTablePageChange(val) {
  xbTable.value.pagination.pageSize = val.limit;
  xbTable.value.pagination.pageNum = val.page;
  getList();
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  title.value = "年级管理/添加";
  open.value = true;
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 删除按钮操作 */
function handleDelete(row) {
  const deleteId = row.id || ids.value;
  proxy.$modal
    .confirm("是否确认删除？")
    .then(() => {
      xbTable.value.loading = true;
      return deleteGradeApi(deleteId);
    })
    .then(() => {
      xbTable.value.loading = false;
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .finally(() => {
      xbTable.value.loading = false;
    });
}
/** 表单重置 */
function reset() {
  form.value = {
    gradeName: undefined,
    remark: undefined,
    sort: 0,
  };
  proxy.resetForm("diaRef");
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  title.value = "年级管理/编辑";
  gradeDetailsApi(row.id).then((res) => {
    form.value = res.data;
    open.value = true;
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["diaRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        updateGradeApi(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addGradeApi(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
// 超管
function handleAdminRole() {
  if (useUserStore().showAllTenant) {
    xbTable.value.columns.splice(1, 0, {
      prop: "tenantName",
      label: "学校名称",
    });
  }
}
handleAdminRole();
getList();
</script>
