<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-row>
    <!-- 表格 -->
    <Table :xbTable="xbTable" @tablePageChange="handleTablePageChange">
      <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 label="学校名称" prop="name">
          <el-input
            v-model="form.name"
            placeholder="请输入"
            clearable
            maxlength="50"
          />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number
            v-model="form.sort"
            :min="1"
            :precision="0"
            controls-position="right"
          />
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入"
            clearable
            maxlength="200"
          />
        </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="schoolManage">
import Filter from "@/views/smartCampus/components/Filter/index.vue";
import Table from "@/views/smartCampus/components/Table/index.vue";
import { ElMessageBox } from "element-plus";
import {
  getSchoolListApi,
  addSchoolApi,
  updateSchoolApi,
  deleteSchoolApi,
} from "@/api/smartCampus/index";
const { proxy } = getCurrentInstance();
const selectConfig = ref({
  statusOption: [
    { label: "正常", value: "0" },
    { label: "停用", value: "1" },
  ],
});
const filterData = ref({}); //过滤
const filterConfig = computed(() => {
  return [
    {
      comName: "admin",
      label: "学校名称",
      prop: "id",
      placeholder: "请选择",
      clearable: true,
    },
  ];
});
const title = ref(""); //弹窗标题
const open = ref(false); //弹窗开关

// 弹窗数据
const data = reactive({
  form: {},
  rules: {
    name: [{ required: true, message: "学校名称不能为空", trigger: "blur" }],
    sort: [{ required: true, message: "排序不能为空", trigger: "change" }],
  },
});
const { form, rules } = toRefs(data);

const xbTable = ref({
  loading: false,
  columns: [
    { prop: "name", label: "学校名称" },
    { prop: "sort", label: "排序" },
    { prop: "remark", label: "备注" },
    { label: "操作", slot: "operation" },
  ],
  tableData: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
  },
});
/** 查询列表 */
function getList() {
  xbTable.value.loading = true;
  getSchoolListApi({ ...xbTable.value.pagination, ...filterData.value }).then(
    (res) => {
      xbTable.value.tableData = res.rows;
      xbTable.value.loading = false;
      xbTable.value.pagination.total = res.total;
    }
  );
}

// 过滤按钮
function handleFilterData(type) {
  switch (type.value) {
    case "search":
      getList();
      break;
    case "reset":
      xbTable.value.pagination.pageNum = 1;
      getList();
      break;
  }
}

/** 表单重置 */
function reset() {
  form.value = {
    name: undefined,
  };
  proxy.resetForm("diaRef");
}
// 添加按钮
function handleAdd() {
  reset();
  title.value = "学校管理/添加";
  open.value = true;
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

// 表格分页
function handleTablePageChange(val) {
  xbTable.value.pagination.pageSize = val.limit;
  xbTable.value.pagination.pageNum = val.page;
  getList();
}
/** 修改按钮操作 */
function handleUpdate(row) {
  if (row.hasUser) {
    ElMessageBox.alert("该学校已关联用户，不可编辑", "", {
      confirmButtonText: "确定",
    });
    return;
  }
  reset();
  title.value = "学校管理/编辑";
  form.value = { ...row };
  open.value = true;
}

/** 删除按钮操作 */
function handleDelete(row) {
  const deleteId = row.id;
  if (row.hasUser) {
    ElMessageBox.alert("该学校已关联用户，不可删除", "", {
      confirmButtonText: "确定",
    });
    return;
  }
  proxy.$modal
    .confirm("是否确认删除？")
    .then(() => {
      xbTable.value.loading = true;
      return deleteSchoolApi(deleteId);
    })
    .then(() => {
      xbTable.value.loading = false;
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .finally(() => {
      xbTable.value.loading = false;
    });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["diaRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        updateSchoolApi(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addSchoolApi(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
getList();
</script>
<style lang="scss" scoped></style>
