<template>
  <div class="p-2">
    <el-card shadow="hover">
      <template #header>
        <el-tabs v-model="activeTab" @tab-change="handleTabChange">
          <el-tab-pane label="基本设置" name="basic" />
          <el-tab-pane label="登录设置" name="login" />
        </el-tabs>
        <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="configList" border>
        <el-table-column label="配置名称" align="center" prop="name" :show-overflow-tooltip="true" />
        <el-table-column label="配置键" align="center" prop="configKey" :show-overflow-tooltip="true" />
        <el-table-column label="配置值" align="center" prop="value" :show-overflow-tooltip="true" />
        <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="500px" append-to-body>
      <el-form ref="configFormRef" :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-item label="配置键" prop="configKey">
          <el-input v-model="form.configKey" placeholder="请输入配置键" />
        </el-form-item>
        <el-form-item label="配置值" prop="value">
          <el-input v-model="form.value" type="textarea" :rows="3" placeholder="请输入配置值" />
        </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="PlatformConfig" lang="ts">
import { listPlatformConfig, getPlatformConfig, addPlatformConfig, updatePlatformConfig, delPlatformConfig } from '@/api/system/platformConfig';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const configList = ref<any[]>([]);
const loading = ref(false);
const total = ref(0);
const activeTab = ref('basic');

// 配置类型映射：basic=1, login=2
const configTypeMap: Record<string, number> = {
  basic: 1,
  login: 2
};

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

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

const configFormRef = ref<ElFormInstance>();

const initFormData = {
  id: undefined as number | undefined,
  configType: 1,
  configKey: '',
  name: '',
  value: ''
};

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

const rules = reactive({
  name: [{ required: true, message: '配置名称不能为空', trigger: 'blur' }],
  configKey: [{ required: true, message: '配置键不能为空', trigger: 'blur' }],
  value: [{ required: true, message: '配置值不能为空', trigger: 'blur' }]
});

/** Tab 切换 */
const handleTabChange = (tabName: string) => {
  queryParams.value.configType = configTypeMap[tabName] || 1;
  queryParams.value.pageNum = 1;
  getList();
};

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

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

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData, configType: queryParams.value.configType };
  configFormRef.value?.resetFields();
};

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

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

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

/** 删除按钮操作 */
const handleDelete = async (row: any) => {
  await proxy?.$modal.confirm('是否确认删除配置"' + row.name + '"?');
  await delPlatformConfig(row.id);
  proxy?.$modal.msgSuccess('删除成功');
  getList();
};

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