<template>
  <div class="p-2">
    <el-card shadow="hover">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
          </el-col>
          <right-toolbar @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="aboutList" border>
        <el-table-column label="标题" align="center" prop="title" :show-overflow-tooltip="true" />
        <el-table-column label="排序" align="center" prop="sort" width="80" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
        <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button link type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        :total="total"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改对话框 -->
    <el-dialog v-model="dialog.visible" :title="dialog.title" width="800px" append-to-body>
      <el-form ref="aboutFormRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input
            v-model="form.content"
            type="textarea"
            :rows="10"
            placeholder="请输入内容（支持HTML）"
          />
        </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="About" lang="ts">
import { listAbout, getAbout, addAbout, updateAbout, delAbout } from '@/api/system/about';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const aboutList = ref<any[]>([]);
const loading = ref(false);
const total = ref(0);

const queryParams = ref({
  pageNum: 1,
  pageSize: 10
});

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const aboutFormRef = ref<ElFormInstance>();

const initFormData = {
  id: undefined as number | undefined,
  title: '',
  content: '',
  sort: 0
};

const form = ref({ ...initFormData });

const rules = reactive({
  title: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
  content: [{ required: true, message: '内容不能为空', trigger: 'blur' }]
});

/** 查询列表 */
const getList = async () => {
  loading.value = true;
  try {
    const res = await listAbout(queryParams.value);
    aboutList.value = res.rows;
    total.value = res.total;
  } finally {
    loading.value = false;
  }
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  aboutFormRef.value?.resetFields();
};

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加关于我们';
};

/** 修改按钮操作 */
const handleUpdate = async (row: any) => {
  reset();
  const res = await getAbout(row.id);
  form.value = res.data;
  dialog.visible = true;
  dialog.title = '修改关于我们';
};

/** 提交按钮 */
const submitForm = () => {
  aboutFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      if (form.value.id) {
        await updateAbout(form.value);
        proxy?.$modal.msgSuccess('修改成功');
      } else {
        await addAbout(form.value);
        proxy?.$modal.msgSuccess('新增成功');
      }
      dialog.visible = false;
      getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row: any) => {
  await proxy?.$modal.confirm('是否确认删除标题为"' + row.title + '"的数据项?');
  await delAbout(row.id);
  proxy?.$modal.msgSuccess('删除成功');
  getList();
};

onMounted(() => {
  getList();
});
</script>
