<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="name">
                        <a-input v-model="formData.name"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="24">
                    <a-form-item label="上级通话类型" name="callType" >
                        <a-select v-model:value="formData.callType" allowClear>
                            <a-select-option v-for="item in dicsStore.dictOptions.CALL_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="callDirection" >
                        <a-select v-model:value="formData.callDirection" allowClear>
                            <a-select-option v-for="item in dicsStore.dictOptions.CALL_DIRECTION" :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="sortNo">
                        <a-input-number v-model="formData.sortNo" style="width: 100%;"></a-input-number>
                    </a-form-item>
                </a-col>
                 <a-col :span="24">
                    <a-form-item label="备注" name="remark">
                        <a-input v-model="formData.remark"></a-input>
                    </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 AreaCascader from '@/components/AreaCascader/index.vue'
const dicsStore = useDicsStore()
const emit = defineEmits(['ok'])
const activeKey = ref('1')
const { modal, showModal, hideModal, showLoading, hideLoading } = useModal()
const { formRecord, formData, formRef, formRules, resetForm } = useForm()
const cancelText = ref('取消')
formRules.value = {
    name: [{ required: true, message: '请输入类型名称', trigger: 'blur' }],
    
}
/**
 * 新建
 */
function handleCreate() {
    showModal({
        type: 'create',
        title: '新建',
    })
}

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

/**
 * 确定
 */
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.serverObj.createItem(params).catch(() => {
                            throw new Error()
                        })
                        break
                    case 'edit':
                        result = await apis.serverObj.updateItem(params).catch(() => {
                            throw new Error()
                        })
                        break
                }
                hideLoading()
                if (config('http.code.success') === result?.code) {
                    hideModal()
                    emit('ok')
                }
            } catch (error) {
                hideLoading()
            }
        })
        .catch(() => {
            hideLoading()
        })
}

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

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

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

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