
import Tables from "@/components/Tables"
import { useAjax } from "@/Hook/useAjax"
import { getAdqLandingPageList, putAdqLandingPage } from "@/services/launchAdq/adq"
import { CheckOutlined, SyncOutlined } from "@ant-design/icons"
import { Button, message, Modal, Space } from "antd"
import React, { useEffect, useState } from "react"
import style from '../goodsModal/index.less'
import columns from "./tableConfig"


/**
 * 获取adq落地页
 * @returns 
 */
interface Props {
    cloudParams: { adcreativeTemplateId?: number }
    visible?: boolean,
    onClose?: () => void,
    onChange?: (data: any) => void,
    data: any
}
const PageModal: React.FC<Props> = (props) => {

    /*************************/
    const { cloudParams, visible, onClose, data: data1, onChange } = props
    const [selectAdz, setSelectAdz] = useState<number>(1)   // 选择广告主
    const [data, setData] = useState<any>(data1 || [])
    const [queryForm, setQueryForm] = useState<{ accountId?: number, pageSize: number, pageNum: number }>({ pageNum: 1, pageSize: 20 })
    const [loading, setLoading] = useState<boolean>(false)

    const listAjax = useAjax((params) => getAdqLandingPageList(params))
    /*************************/

    useEffect(() => {
        if (data?.length > 0) {
            setQueryForm({ ...queryForm, accountId: data[selectAdz - 1].adAccountId })
        }
    }, [selectAdz])

    useEffect(() => {
        if (queryForm?.accountId) {
            getList()
        }
    }, [queryForm])

    // 获取落地页列表
    const getList = () => {
        console.log('data-->', cloudParams);
        
        listAjax.run({ ...queryForm, ...cloudParams })
    }

    const handleOk = () => {
        console.log('data---->', data);
        if (data?.every((item: { pageList: any }) => item.pageList)) {
            onChange && onChange(data)
        } else {
            message.error('还有账号未选择落地页！')
        }
    }

    /** 设置选中广告主 */
    const handleSelectAdz = (value: number, item: any) => {
        if (value === selectAdz) {
            return
        }
        setSelectAdz(value)
    }

    /** 表格选折 */
    const onChangeTable = (selectedRowKeys: React.Key[], selectedRows: any) => {
        let newData = JSON.parse(JSON.stringify(data))
        newData[selectAdz - 1]['pageList'] = selectedRows
        setData([...newData])
    }

    /** 同步落地页 */
    const synPageList = () => {
        setLoading(true)
        let ajaxs = data?.map((item: { id: number }) => putAdqLandingPage(item.id))
        Promise.all(ajaxs).then(res => {
            setLoading(false)
            listAjax.refresh()
        }).catch(() => setLoading(false))
    }

    console.log('data---->', data);
    
    
    return <Modal
        title={<Space>
            <span>ADQ落地页</span>
            <Button size="small" onClick={() => { synPageList() }} type="link" loading={loading}>同步落地页</Button>
        </Space>}
        visible={visible}
        onCancel={() => { onClose && onClose() }}
        onOk={handleOk}
        width={1100}
        className={style.SelectPackage}
        bodyStyle={{ padding: '0 10px 0 10px' }}
    >

        <div className={style.content}>
            <div className={style.left}>
                <h4 className={style.title}>媒体账户</h4>
                {data?.map((item: { adAccountId: number, id: number }, index: number) => (
                    <div key={index} onClick={() => { handleSelectAdz(index + 1, item) }} className={`${style.accItem} ${selectAdz === index + 1 && style.select} `}>
                        {item?.adAccountId}
                        {data[index].pageList?.length > 0 && <CheckOutlined style={{ color: '#1890ff' }} />}
                    </div>))}
            </div>
            <div className={style.right}>
                <Space style={{ marginBottom: 10 }} align="end">
                    <Button icon={<SyncOutlined />} type='link' loading={listAjax?.loading} onClick={() => { listAjax?.refresh() }}></Button>
                </Space>
                <Tables
                    columns={columns()}
                    dataSource={listAjax?.data?.records?.map((item: any) => ({ ...item, id: item.pageId }))}
                    size="small"
                    loading={listAjax?.loading}
                    scroll={{ y: 300 }}
                    bordered
                    total={listAjax?.data?.total}
                    defaultPageSize={20}
                    pageChange={(page: number, pageSize?: number) => {
                        setQueryForm({ ...queryForm, pageNum: page, pageSize: pageSize as number || 20 })
                    }}
                    rowSelection={{
                        type: 'radio',
                        selectedRowKeys: data[selectAdz - 1]?.pageList?.map((item: any) => item?.id?.toString()),
                        onChange: onChangeTable
                    }}
                />
            </div>
        </div>

    </Modal>
}

export default React.memo(PageModal)