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 } from 'antd';
import {
    PostJobapplicantAdddesireindustry,
    PostJobapplicantDeldesireindustry,
    PostJobapplicantUpdatedesireindustry,
    PostJobapplicantListdesireindustry,
    PostJobapplicantDesireindustrydetail,
} from '@/apis/api';

const selectfieldNames = { label: 'name', value: 'id', children: 'childss' };

const PagesMainJobseekerListResumeIntentionIndustry: 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)

    useEffect(() => {
        dispatch({ type: 'dictModel/getList', payload: { code: 2009, type: 'setAreaList' } })
    }, [])

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

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

                    },
                        {
                            title: '行业',
                            dataIndex: 'industry',
                            render: (text, record) => (
                                <>
                                    <span>{record.industry_txt}</span>
                                </>
                            ),
                            valueType: 'select',
                            fieldProps: (form, config) => {
                                return {
                                    placeholder: '请选择行业',
                                    options: dictModel.industryPostList,
                                    fieldNames: selectfieldNames,
                                }
                            },
                        },
                        {
                            title: '操作',
                            valueType: 'option',
                            width: 200,
                            render: (text, record, _, action) => [
                                <a
                                    key="editable"
                                    onClick={async () => {
                                        if(total > 3) {
                                            message.error('最多添加3条意向行业')
                                            return false;
                                        } 
                                        action?.startEditable?.(record.post_id);
                                    }}
                                >
                                    编辑
                                </a>,
                                <a
                                    key="delete"
                                    onClick={() => {
                                    }}
                                >
                                    删除
                                </a>,
                            ],
                        },
                    ]}
                    editable={{
                        type: 'single',
                        editableKeys,
                        onSave: async (rowKey, data: ResumeType.intentionIndustry, row) => {
                            console.log(rowKey, data, row)
                            data.customer_id = id.customer_id;
                            if (rowKey && originList.findIndex(item => item.id === rowKey) !== -1) {
                                data.id = rowKey;
                                let res1 = await PostJobapplicantUpdatedesireindustry({ id: data.id, customer_id: data.customer_id, industry: data.industry })
                                message.success('提交成功')
                                actionRef.current.reload();
                            } else {
                                let res1 = await PostJobapplicantAdddesireindustry({ customer_id: data.customer_id, industry: data.industry })
                                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
}))(PagesMainJobseekerListResumeIntentionIndustry);