<template>
    <a-modal :open="modal.open" :title="modal.title" :confirm-loading="modal.confirmLoading" :after-close="onAfterClose"
        @ok="handleOk" @cancel="handleCancel">
        <a-form ref="formRef" scroll-to-first-error :model="formData" :rules="formRules"
            :label-col="{ style: { width: '80px' } }">
            <a-form-item label="镜像名称" name="imageName">
                <a-input v-model:value="formData.imageName"></a-input>
            </a-form-item>
             <a-form-item label="镜像类型" name="imageType">
                <a-input v-model:value="formData.imageType"></a-input>
            </a-form-item>
             <a-form-item label="镜像路径" name="imagePath">
                <a-input v-model:value="formData.imagePath"></a-input>
            </a-form-item>
            <a-form-item label="镜像大小" name="imageSize">
                <a-input v-model:value="formData.imageSize"></a-input>
            </a-form-item>
            <a-form-item label="镜像版本" name="imageVersion">
                <a-input v-model:value="formData.imageVersion"></a-input>
            </a-form-item>
             <a-form-item label="描述" name="description">
                <a-input v-model:value="formData.description"></a-input>
            </a-form-item>
        </a-form>
    </a-modal>
</template>

<script setup>
import { cloneDeep } from 'lodash-es'
import apis from '@/apis'
import { useForm, useModal } from '@/hooks'

const emit = defineEmits(['ok'])

const { modal, showModal, hideModal, showLoading, hideLoading } = useModal()
const { formRef, formRules, formRecord, formData, resetForm } = useForm()

formRules.value = {
    imageName: { required: true, message: '请输入镜像名称' },
    imageType: { required: true, message: '请输入镜像类型' },
    description: { required: false, message: '请输入描述' },
    imagePath: { required: true, message: '请输入镜像路径' },
    imageSize: { required: true, message: '请输入镜像大小' },
    imageVersion: { required: true, message: '请输入镜像版本' },
    userName: { required: true, message: '请输入所属用户名' },
}

/**
 * 新建
 */
function handleCreate() {
    showModal({
        type: 'create',
        title: '新建',
    })
}

/**
 * 编辑
 */
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 = {
                    ...values,
                }
                let result = null
                switch (modal.value.type) {
                    case 'create':
                        result = await apis.images.addVouchers(params).catch(() => {
                            throw new Error()
                        })
                        break
                    case 'edit':
                        result = await apis.images.editeVouchers(formRecord.value.id, params).catch(() => {
                            throw new Error()
                        })
                        break
                }
                hideLoading()
                if (200 === result?.code) {
                    hideModal()
                    emit('ok')
                }
            } catch (error) {
                hideLoading()
            }
        })
        .catch(() => {
            hideLoading()
        })
}

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

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

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

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