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

/**
 * 获取转化ID
 * @returns 
 */
interface Props {
    visible?: boolean,
    onClose?: () => void,
    onChange?: (data: any) => void,
    data: any
}
const IdModal: React.FC<Props> = (props) => {

    /************************/
    const { visible, onClose, onChange, data: data1 } = props
    const [tableData, setTableData] = useState<any[]>([])//table数据
    const [selectAdz, setSelectAdz] = useState<number>(1)   // 选择广告主
    const [data, setData] = useState<any>(data1)

    const getId = useAjax((params) => getIdApi(params))
    const sysId = useAjax((params) => sysIdApi(params))
    /************************/

    useEffect(() => {
        if (data?.length > 0) {
            getList([data[selectAdz - 1].adAccountId])
        } else {
            setTableData([])
        }
    }, [selectAdz])

    // 获取数据源
    const getList = (params: number[]) => {
        getId.run(params).then(res => {
            if (res && Object.keys(res)?.indexOf(params[0].toString()) !== -1) {
                setTableData(res[params[0]]?.map((item: { userActionSetId: string }) => ({ ...item, id: item?.userActionSetId })))
            } else {
                setTableData([])
            }
        })
    }

    const handleOk = () => {
        onChange && onChange(data)
    }

    // 同步商品库
    const synIdList = () => {
        sysId.run(data?.map((item: { adAccountId: number }) => item?.adAccountId)).then(res => {
            getList([data[selectAdz - 1].adAccountId])
        })
    }

    /** 设置选中广告主 */
    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]['userActionSetsList'] = selectedRows
        setData([...newData])
    }

    return <Modal
        title={<Space>
            <span>商品库</span>
            <Button size="small" onClick={() => { synIdList() }} loading={sysId?.loading}>同步商品库</Button>
        </Space>}
        open={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].conversionList?.length > 0 && <CheckOutlined style={{ color: '#1890ff' }} />}
                    </div>))}
            </div>
            <div className={style.right}>
                <Space style={{ marginBottom: 10 }}>
                    <Button icon={<SyncOutlined />} type='link' loading={getId?.loading} onClick={() => { getList([data[selectAdz - 1].adAccountId]) }}></Button>
                </Space>
                <Tables
                    columns={columns()}
                    dataSource={tableData}
                    size="small"
                    loading={getId?.loading}
                    scroll={{ y: 300 }}
                    bordered
                    rowSelection={{
                        type: 'checkbox',
                        selectedRowKeys: data[selectAdz - 1]?.conversionList?.map((item: any) => item?.id?.toString()),
                        onChange: onChangeTable
                    }}
                />
            </div>
        </div>
    </Modal>
}


export default React.memo(IdModal)