<template>
  <div class="config-management">
    <el-card>
      <template #header>
        <div class="card-title">
          <span>参数配置</span>
          <el-button
            type="primary"
            @click="handleAdd"
          >
            新增配置
          </el-button>
        </div>
      </template>
      <el-table
        v-loading="loading"
        :data="dataSource"
        style="width: 100%"
      >
        <el-table-column
          prop="id"
          label="ID"
        />
        <el-table-column
          prop="configName"
          label="参数名称"
        />
        <el-table-column
          prop="configKey"
          label="参数键名"
        />
        <el-table-column
          prop="configValue"
          label="参数值"
        />
        <el-table-column label="类型">
          <template #default="{ row }">
            <el-tag 
              :type="row.configType === 'Y' ? 'info' : 'success'"
              effect="dark"
              style="font-weight: 500; font-size: 14px;"
            >
              {{ row.configType === 'Y' ? '内置' : '自定义' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="150"
        >
          <template #default="{ row }">
            <el-button
              type="primary"
              link
              size="small"
              @click="handleEdit(row)"
            >
              编辑
            </el-button>
            <el-button
              type="danger"
              link
              size="small"
              @click="handleDelete(row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog
      v-model="modalVisible"
      :title="modalTitle"
      width="500px"
    >
      <el-form
        :model="formState"
        label-width="80px"
      >
        <el-form-item label="参数名称">
          <el-input v-model="formState.configName" />
        </el-form-item>
        <el-form-item label="参数键名">
          <el-input v-model="formState.configKey" />
        </el-form-item>
        <el-form-item label="参数值">
          <el-input v-model="formState.configValue" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="modalVisible = false">
          取消
        </el-button>
        <el-button
          type="primary"
          @click="handleModalOk"
        >
          确定
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import request from '@/utils/request'

const loading = ref(false)
const dataSource = ref([])
const modalVisible = ref(false)
const modalTitle = ref('')
const formState = reactive({ id: null, configName: '', configKey: '', configValue: '' })

const fetchData = async () => {
  loading.value = true
  try {
    const res: any = await request.get('/config')
    dataSource.value = res
  } finally {
    loading.value = false
  }
}

const handleAdd = () => {
  modalTitle.value = '新增配置'
  Object.assign(formState, { id: null, configName: '', configKey: '', configValue: '' })
  modalVisible.value = true
}

const handleEdit = (row: any) => {
  modalTitle.value = '编辑配置'
  Object.assign(formState, row)
  modalVisible.value = true
}

const handleDelete = async (row: any) => {
  try {
    await ElMessageBox.confirm('确定要删除该配置吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    await request.delete(`/config/${row.id}`)
    ElMessage.success('删除成功')
    fetchData()
  } catch (error) {
    console.error('删除配置失败:', error)
  }
}

const handleModalOk = async () => {
  try {
    console.log('handleModalOk called, formState:', formState)
    if (formState.id) {
      console.log('Sending PUT request to /config/' + formState.id)
      const response = await request.put(`/config/${formState.id}`, formState)
      console.log('PUT response:', response)
    } else {
      console.log('Sending POST request to /config')
      const response = await request.post('/config', formState)
      console.log('POST response:', response)
    }
    ElMessage.success('操作成功')
    modalVisible.value = false
    fetchData()
  } catch (error) {
    console.error('handleModalOk error:', error)
    ElMessage.error('操作失败')
  }
}

onMounted(() => fetchData())
</script>

<style scoped lang="css">
.config-management .card-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
