import { useRef, useState, useEffect } from 'react';
import { connect } from '@umijs/max';
import type { ActionType, EditableFormInstance } from '@ant-design/pro-components';
import {
    EditableProTable,
    ProFormCascader,
} from '@ant-design/pro-components';
import { ConfigProvider, Flex, Space, message, Image, Row, Col, Cascader, Popconfirm } from 'antd';
import {
    PostJobapplicantAddassistandintro, GetJobapplicantListassist, GetJobapplicantAssistdetail, PostJobapplicantDelassist, PostJobapplicantUpdateassist
} from '@/apis/api';

const PagesMainJobseekerListResumeHelpAssist: React.FC = ({ dispatch, dictModel, openModel, id }: any) => {
    const actionRef = useRef<ActionType>();
    const editableFormRef = useRef<EditableFormInstance>();

    const [editableKeys, setEditableRowKeys] = useState<React.Key[]>([]);
    const [list, setList] = useState<object[]>([])
    const [originList, setOriginList] = useState<object[]>([])
    const [total, setTotal] = useState<number>(0)

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

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

                    },
                    {
                        title: '帮扶情况',
                        dataIndex: 'content'
                    },
                    {
                        title: '操作',
                        valueType: 'option',
                        width: 200,
                        render: (text, record, _, action) => [
                            <a
                                key="editable"
                                onClick={async () => {
                                    action?.startEditable?.(record.id);
                                }}
                            >
                                编辑
                            </a>,
                            <Popconfirm
                                title="是否删除"
                                onConfirm={(e) => {
                                    PostJobapplicantDelassist({ id: record.id }).then(res => {
                                        message.success('删除成功')
                                        actionRef.current.reload();

                                    })
                                }}
                                okText="删除"
                                cancelText="取消"
                            >
                                <a
                                    key="delete"
                                >
                                    删除
                                </a>
                            </Popconfirm>


                        ],
                    },
                    ]}
                    editable={{
                        type: 'single',
                        editableKeys,
                        onSave: async (rowKey, data: ResumeType.intentionPosition, row) => {
                            console.log(rowKey, data, row)
                            data.customer_id = id.customer_id;
                            if (rowKey && originList.findIndex(item => item.id === rowKey) !== -1) {
                                let res1 = await PostJobapplicantUpdateassist({ id: rowKey, customer_id: data.customer_id, content: data.content })
                                message.success('提交成功')
                                actionRef.current.reload();
                            } else {
                                let res1 = await PostJobapplicantAddassistandintro({ customer_id: data.customer_id, assist_content: [data.content] })
                                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
}))(PagesMainJobseekerListResumeHelpAssist);