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, Image, Row, Col } from 'antd';
import { PostJobapplicantAddcertificate, PostJobapplicantDelcertificate, PostJobapplicantUpdatecertificate, PostJobapplicantListcertificate, PostJobapplicantCertificatedetail } from '@/apis/api';
import { Imageprefix } from '@/constants';
import UploadModel from '@/components/Common/upload';
const PagesMainJobseekerListResumeCredetial: 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)

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


    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 PostJobapplicantListcertificate({
                            customer_id: id.customer_id,
                            sortby: 'asc',
                            sort: 'id'
                        })
                        setList(res.data.jobapplicantcertificates)
                        setOriginList(res.data.jobapplicantcertificates)
                        setTotal(res.data.total)
                        return {
                            data: res.data.jobapplicantcertificates,
                            total: res.data.total,
                            success: true,
                        }

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

                    },
                    {
                        title: '证书名称',
                        dataIndex: 'name',
                        formItemProps: {
                            rules: [
                                {
                                    required: true,
                                    message: '请输入证书名称',
                                },
                            ],
                        },
                    },
                    {
                        title: '证书描述',
                        dataIndex: 'description',
                        formItemProps: {
                            rules: [
                                {
                                    required: true,
                                    message: '请输入证书描述',
                                },
                            ],
                        },
                    },
                    {
                        title: '证书',
                        dataIndex: 'certificate_photo',
                        formItemProps: {
                            rules: [
                                {
                                    required: true,
                                    message: '请上传证书',
                                },
                            ],
                        },
                        render: (text, record) => (
                            <>
                                <Image src={`${Imageprefix + record.certificate_photo}`} width={40} height={40} preview={false} />
                            </>
                        ),
                        renderFormItem: (_, record, { isEditable }) => {
                            return isEditable ? <>
                                <UploadModel multiple={false} form_name="certificate_photo" image_length={1} uploadTxt="证书" image_type={2} imageUrl={uploadedFilephotoName} onUploadComplete={handleFileUploadedphoto}></UploadModel>
                            </> :
                                <>
                                    <UploadModel multiple={false} form_name="certificate_photo" image_length={1} uploadTxt="证书" image_type={2} imageUrl={uploadedFilephotoName} onUploadComplete={handleFileUploadedphoto}></UploadModel>
                                </>;
                        },
                    },
                    {
                        title: '操作',
                        valueType: 'option',
                        width: 200,
                        render: (text, record, _, action) => [
                            <a
                                key="editable"
                                onClick={() => {
                                    setUploadedFilephotoName(record.certificate_photo)
                                    action?.startEditable?.(record.id);
                                }}
                            >
                                编辑
                            </a>,
                            <a
                                key="delete"
                                onClick={() => {
                                }}
                            >
                                删除
                            </a>,
                        ],
                    },
                    ]}
                    editable={{
                        type: 'single',
                        editableKeys,
                        onSave: async (rowKey, data: ResumeType.credential, row) => {
                            console.log(rowKey, data, row)
                            data.customer_id = id.customer_id;
                            data.certificate_photo = uploadedFilephotoName ? uploadedFilephotoName : '';
                            delete data.index;
                            if (rowKey && originList.findIndex(item => item.id === rowKey) !== -1) {
                                data.id = rowKey;
                                let res = await PostJobapplicantUpdatecertificate(data)
                                message.success('提交成功')
                                actionRef.current.reload();
                            } else {
                                delete data.id;
                                let res = await PostJobapplicantAddcertificate(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
}))(PagesMainJobseekerListResumeCredetial);