<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="12">
                        <a-form-item label="姓名" name="name">
                            <a-input v-model:value="formData.name" placeholder="请输入姓名" />
                        </a-form-item>
                    </a-col>
                    <!-- 性别 -->
                    <a-col :span="12">
                        <a-form-item label="性别" name="gender">
                            <a-radio-group v-model:value="formData.gender">
                                <a-radio value="1">男</a-radio>
                                <a-radio value="2">女</a-radio>
                            </a-radio-group>
                        </a-form-item>
                    </a-col>
                    <!-- 证件号码 -->
                    <a-col :span="12">
                        <a-form-item label="身份证号" name="idCard">
                            <a-input v-model:value="formData.idCard" placeholder="请输入证件号码" />
                        </a-form-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-item label="电话" name="phone">
                            <a-input v-model:value="formData.phone" placeholder="请输入电话" />
                        </a-form-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-item label="电话" name="phone">
                            <a-input v-model:value="formData.phone" placeholder="请输入电话" />
                        </a-form-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-item label="护理人员类型" name="serviceType">
                            <a-select v-model:value="formData.serviceType" allowClear>
                                <a-select-option v-for="item in dicsStore.dictOptions.STAFF_TYPE" :key="item.dval"
                                    :value="item.dval">{{
                                        item.introduction }}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-item label="护理人员状态" name="status">
                            <a-select v-model:value="formData.status" allowClear>
                                <a-select-option v-for="item in dicsStore.dictOptions.STAFF_STATUS" :key="item.dval"
                                    :value="item.dval">{{
                                        item.introduction }}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-item label="用工形式" name="workType">
                            <a-select v-model:value="formData.workType" allowClear>
                                <a-select-option v-for="item in dicsStore.dictOptions.USE_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="stationLables">
                            <node-tree v-model:value="formData.stationLables" />
                        </a-form-item>
                    </a-col>
                    <!-- <a-col :span="8">
                        <a-form-item label="组织所在区域" name="areaLabels">
                          <node-tree v-model:value="formData.areaLabels" />
                        </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'
import NodeTree from '@/components/NodeTree/index.vue'
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('取消')
const dicsStore = useDicsStore()


/**
 * 新建
 */
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 = {
                    ...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({
    handleEdit,
    handleCreate,
})
</script>

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