import React, { useState } from 'react';
import { Button } from 'antd';
import SelectBookLink from './selectBookLink';

interface SelectBookLinkButtonProps {
    children?: React.ReactNode;
    linkList?: {
        type: 'link' | 'miniprogram',
        data: any[]
    }
    mpAccountId: number | null
    bookPlatForm: TASK_CREATE.BookPlatFormProps[]
    linkData: { [x: string]: any }[]
    onChange: (linkStr: string, miniprogramAppid?: string, miniprogramPage?: string) => void
    platform: string
}

const SelectBookLinkButton: React.FC<SelectBookLinkButtonProps> = ({ mpAccountId, linkList, bookPlatForm, linkData, onChange, children = '选择', platform }) => {

    /********************************/
    const [visible, setVisible] = useState<boolean>(false)
    /********************************/

    return <>
        <Button type="link" style={{ padding: 0 }} onClick={() => setVisible(true)}>
            {children}
        </Button>
        {visible && <SelectBookLink
            mpAccountId={mpAccountId}
            platform={platform}
            bookPlatForm={bookPlatForm}
            visible={visible}
            linkData={linkData}
            onClose={() => setVisible(false)}
            linkList={linkList}
            onChange={(linkStr, miniprogramAppid, miniprogramPage) => {
                console.log('linkStr', linkStr)
                onChange(linkStr, miniprogramAppid, miniprogramPage)
                setVisible(false)
            }}
        />}
    </>
};

export default SelectBookLinkButton;