<template>
    <PagingTable url="/properties" :query="query" ref="table">
        <template #filter>
            <ElButton :icon="Plus" @click=";(editMode = false), onEdit()">添加</ElButton>
        </template>
        <ElTableColumn prop="id" label="#" width="150" />
        <ElTableColumn prop="name" label="名称" width="150" />
        <ElTableColumn prop="value" label="值" min-width="120" show-overflow-tooltip />
        <ElTableColumn prop="createdAt" label="创建时间" :formatter="timeFormatter" width="150" />
        <ElTableColumn label="操作" align="center" width="120">
            <template #default="{ row }">
                <ElButton @click=";(editMode = true), onEdit(row)">编辑</ElButton>
            </template>
        </ElTableColumn>
    </PagingTable>
    <EditDialog v-model="showEditDialog" :model="model" :rules="rules" :on-submit="submit" @success="table.refresh()">
        <ElFormItem prop="id" label="ID" v-if="!editMode">
            <ElInput v-model="model.id" placeholder="请输入ID" />
        </ElFormItem>
        <ElFormItem prop="name" label="名称">
            <ElInput v-model="model.name" placeholder="请输入名称" />
        </ElFormItem>
        <ElFormItem prop="type" label="类型">
            <ElInput v-model="model.type" placeholder="请输入类型" />
        </ElFormItem>
        <ElFormItem prop="value" label="值" v-if="model.name === 'model'">
            <ElSelect v-model="model.value">
                <ElOption label="gpt-3.5" value="gpt-3.5" />
                <ElOption label="gpt-4" value="gpt-4" />
                <ElOption label="azure-gpt-3.5" value="azure-gpt-3.5" />
                <ElOption label="azure-gpt-4" value="azure-gpt-4" />
                <ElOption label="cf-gpt-3.5" value="cf-gpt-3.5" />
                <ElOption label="cf-gpt-4" value="cf-gpt-4" />
            </ElSelect>
        </ElFormItem>
        <ElFormItem prop="value" label="值" v-else>
            <ElInput v-model="model.value" placeholder="请输入值" />
        </ElFormItem>
    </EditDialog>
</template>
<script setup>
import { ref } from 'vue'
import PagingTable from '@/components/PagingTable.vue'
import { useTimeFormatter } from '@/utils/formatter'
import { Plus } from '@vicons/tabler'
import EditDialog from '@/components/EditDialog.vue'
import { setupEditDialog } from '@/utils/editDialog'
import EnumSelect from '@/components/EnumSelect.vue'
import { UserRole } from '@/enums'
import { http } from '@/plugins/http'
import { ElMessage } from 'element-plus'
import { useClipboard } from '@vueuse/core'

const query = ref({})
const timeFormatter = useTimeFormatter()
const table = ref(null)
const model = ref({})
const rules = {
    id: [
        { required: true, message: '请输入ID', trigger: 'blur' },
        { type: 'string', pattern: /^[a-zA-Z0-9_]+$/, message: '只能输入字母、数字、下划线', trigger: 'blur' },
        { min: 1, max: 72, message: '长度在 1 到 72 个字符', trigger: 'blur' }
    ],
    name: [
        { required: true, message: '请输入名称', trigger: 'blur' },
        { type: 'string', pattern: /^[a-zA-Z0-9_]+$/, message: '只能输入字母、数字、下划线', trigger: 'blur' }
    ],
    type: [{ required: true, message: '请输入类型', trigger: 'blur' }],
    value: [{ required: true, message: '请输入值', trigger: 'blur' }]
}
const { showEditDialog, onEdit } = setupEditDialog(model)
const editMode = ref(false)
async function submit() {
    await (editMode.value
        ? http.put(`/properties/${model.value.id}`, model.value)
        : http.post('/properties', model.value))
    ElMessage.success('保存成功')
}
</script>
