<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="plus"
          size="default"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <!-- <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar> -->
    </el-row>
    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="编号" prop="id" align="center" />
      <el-table-column label="导航名称" prop="name" align="center" />
      <el-table-column label="操作" align="center">
        <template #default="{ row }">
          <el-button
            round
            type="primary"
            size="small"
            @click="handleUpdate(row)"
            >修改</el-button
          >
          <el-button round type="danger" size="small" @click="handleDelete(row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" v-model="showEditDialog" width="500px">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="导航名称:" prop="name">
          <el-input v-model="form.name" placeholder="请输入导航名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup name="PlanCategory">
import { reactive, ref } from "vue";
import {
  planCategoryList,
  planCategoryAdd,
  planCategoryDelete,
  planCategoryDetail,
  planCategoryUpdate,
} from "@/api/website/solution";
import { ElMessage, ElMessageBox } from "element-plus";

const loading = ref(true);
const dataList = ref([]);
// const total = ref(0);
const data = reactive({
  form: {},
  rules: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
});

const { queryParams, form, rules } = toRefs(data);
// 获取服务列表
const getList = async () => {
  loading.value = true;
  const { data } = await planCategoryList(queryParams.value);
  dataList.value = data;
  //   total.value = resp.total;
  loading.value = false;
};
getList();

const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

const formRef = ref();
const title = ref("");
const showEditDialog = ref(false);
// 表单重置
const reset = () => {
  form.value = {};
  if (formRef.value) {
    formRef.value.resetFields();
  }
};
// 取消按钮
const cancel = () => {
  showEditDialog.value = false;
  reset();
};
const handleAdd = () => {
  reset();
  showEditDialog.value = true;
  title.value = "新增导航";
};

const handleUpdate = async (row) => {
  reset();
  const resp = await planCategoryDetail(row.id);
  form.value = resp.data;
  showEditDialog.value = true;
  title.value = "修改导航";
};

/** 删除按钮操作 */
const handleDelete = (row) => {
  ElMessageBox.confirm(`是否确认删除名为${row.name}的数据项？`)
    .then(async () => {
      await planCategoryDelete([row.id]);
      getList();
      ElMessage.success("删除成功");
    })
    .catch(() => {});
};

const submitForm = async () => {
  await formRef.value.validate();
  if (form.value.id != undefined) {
    planCategoryUpdate(form.value).then((response) => {
      ElMessage.success("修改成功");
      showEditDialog.value = false;
      getList();
    });
  } else {
    planCategoryAdd(form.value).then((response) => {
      ElMessage.success("新增成功");
      showEditDialog.value = false;
      getList();
    });
  }
};
</script>
