<template>
    <a-modal :open="modal.open" :title="modal.title" :width="640" :confirm-loading="modal.confirmLoading"
        :after-close="onAfterClose" :cancel-text="cancelText" :ok-text="okText" @ok="handleOk" @cancel="handleCancel">
        <a-spin :spinning="spining">
            <a-form ref="formRef" :model="formData" :rules="formRules">
                <a-card class="mb-8-2">
                    <a-row :gutter="12">
                        <a-col :span="24">
                            <a-form-item :label="'招聘岗位'" name="title">
                                <a-input :placeholder="'请输入招聘岗位名称'" v-model:value="formData.title"></a-input>
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item :label="'所属基地'" name="jobAreaId">
                                <div style="display: flex;justify-content: space-between;">
                                    <a-select v-model:value="formData.jobAreaId" allowClear>
                                        <a-select-option v-for="item in areaList" :value="item.code">{{ item.name }}</a-select-option>
                                    </a-select>
                                    <a-button type="primary" @click="childOpen = true">新增基地</a-button>
                                </div>
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item :label="'岗位要求'" name="introduce">
                                <a-textarea :placeholder="'请输入岗位要求'" v-model:value="formData.introduce"></a-textarea>
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item :label="'岗位职责'" name="duty">
                                <a-textarea :placeholder="'请输入岗位职责'" v-model:value="formData.duty"></a-textarea>
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item :label="'薪资范畴'" name="salary">
                                <a-input v-model:value="formData.salary" :placeholder="'请输入薪资范畴'" />
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item :label="'岗位排序'" name="sequence">
                                <a-input-number v-model:value="formData.sequence" :placeholder="'请输入排序'"
                                    style="width: 100%;" />
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <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-card>
            </a-form>
        </a-spin>
        <a-modal ref="modalRef" v-model:open="childOpen" :wrap-style="{ overflow: 'hidden' }" @ok="childHandleOk">
            <a-card class="mb-8-2">
                <a-form ref="areaFormRef" :model="formArea" :rules="areaFormRules">
                    <a-row :gutter="12">
                        <a-col :span="24">
                            <a-form-item :label="'基地名称'" name="name">
                                <a-input v-model:value="formArea.name" :placeholder="'请输入基地名称'" />
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item :label="'基地排序'" name="name">
                                <a-input v-model:value="formArea.sequence" :placeholder="'请输入基地排序'" />
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item :label="'状态'" name="status" style="width: 100%;">
                                <a-radio-group v-model:value="formArea.status" :options="[
                                    { label: '启用', value: 'enabled' },
                                    { label: '停用', value: 'disabled' },
                                ]"></a-radio-group>
                            </a-form-item>
                        </a-col>
                    </a-row>
                </a-form>
            </a-card>
            <template #title>
                <div ref="modalTitleRef" style="width: 100%; cursor: move">新增基地</div>
            </template>

        </a-modal>
    </a-modal>
</template>

<script setup>
import { cloneDeep } from 'lodash-es'
import { ref, onBeforeMount } from 'vue'
import { config } from '@/config'
import apis from '@/apis'
import { useForm, useModal,useSpining } from '@/hooks'
import { message } from 'ant-design-vue'
import { useI18n } from 'vue-i18n'
import dayjs from 'dayjs'
const areaFormRef = ref()
const emit = defineEmits(['ok'])
const { t } = useI18n() // 解构出t方法
const { modal, showModal, hideModal, showLoading, hideLoading } = useModal()
const { formRecord, formData, formRef, formRules, resetForm } = useForm()
const { spining,showSpining,hideSpining} = useSpining()
const cancelText = ref(t('button.cancel'))
const okText = ref(t('button.confirm'))
const rolesValue = ref([])
const roles = ref([])
const imgUrl = ref('')
const areaList = ref([])
const childOpen = ref(false)
const formArea = ref({ name: '', status: 'enabled' })
formRules.value = {
    title: { required: true, message: '请输入岗位名称' },
    jobAreaId: { required: true, message: '请选择所属基地', trigger: 'change' },
    introduce: { required: true, message: '请输入岗位要求' },
    duty: [{ required: true, message: '请输入岗位职责' }],
    salary: [{ required: true, message: '请输入薪资范畴' }],
    sequence: [{ required: true, message: '请输入岗位排序' }],
    status: [{ required: true, message: '请选择状态', trigger: 'change' }],
}
const areaFormRules = {
    name: [{ required: true, message: '请输入基地名称' }],
    status: [{ required: true, message: '请选择状态', trigger: 'change' }],
}
const initData = async () => {
    try {
        showSpining()
        const { success, data, total } = await apis.recruitment.getAreasList({ pageSize: 999, page: 1, })
        if (config('http.code.success') === success) {
            hideSpining()
            areaList.value = data.map(item => {
                if (item.status === 'enabled') {
                    return { code: item.id, name: item.name }
                }
            })
        }
    } catch (error) {
        message.error({ content: error.message })
         hideSpining()
    }
}

/**
 * 新建
 */
function handleCreate() {
    showModal({
        type: 'create',
        title: '薪资招聘岗位',
    })
    initData()
    formData.value.status = 'enabled'
}

//新建基地

const childHandleOk = async () => {

    areaFormRef.value.validateFields().then(async (values) => {
        try {
            const params = { ...formArea.value }
            const { success } = await apis.recruitment.createAreaItem(params)
            if (success) message.success('新增成功')
            childOpen.value = false
            formArea.value = { name: '', status: 'enabled' }
            initData()
        } catch (error) {
            message.error(error.message)
        }
    })

}
/**
 * 编辑
 */
async function handleEdit(record = {}) {
    initData()
    showModal({
        type: 'edit',
        title: t('pages.system.user.edit'),
    })
    const { data, success } = await apis.recruitment.getItem(record.id).catch()
    if (!success) {
        hideModal()
        return
    }
    formData.value = { ...data }
    imgUrl.value = config('http.apiBasic') + data.img
}

/**
 * 确定
 */
function handleOk() {
    formRef.value.validateFields().then(async (values) => {
        try {
            showLoading()
            const params = {
                ...values,
                img: formData.value.img,
                pushAt: dayjs().format('YYYY-MM-DD'),
                type: 'news'
            }
            let result = null
            switch (modal.value.type) {
                case 'create':
                    result = await apis.recruitment.createItem(params).catch((error) => {
                        throw new Error(error)
                    })
                    break
                case 'edit':
                    result = await apis.recruitment.updateItem(formData.value.id, params).catch(() => {
                        throw new Error(error)
                    })
                    break
            }
            hideLoading()
            if (config('http.code.success') === result?.success) {
                hideModal()
                emit('ok')
            }
        } catch (error) {
            message.error({ content: error.message })
            hideLoading()
        }
    })
        .catch((e) => {
            console.log(e)
            hideLoading()
        })
}


/**
 * 取消
 */
function handleCancel() {
    imgUrl.value = ''
    hideModal()
}

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

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

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