<template>
    <a-modal :open="modal.open" :title="modal.title" :confirm-loading="modal.confirmLoading" :after-close="onAfterClose"
        @ok="handleOk" @cancel="handleCancel" width="800px">
        <a-card>
            <a-form ref="formRef" scroll-to-first-error :model="formData" :rules="formRules">
                <a-row :gutter="24">
                    <!-- <a-col :span="12">
                        <a-form-item label="地区ID" name="centerID">
                            <a-input v-model:value="formData.centerID" placeholder="请输入地区ID" />
                        </a-form-item>
                    </a-col> -->
                    <a-col :span="12">
                        <a-form-item label="核心数" name="coreNum">
                            <a-input-number v-model:value="formData.coreNum" placeholder="请输入核心数"
                                style="width: 100%" />
                        </a-form-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-item label="GPU数量" name="gpuNum">
                            <a-input-number v-model:value="formData.gpuNum" placeholder="请输入GPU数量"
                                style="width: 100%" />
                        </a-form-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-item label="GPU类型" name="gpuType">
                            <a-input v-model:value="formData.gpuType" placeholder="例如：A100, V100" />
                        </a-form-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-item label="显存 (GB)" name="vram">
                            <a-input-number v-model:value="formData.vram" placeholder="显存大小" style="width: 100%" />
                        </a-form-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-item label="状态" name="status">
                            <a-select v-model:value="formData.status" placeholder="请选择状态" style="width: 100%">
                                <a-select-option value="enabled">启用</a-select-option>
                                <a-select-option value="disabled">停用</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item label="描述" name="description">
                            <a-textarea v-model:value="formData.description" placeholder="请输入描述" :rows="3" />
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item label="是否使用" name="isUse">
                            <a-switch v-model:checked="formData.isUse" />
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </a-card>
    </a-modal>
</template>

<script setup>
import { cloneDeep } from 'lodash-es'
import apis from '@/apis'
import { useForm, useModal } from '@/hooks'
import { ref } from 'vue'
import dayjs from 'dayjs'
const emit = defineEmits(['ok'])
const { modal, showModal, hideModal, showLoading, hideLoading } = useModal()
const { formRef, formRules, formRecord, formData, resetForm } = useForm()
formData.value.computingCards = []
formRules.value = {
    // name: { required: true, message: '请输入标题' },
}

/**
 * 新建
 */
function handleCreate(record = {}) {
    showModal({
        type: 'create',
        title: '新建',
    })
    console.log(record);
    formData.value.centerID=record.id
    formData.value.hostID=record.hostID
    formData.value.status='enabled'
}

/**
 * 编辑
 */
function handleEdit(record = {}) {
    showModal({
        type: 'edit',
        title: '编辑',
    })
    formRecord.value = record
    formData.value = cloneDeep(record)
}


/**
 * 确定
 */
function handleOk() {
    formRef.value
        .validateFields()
        .then(async (values) => {
            try {
                showLoading()
                const params = {
                    ...formData.value,
                }
                let result = null
                switch (modal.value.type) {
                    case 'create':
                        result = await apis.host.createComputeCard(params).catch(() => {
                            throw new Error()
                        })
                        break
                    case 'edit':
                        result = await apis.host.updatePage(formRecord.value.id, params).catch(() => {
                            throw new Error()
                        })
                        break
                }
                hideLoading()
                if (true === result?.success) {
                    hideModal()
                    emit('ok')
                }
            } catch (error) {
                hideLoading()
            }
        })
        .catch(() => {
            hideLoading()
        })
}

/**
 * 取消
 */
function handleCancel() {
    hideModal()
}

/**
 * 关闭后
 */
function onAfterClose() {
    resetForm()
}

defineExpose({
    handleCreate,
    handleEdit,
})
</script>

<style lang="less" scoped></style>
