<template>
  <div class="p-2">
    <el-card shadow="hover">
      <template #header>
        <div class="card-header">
          <span class="title">关于我们信息列表</span>
          <div class="header-actions">
            <el-button type="primary" icon="Plus" @click="handleAdd">新增</el-button>
            <el-button type="success" icon="Link" @click="handleGenerateLink">生成链接</el-button>
            <el-button icon="Refresh" circle @click="getList" />
          </div>
        </div>
      </template>

      <el-table v-loading="loading" :data="dataList" border>
        <el-table-column label="标题" prop="title" align="center" />
        <el-table-column label="操作" width="150" align="center">
          <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" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>

    <!-- 添加或修改对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="800px" append-to-body>
      <el-form ref="formRef" :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="content">
          <editor v-model="form.content" :min-height="300" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" :min="0" />
        </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="AboutUs" lang="ts">
import { listAbout, getAbout, addAbout, updateAbout, delAbout } from '@/api/system/about';
import { ComponentInternalInstance, getCurrentInstance, ref, reactive, toRefs } from 'vue';
import type { FormInstance } from 'element-plus';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const dataList = ref([]);
const loading = ref(true);
const total = ref(0);

const formRef = ref<FormInstance>();

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

const data = reactive({
  form: {} as any,
  queryParams: {
    pageNum: 1,
    pageSize: 10
  },
  rules: {
    title: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
    content: [{ required: true, message: '内容不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

const getList = async () => {
  loading.value = true;
  const res = await listAbout(queryParams.value);
  dataList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

const reset = () => {
  form.value = {
    id: undefined,
    title: undefined,
    content: undefined,
    sort: 0
  };
  formRef.value?.resetFields();
};

const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加关于我们';
};

const handleUpdate = async (row: any) => {
  reset();
  const res = await getAbout(row.id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改关于我们';
};

const submitForm = async () => {
  await formRef.value?.validate();
  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('是否确认删除？');
  await delAbout(row.id);
  proxy?.$modal.msgSuccess('删除成功');
  getList();
};

const handleGenerateLink = () => {
  proxy?.$modal.msgSuccess('链接已生成');
};

const cancel = () => {
  dialog.visible = false;
  reset();
};

getList();
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.card-header .title {
  font-size: 16px;
  font-weight: bold;
}
.header-actions {
  display: flex;
  gap: 10px;
}
</style>
