import { message, Spin } from "antd"
import React, { useCallback,useState } from "react"
import { api } from '@/services/api'
function ToEqq(props: { name: string, account_id: any, url: string,}) {
    let { name, account_id, url } = props
    const [isReq,setIsReq] = useState<boolean>(false)
    const go = useCallback(() => {
        if(!isReq){
            setIsReq(true)
            fetch(`${api}/erp/config/sysVersion/plugin`,{
                headers:{
                    'Authorization':'Bearer ' + sessionStorage.getItem('Admin-Token') 
                }
            }).then(r=>r.json()).then(rs=>{
                let p_v = rs?.data?.configValue
                if(p_v ===  sessionStorage.getItem('qc_chrome')){
                    fetch(`http://47.99.157.216:8020/spread/getMpCookies`, {
                        method: "post",
                        headers: { "Content-Type": "application/json" }, //如果写成contentType会报错
                        body: JSON.stringify({
                            sys_user_id: 1,
                            cookies: "SSO_SESSION_ID=3329c768-47a3-11ec-9f90-f92f44ad4e0e;redirectUrl=https://sso.e.qq.com/login/portal?service_tag=10&sso_redirect_uri=https%3A%2F%2Fe.qq.com%2Fads%2F;nickname=沈三岁;headUrl=https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTILwqawznG0IcDD3ttomv39KnUGOV0OKfXYOr0cKvHvsj6BE60YS2UKXSZKoqRQYaMUZDEH3pR2xg/132;adTicket=ST-22625-NcbVyJ0FIqneyNv7mOPdv2jBlixQff3grQMKca9SncW8qSyPhaMFWFnF5PjV3elE",
                            phone: "15924065097",
                            channels: [
                                {
                                    channel: {
                                        account_id,
                                        url,
                                        service_tag: 64,
                                    },
                                },
                            ],
                        })
                    }).then((res: any) => res.json()).then(js => {
                        setIsReq(false)
                        if (js?.data?.cookies) {
                            let { cookies, url } = js?.data
                            window.postMessage({ type: 'open', cookies, url,})
                        }else{
                            message.error(js?.msg)
                        }
                    })
                }else{
                    setIsReq(false)
                    message.error('插件版本太低，请重新下载安装插件')
                }
            })
         
        }
    }, [isReq])
    return <>
        {
            sessionStorage.getItem('qc_chrome') ? <Spin spinning={isReq}><a onClick={go}>{name}</a> </Spin>: name
        }
    </>
}
export default ToEqq