<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" v-hasPermi="['system:platformConfig:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['system:platformConfig:remove']">删除</el-button>
          </el-col>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="配置键" prop="configKey" />
        <el-table-column label="配置名称" prop="name" />
        <el-table-column label="配置值" prop="value" show-overflow-tooltip />
        <el-table-column label="操作" width="150" align="center">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:platformConfig:edit']">修改</el-button>
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:platformConfig:remove']">删除</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="500px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="配置键" prop="configKey">
          <el-input v-model="form.configKey" placeholder="请输入配置键" />
        </el-form-item>
        <el-form-item label="配置名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入配置名称" />
        </el-form-item>
        <el-form-item label="配置值" prop="value">
          <el-input v-model="form.value" type="textarea" 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="LoginSetting" lang="ts">
import { listPlatformConfig, getPlatformConfig, addPlatformConfig, updatePlatformConfig, delPlatformConfig } from '@/api/system/platformConfig';
import { ComponentInternalInstance, getCurrentInstance, ref, reactive, toRefs } from 'vue';
import { ElForm } from 'element-plus';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const dataList = ref([]);
const loading = ref(true);
const ids = ref<Array<number | string>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const formRef = ref(ElForm);

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

const data = reactive({
  form: {} as any,
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    configType: '1'
  },
  rules: {
    configKey: [{ required: true, message: '配置键不能为空', trigger: 'blur' }],
    name: [{ required: true, message: '配置名称不能为空', trigger: 'blur' }],
    value: [{ required: true, message: '配置值不能为空', trigger: 'blur' }]
  }
});

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

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

const handleSelectionChange = (selection: any[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
};

const reset = () => {
  form.value = {
    id: undefined,
    configType: '1',
    configKey: undefined,
    name: undefined,
    value: undefined
  };
  formRef.value?.resetFields();
};

const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加登录配置';
};

const handleUpdate = async (row: any) => {
  reset();
  const id = row.id || ids.value[0];
  const res = await getPlatformConfig(id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改登录配置';
};

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

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

getList();
</script>
