
import { useRef, useState, useEffect } from 'react';
import { connect } from '@umijs/max';
import type { ActionType, ProFormInstance } from '@ant-design/pro-components';
import {
    EditableProTable
} from '@ant-design/pro-components';
import { ConfigProvider, Flex, Space, message } from 'antd';
import { PostJobapplicantAddtraining, PostJobapplicantDeltraining, PostJobapplicantUpdatetraining, PostJobapplicantListtraining, PostJobapplicantTrainingdetail } from '@/apis/api';

const PagesMainJobseekerListResumeTrain: React.FC = ({ dispatch, dictModel, openModel, id }: any) => {
    const actionRef = useRef<ActionType>();
    const [editableKeys, setEditableRowKeys] = useState<React.Key[]>([]);
    const [list, setList] = useState<object[]>([])
    const [originList, setOriginList] = useState<object[]>([])

    const [total, setTotal] = useState<number>(0)

    useEffect(() => {
        dispatch({ type: 'dictModel/getList', payload: { code: 2007, type: 'setOtherLanguageList' } })
        dispatch({ type: 'dictModel/getList', payload: { code: 2008, type: 'setDegreeList' } })
        dispatch({ type: 'dictModel/getList', payload: { code: 2023, type: 'setVehicleList' } })
        dispatch({ type: 'dictModel/getList', payload: { code: 2024, type: 'setOvertimeList' } })
    }, [])


    return (
        <>
            <ConfigProvider
                theme={{
                    token: {
                        colorPrimary: '#4FBE70',
                    }
                }}
            >
                <EditableProTable
                    actionRef={actionRef}
                    rowKey="id"
                    loading={false}
                    request={async () => {
                        let res = await PostJobapplicantListtraining({
                            customer_id: id.customer_id,
                            sortby: 'asc',
                            sort: 'id'
                        })
                        setList(res.data.jobapplicanttrainings)
                        setOriginList(res.data.jobapplicanttrainings)
                        setTotal(res.data.total)
                        return {
                            data: res.data.jobapplicanttrainings,
                            total: res.data.total,
                            success: true,
                        }

                    }}
                    columns={[
                        {
                            title: 'ID',
                            dataIndex: 'id',
                            editable: false,
                            width: 100

                        },
                        {
                            title: '起止时间',
                            dataIndex: 'date',
                            valueType: 'dateRange',
                            fieldProps: {
                                format: 'YYYY-MM'
                            },
                            width: 200,
                            formItemProps: {
                                rules: [
                                    {
                                        required: true,
                                        message: '请选择起止时间',
                                    },
                                ],
                            },
                            render: (text, record, _, action) => (
                                <>
                                    {
                                        `${record.start_year}-${record.start_month}至${record.end_year}-${record.end_month}`
                                    }
                                </>
                            ),

                        },
                        {
                            title: '是否至今',
                            dataIndex: 'end_today',
                            width: 100,
                            valueType: 'select',
                            fieldProps: {
                                options: [
                                    { label: '非至今', value: 1 },
                                    { label: '至今', value: 2 },
                                ]
                            },
                            formItemProps: {
                                rules: [
                                    {
                                        required: true,
                                        message: '请选择是否至今',
                                    },
                                ],
                            },
                        },
                        {
                            title: '学校/培训机构',
                            dataIndex: 'school_or_institution',
                            formItemProps: {
                                rules: [
                                    {
                                        required: true,
                                        message: '请输入学校/培训机构',
                                    },
                                ],
                            },
                        },
                        {
                            title: '专业',
                            dataIndex: 'major',
                            formItemProps: {
                                rules: [
                                    {
                                        required: true,
                                        message: '请输入专业',
                                    },
                                ],
                            },
                        },
                        {
                            title: '获得证书',
                            dataIndex: 'certificate',
                            formItemProps: {
                                rules: [
                                    {
                                        required: true,
                                        message: '请输入获得证书',
                                    },
                                ],
                            },
                        },
                        {
                            title: '曾获学校奖项或技能比赛奖项',
                            dataIndex: 'awards',
                            formItemProps: {
                                rules: [
                                    {
                                        required: true,
                                        message: '请输入曾获学校奖项或技能比赛奖项',
                                    },
                                ],
                            },
                        },
                        {
                            title: '曾任职务',
                            dataIndex: 'positions_held',
                            formItemProps: {
                                rules: [
                                    {
                                        required: true,
                                        message: '请输入曾任职务',
                                    },
                                ],
                            },
                        },
                        {
                            title: '操作',
                            valueType: 'option',
                            width: 200,
                            render: (text, record, _, action) => [
                                <a
                                    key="editable"
                                    onClick={() => {
                                        record.date = [`${record.start_year}-${record.start_month}`, `${record.end_year}-${record.end_month}`]
                                        action?.startEditable?.(record.id);
                                    }}
                                >
                                    编辑
                                </a>,
                                <a
                                    key="delete"
                                    onClick={() => {
                                    }}
                                >
                                    删除
                                </a>,
                            ],
                        },
                    ]}
                    editable={{
                        type: 'single',
                        editableKeys,
                        onSave: async (rowKey, data: ResumeType.train, row) => {
                            console.log(rowKey, data, row)
                            data.customer_id = id.customer_id;
                            let time1 = data.date[0].split('-');
                            let time2 = data.date[1].split('-');
                            data.start_year = parseInt(time1[0], 10);
                            data.start_month = parseInt(time1[1], 10);
                            data.end_year = parseInt(time2[0], 10);
                            data.end_month = parseInt(time2[1], 10);
                            delete data.date;
                            delete data.index;
                            if (rowKey && originList.findIndex(item => item.id === rowKey) !== -1) {
                                data.id = rowKey;
                                let res = await PostJobapplicantUpdatetraining(data)
                                message.success('提交成功')
                                actionRef.current.reload();
                            } else {
                                delete data.id;
                                let res = await PostJobapplicantAddtraining(data)
                                message.success('提交成功')
                                actionRef.current.reload();
                            }
                        },
                        onChange: setEditableRowKeys,
                    }}
                    recordCreatorProps={
                        {
                            position: 'bottom',
                            record: () => ({ id: list.length > 0 ? (list[list.length - 1].id + 1).toFixed(0) : 0 }),
                        }
                    }
                />
            </ConfigProvider>
        </>
    );
};



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