
import { useRef, useState, useEffect } from 'react';
import { connect } from '@umijs/max';
import type { ActionType, ProFormInstance } from '@ant-design/pro-components';
import {
    ProFormText,
    ProFormSegmented,
    ProFormSelect,
    ProForm,
    ProFormDatePicker,
    ProFormCascader,
    ProFormDigit
} from '@ant-design/pro-components';
import { ConfigProvider, Flex, Space, message, Row, Col, Image } from 'antd';
import { PostJobapplicantAdd, PostJobapplicantUpdate, GetJobapplicantDetail } from '@/apis/api';
import { Imageprefix } from '@/constants';
import UploadModel from '@/components/Common/upload';
import { verifyIdCard, verifySex } from '@/utils/VerifyHelper'
import { disAfterData } from '@/utils/format'
const selectfieldNames = { label: 'name', value: 'id' };
const cascaderfieldNames = { label: 'name', value: 'id', children: 'children' };

const PagesMainJobseekerListResumeBasic: React.FC = ({ dispatch, dictModel, openModel, id }: any) => {
    const formRef = useRef<ProFormInstance>();
    const [detail, setDetail] = useState<ResumeType.basic | null>(null)


    useEffect(() => {
        dispatch({ type: 'dictModel/getList', payload: { code: 2004, type: 'setPositionList' } })
        dispatch({ type: 'dictModel/getList', payload: { code: 2009, type: 'setAreaList' } })
        dispatch({ type: 'dictModel/getList', payload: { code: 2005, type: 'setPayList' } })
        dispatch({ type: 'dictModel/getList', payload: { code: 2016, type: 'setAccommodationRequirementList' } })
        dispatch({ type: 'dictModel/getList', payload: { code: 2017, type: 'setHolidayRestList' } })
        dispatch({ type: 'dictModel/getList', payload: { code: 2018, type: 'setLicenseList' } })
        dispatch({ type: 'dictModel/getList', payload: { code: 2021, type: 'setWorkExperienceList' } })
        dispatch({ type: 'dictModel/getList', payload: { code: 2006, type: 'setEducationList' } })
        dispatch({ type: 'dictModel/getList', payload: { code: 2014, type: 'setTitleList' } })
        dispatch({ type: 'dictModel/getList', payload: { code: 2015, type: 'setSkillCertificationeList' } })
        dispatch({ type: 'dictModel/getList', payload: { code: 2007, type: 'setOtherLanguageList' } })
        dispatch({ type: 'dictModel/getList', payload: { code: 2008, type: 'setDegreeList' } })
        dispatch({ type: 'dictModel/getList', payload: { code: 2027, type: 'setIndustryPostList' } })
        dispatch({ type: 'dictModel/getList', payload: { code: 2010, type: 'setPoliticalList' } })
    }, [])

    const [uploadedFilephotoName, setUploadedFilephotoName] = useState<string>('');
    const handleFileUploadedphoto = (filename: string) => {
        setUploadedFilephotoName(filename);
    };

    return (
        <>
            <ConfigProvider
                theme={{
                    token: {
                        colorPrimary: '#4FBE70',
                    }, components: {
                        Segmented: {
                            itemSelectedBg: '#19be6e',
                            itemSelectedColor: '#ffffff'
                        },
                    },
                }}
            >
                <ProForm<ResumeType.basic>
                    formRef={formRef}
                    request={async () => {
                        if (id) {
                            let res = await GetJobapplicantDetail({ customer_id: id.customer_id })
                            res.data.reg_arr = [
                                res.data.reg_level1 ? res.data.reg_level1 : 0,
                                res.data.reg_level2 ? res.data.reg_level2 : 0,
                                res.data.reg_level3 ? res.data.reg_level3 : 0,
                                res.data.reg_level4 ? res.data.reg_level4 : 0
                            ]
                            res.data.now_arr = [
                                res.data.now_level1 ? res.data.now_level1 : 0,
                                res.data.now_level2 ? res.data.now_level2 : 0,
                                res.data.now_level3 ? res.data.now_level3 : 0,
                                res.data.now_level4 ? res.data.now_level4 : 0
                            ]
                            res.data.native_arr = ['',
                                res.data.native_place_txt ? res.data.native_place_txt : 0
                            ]
                            setUploadedFilephotoName(res.data.photo)
                            setDetail(res.data)
                            return res.data;
                        }
                    }}
                    submitter={{
                        searchConfig: {
                            resetText: '取消',
                            submitText: '保存',
                        },
                        render: (_, dom) => { return <Flex justify='flex-end'> <Space>{dom}</Space></Flex> },
                        onReset: () => {
                            dispatch({ type: 'openModel/getOpenModal', payload: false })
                        }
                    }}
                    onFinish={async (values) => {
                        values.photo = uploadedFilephotoName ? uploadedFilephotoName : '';
                        // 处理户口所在地
                        for (let i = 0; i < 4; i++) {
                            values[`reg_level${i + 1}`] = values.reg_arr[i] || 0;
                        }
                        for (let i = 0; i < 4; i++) {
                            values[`now_level${i + 1}`] = values.now_arr[i] || 0;
                        }
                        if (values.native_arr && values.native_arr.length == 1) {
                            message.warning('请选择市');
                        } else {
                            values.native_place = detail && detail.native_place != 0 ? detail.native_place : values.native_arr[1];
                        }
                        let year = values.id_number.substring(6, 10);
                        let month = values.id_number.substring(10, 12);
                        let day = values.id_number.substring(12, 14);
                        values.dob = year + '-' + month + '-' + day;
                        values.gender = verifySex(values.id_number);
                        delete values.reg_arr;
                        delete values.now_arr;
                        delete values.native_arr;
                        if (detail) {
                            values.id = detail.id;
                            values.customer_id = detail.customer_id;
                            let res = await PostJobapplicantUpdate(values)
                            message.success('提交成功')
                        } else {
                            let res = await PostJobapplicantAdd(values)
                            message.success('提交成功')
                            dispatch({ type: 'openModel/getOpenModal', payload: false })
                        }
                    }}
                >
                    <Row gutter={[16, 16]}>
                        <Col span={24} style={{ display: 'flex' }}>
                            <UploadModel multiple={false} form_name="photo" image_length={1} uploadTxt="个人照片" image_type={3} imageUrl={uploadedFilephotoName} onUploadComplete={handleFileUploadedphoto}></UploadModel>
                        </Col>
                        <Col span={8}>
                            <ProFormText
                                label="姓名"
                                name="name"
                                placeholder="请输入姓名"
                            />
                        </Col>
                        {/* <Col span={8}>
                            <ProFormSegmented
                                name="gender"
                                label="性别"
                                request={async () => [
                                    { label: '男', value: '男' },
                                    { label: '女', value: '女' }
                                ]}
                            />
                        </Col> */}

                        {/* <Col span={8}>
                            <ProFormDatePicker
                                name="dob"
                                label="出生日期"
                                placeholder='请选择出生日期'
                                fieldProps={{
                                    disabledDate: disAfterData
                                }}
                            />
                        </Col> */}
                        <Col span={8}>
                            <ProFormText
                                label="身份证"
                                name="id_number"
                                placeholder="请输入身份证"
                                validateTrigger='onBlur'
                                rules={[{
                                    required: false,
                                }, () => ({
                                    validator: (rule, value) => {
                                        let info = verifyIdCard(value);


                                        if (typeof (info) == 'boolean') {
                                            return Promise.resolve();
                                        }
                                        return Promise.reject(new Error(info));
                                    }
                                })]}
                            />
                        </Col>
                        <Col span={8}>
                            <ProFormSelect
                                name="education"
                                label="学历"
                                fieldProps={{
                                    fieldNames: selectfieldNames
                                }}
                                options={dictModel.educationList}
                            />
                        </Col>
                        <Col span={8}>
                            <ProFormCascader
                                label="户口所在地"
                                name="reg_arr"
                                fieldProps={{
                                    fieldNames: cascaderfieldNames,
                                    options: dictModel.areaList,
                                    changeOnSelect: true
                                }}
                                placeholder="请选择户口所在地"
                            />
                        </Col>
                        <Col span={8}>
                            <ProFormCascader
                                label="现居地"
                                name="now_arr"
                                fieldProps={{
                                    fieldNames: cascaderfieldNames,
                                    options: dictModel.areaList,
                                    changeOnSelect: true
                                }}
                                placeholder="请选择现居地"
                                rules={[{ required: true, message: '请选择现居地' }]}

                            />
                        </Col>
                        <Col span={8}>
                            <ProFormSelect
                                name="title"
                                label="职称"
                                fieldProps={{
                                    fieldNames: selectfieldNames
                                }}
                                options={dictModel.titleList}
                            />
                        </Col>
                        <Col span={8}>
                            <ProFormSelect
                                name="marital_status"
                                label="婚姻状况"
                                fieldProps={{
                                    fieldNames: selectfieldNames
                                }}
                                options={dictModel.maritalList}
                            />
                        </Col>
                        <Col span={8}>
                            <ProFormSelect
                                name="ethnicity"
                                label="民族"
                                fieldProps={{
                                    fieldNames: selectfieldNames
                                }}
                                options={dictModel.ethnicityList}
                            />
                        </Col>
                        <Col span={8}>
                            <ProFormSelect
                                name="political_status"
                                label="政治面貌"
                                fieldProps={{
                                    fieldNames: selectfieldNames
                                }}
                                options={dictModel.politicalList}
                            />
                        </Col>
                        <Col span={8}>
                            <ProFormCascader
                                label="籍贯"
                                name="native_arr"
                                fieldProps={{
                                    fieldNames: cascaderfieldNames,
                                    options: dictModel.areaList,
                                    changeOnSelect: true
                                }}
                                placeholder="请选择籍贯"
                            />
                        </Col>
                        <Col span={8}>
                            <ProFormDigit
                                label="身高(厘米cm)"
                                placeholder="请输入身高(厘米cm)"
                                name="height"
                                min={0}
                                max={300}
                            />
                        </Col>
                        <Col span={8}>
                            <ProFormDigit
                                label="体重(千克kg)"
                                placeholder="请输入体重(千克kg)"
                                name="weight"
                                min={0}
                                max={300}
                            />
                        </Col>
                    </Row>
                </ProForm>
            </ConfigProvider>
        </>
    );
};



export default connect(({ dictModel, openModel }: any) => ({
    dictModel, openModel
}))(PagesMainJobseekerListResumeBasic);