
import { useRef, useState, useEffect } from 'react';
import { connect } from '@umijs/max';
import type {  ProFormInstance } from '@ant-design/pro-components';
import {
    ProForm,
    FormControlRender,
    ProFormDependency,
} from '@ant-design/pro-components';
import { ConfigProvider, Flex, Space, message,  Row, Col, Form } from 'antd';
import { PostJobapplicantAddintroduction, PostJobapplicantUpdateintroduction, GetJobapplicantIntroductiondetail } from '@/apis/api';

import RichComponent from '@/components/Common/rich';



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

    return (
        <>
            <ConfigProvider
                theme={{
                    token: {
                        colorPrimary: '#4FBE70',
                    }
                }}
            >
                <ProForm<ResumeType.introduction>
                    formRef={formRef}
                    request={async () => {
                        if (id) {
                            let res = await GetJobapplicantIntroductiondetail({ customer_id: id.customer_id })
                            setDetail(res.data)
                            return res.data;
                        }
                    }}
                    submitter={{
                        searchConfig: {
                            resetText: '取消',
                            submitText: '保存',
                        },
                        render: (_, dom) => { return <Flex justify='flex-end'> <Space>{dom}</Space></Flex> },
                        onReset: () => {
                            setDetail(null)
                            dispatch({ type: 'openModel/getOpenModal', payload: false })
                        }
                    }}
                    onFinish={async (values) => {
                        if (detail) {
                            values.id = detail.id;
                            values.customer_id = detail.customer_id;
                            let res = await PostJobapplicantUpdateintroduction(values)
                            message.success('提交成功')
                        } else {
                            let res = await PostJobapplicantAddintroduction(values)
                            message.success('提交成功')
                        }
                    }}
                >

                    <Row gutter={[16, 16]}>
                        <Col span={24}>
                            <Form.Item
                                name='description'
                                label="个人简介"
                                rules={[{ required: true, message: '请输入个人简介' }]}
                            >
                                <FormControlRender>
                                    {() => {
                                        return (
                                            <ProFormDependency name={['description']} >
                                                {({ description }, form) => {
                                                    return (
                                                        <RichComponent placeholder="请输入个人简介" defaultValue={description}
                                                            onChange={(value) => {
                                                                form.formRef.current.setFieldsValue({
                                                                    description: value
                                                                })
                                                            }}></RichComponent>
                                                    );
                                                }}
                                            </ProFormDependency>
                                        );
                                    }}
                                </FormControlRender>
                            </Form.Item>
                        </Col>
                    </Row>
                </ProForm>
            </ConfigProvider>
        </>
    );
};



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