<template>
    <a-modal :open="modal.open" :title="modal.title" :width="600" :confirm-loading="modal.confirmLoading"
        :after-close="onAfterClose" :cancel-text="cancelText" @ok="handleOk" @cancel="handleCancel">
        <a-card>
            <a-form ref="formRef" :model="formData" :rules="formRules">
                <a-row :gutter="24">
                    <a-col :span="24">
                        <a-form-item label="类型" name="categoryType">
                            <a-select v-model:value="formData.categoryType" allowClear>
                                <a-select-option v-for="item in dicsStore.dictOptions.PROJECT_TYPE" :key="item.dval"
                                    :value="item.dval">{{
                                        item.introduction }}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item label="项目类型名称" name="name">
                            <a-input v-model:value="formData.name" placeholder="请输入项目类型名称"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item label="分类描述" name="remark">
                            <a-input v-model:value="formData.remark"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-item :label="'状态'" name="status">
                            <a-radio-group v-model:value="formData.status" :options="[
                                { label: '启用', value: 'enabled' },
                                { label: '停用', value: 'disabled' },
                            ]"></a-radio-group>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </a-card>

    </a-modal>
</template>

<script setup>
import { cloneDeep } from 'lodash-es'
import { ref, defineProps } from 'vue'
import { config } from '@/config'
import apis from '@/apis'
import { useForm, useModal } from '@/hooks'
import { useDicsStore } from '@/store'
import { message } from 'ant-design-vue'
const emit = defineEmits(['ok'])

const { modal, showModal, hideModal, showLoading, hideLoading } = useModal()
const { formRecord, formData, formRef, formRules, resetForm } = useForm()
const cancelText = ref('取消')
const dicsStore = useDicsStore()
formRules.value = {
    categoryType: [{ required: true, message: '请选择类型', trigger: ['blur', 'change'] }],
    name: [{ required: true, message: '请输入项目分类名称', trigger: 'blur' }],
}

/**
 * 新建
 */
function handleCreate() {
    showModal({
        type: 'create',
        title: '新增项目分类',
    })
    formData.value.status='enabled'
}
/**
 * 编辑
 */
function handleEdit(record = {}) {
    showModal({
        type: 'edit',
        title: '编辑项目分类',
    })
    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.projectType.createItem(params).catch(() => {
                            throw new Error()
                        })
                        console.log(result)
                        break
                    case 'edit':
                        result = await apis.projectType.updateItem(params).catch(() => {
                            throw new Error()
                        })
                        break
                }
                hideLoading()
                if (config('http.code.success') === result?.success) {
                    hideModal()
                    emit('ok')
                }
            } catch (error) {
                hideLoading()
            }
        })
        .catch(() => {
            hideLoading()
        })
}

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

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

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

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