import { useState, useEffect, useRef } from 'react';
import { Link, history, connect } from '@umijs/max';
import type { CaptFieldRef, FormInstance } from '@ant-design/pro-components';
import { HomeOutlined, FormOutlined, CameraOutlined } from '@ant-design/icons'
import { LoginForm, ProConfigProvider, ProFormDatePicker, ProFormCheckbox, ProFormText, ProFormSelect, ProForm, ProFormCascader, ProFormDigit, ProFormRadio } from '@ant-design/pro-components';
import { Space, Tabs, Button, Avatar, Row, Col, Image, Flex, Card, Modal, ConfigProvider } from 'antd';
import { selectfieldNames, cascaderfieldNames } from '@/constants'
import { verifyPhone } from '@/utils/VerifyHelper'
import { findAncestors } from '@/utils/dataHelper'

import {  PostJobapplicantAddDesireindustry, PostJobapplicantUpdateDesireindustry } from '@/services/apis/resume'



interface ManageJobseekerResumeIntentionIndustryCreateProps {
    detail: any,
    closeModel: (value: boolean) => void
}
const ManageJobseekerResumeIntentionIndustryCreate: React.FC<ManageJobseekerResumeIntentionIndustryCreateProps> = ({ dispatch, dictModel, detail, closeModel }: any) => {
    const formRef = useRef<FormInstance | null>();

    useEffect(() => {
        dispatch({ type: 'dictModel/getList', payload: { code: 2028, type: 'setIndustryPostList' } })
    }, [])

    return (
        <>
            <ConfigProvider
                theme={{
                    token: {
                        colorPrimary: '#19be6e',
                    }, components: {
                    },
                }}
            >
                <ProForm
                    <Resume.IntentionPosition>
                    formRef={formRef}
                    request={async () => {
                        if (detail && detail.id){
                            let level0 = findAncestors(dictModel.industryPostList,detail.industry)
                            detail.industry_array = [
                                level0[0] ? level0[0] : 0,
                                detail.industry ? detail.industry : ''
                            ]
                            return detail
                        } else {
                            return {}
                        }
                    }}
                    submitter={{
                        searchConfig: {
                            resetText: '退出编辑',
                            submitText: '保存',
                        },
                        render: (_, dom) => { return <Flex justify='flex-end'> <Space>{dom}</Space></Flex> },
                        onReset: () => {
                            Modal.confirm({
                                title: '退出编辑',
                                content: '未完成编辑，此时退出当前编辑内容不会保存，是否确定退出？',
                                centered: true,
                                okText: '退出',
                                cancelText: '取消',
                                onOk() {
                                    closeModel(false);
                                },
                                onCancel() {
                                    return;
                                },
                            });
                        }
                    }}
                    onFinish={async (values) => {
                        values.customer_id =  Number(localStorage.getItem('customerid'));
                        values.industry =  values.industry_array && values.industry_array.length > 0 && values.industry_array[1] ? values.industry_array[1] : 0
                        delete values.industry_array;

                        if (detail && detail.id) {
                            debugger
                            values.id = detail.id;
                            let res = await PostJobapplicantUpdateDesireindustry(values);
                            closeModel(false);
                        } else {
                            let res = await PostJobapplicantAddDesireindustry(values);
                            closeModel(false);
                        }
                    }}
                >
                    <Row gutter={20}>
                        <Col span={8}>
                            <ProFormCascader
                                name="industry_array"
                                label="期望行业(请选择到第二项)"
                                fieldProps={{
                                    fieldNames: cascaderfieldNames,
                                    options: dictModel.industryPostList,
                                    changeOnSelect: true,

                                    onChange: (value) => {
                                    }
                                }}
                            />
                        </Col>
                    </Row>

                </ProForm>
            </ConfigProvider>

        </>
    );
};

export default connect(({ dictModel }: any) => ({
    dictModel
}))(ManageJobseekerResumeIntentionIndustryCreate);