<template>
    <PagingTable url="/sys-config" :where="where" ref="table">
        <template #filter>
            <ElButton :icon="Plus" @click="onEdit()">添加</ElButton>
        </template>
        <ElTableColumn prop="id" label="#" width="80" />
        <ElTableColumn prop="name" label="名称" width="150" />
        <ElTableColumn prop="remark" label="备注" min-width="120" show-overflow-tooltip />
        <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="onEdit(row)">编辑</ElButton>
            </template>
        </ElTableColumn>
    </PagingTable>
    <EditDialog v-model="showEditDialog" :model="model" :rules="rules" :on-submit="submit" @success="table.refresh()">
        <ElFormItem prop="name" label="名称">
            <ElInput v-model="model.name" placeholder="请输入名称" />
        </ElFormItem>
        <ElFormItem prop="remark" label="备注">
            <ElInput v-model="model.remark" placeholder="请输入备注" />
        </ElFormItem>
        <ElFormItem prop="type" label="类型">
            <EnumSelect v-model="model.type" :enum="SysConfigType" />
        </ElFormItem>
        <ElFormItem prop="value" label="值" v-if="model.type === 'file'">
            <ElInput v-model="model.value" placeholder="请输入">
                <template #append>
                    <ElButton type="primary" @click="chooseFile" :loading="uploading">上传</ElButton>
                </template>
            </ElInput>
        </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'
import resolveUrl from 'resolve-url'
import { SysConfigType } from '@/enums'

const uploadUrl = resolveUrl(import.meta.env.VITE_API_BASE_URL, '/api/file/upload')
const where = ref({})
const timeFormatter = useTimeFormatter()
const table = ref(null)
const model = ref({})
const rules = {
    name: [
        { required: true, message: '请输入名称', trigger: 'blur' },
        { type: 'string', pattern: /^[a-zA-Z0-9_]+$/, message: '只能输入字母、数字、下划线' }
    ],
    value: [{ required: true, message: '请输入值', trigger: 'blur' }]
}
const { showEditDialog, onEdit } = setupEditDialog(model)
async function submit() {
    await http.put('/admin/sys-config', model.value)
    ElMessage.success('保存成功')
}
function getToken(row) {
    http.get(`/auth/admin/user/${row.id}/token`).then((res) => {
        const { copy } = useClipboard({ legacy: true })
        copy(res.access_token)
        ElMessage.success('复制成功')
    })
}
const uploading = ref(false)
function chooseFile() {
    const input = document.createElement('input')
    input.type = 'file'
    input.onchange = async (e) => {
        const file = e.target.files[0]
        if (!file) {
            return
        }
        uploading.value = true
        const formData = new FormData()
        formData.append('file', file)
        const res = await http.post(uploadUrl, formData)
        model.value.value = res.url
        uploading.value = false
    }
    input.click()
}
</script>
