<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 icon="Refresh" circle @click="getList" />
          </div>
        </div>
      </template>

      <el-table v-loading="loading" :data="termsList" border>
        <el-table-column label="编号" align="center" prop="sort" width="100" />
        <el-table-column label="事项类型" align="center" prop="type" />
        <el-table-column label="操作" align="center" width="100">
          <template #default="scope">
            <el-button link type="primary" @click="handleUpdate(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="termsFormRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="事项类型" prop="type">
          <el-input v-model="form.type" 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">
          <editor v-model="form.content" :min-height="300" />
        </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="Terms" lang="ts">
import { listTerms, getTerms, addTerms, updateTerms } from '@/api/system/terms';
import { ComponentInternalInstance, getCurrentInstance, ref, reactive, onMounted } from 'vue';
import type { FormInstance } from 'element-plus';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

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

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

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

const termsFormRef = ref<FormInstance>();

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

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

const rules = reactive({
  type: [{ required: true, message: '事项类型不能为空', trigger: 'blur' }],
  content: [{ required: true, message: '内容不能为空', trigger: 'blur' }]
});

const getList = async () => {
  loading.value = true;
  try {
    const res = await listTerms(queryParams.value);
    termsList.value = res.rows;
    total.value = res.total;
  } finally {
    loading.value = false;
  }
};

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

const reset = () => {
  form.value = { ...initFormData };
  termsFormRef.value?.resetFields();
};

const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加事项与条款';
};

const handleUpdate = async (row: any) => {
  reset();
  const res = await getTerms(row.id);
  form.value = res.data;
  dialog.visible = true;
  dialog.title = '修改事项与条款';
};

const submitForm = async () => {
  await termsFormRef.value?.validate();
  if (form.value.id) {
    await updateTerms(form.value);
    proxy?.$modal.msgSuccess('修改成功');
  } else {
    await addTerms(form.value);
    proxy?.$modal.msgSuccess('新增成功');
  }
  dialog.visible = false;
  getList();
};

onMounted(() => {
  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>
