import { copy } from "@/utils/utils"
import { ColumnsType } from "antd/es/table"
import { FANQIELINKTYPEEle, HUASHENGLINKTYPEEle, LINKTYPEEle, WENDINGLINKTYPEELE } from "./const"
import { Button, Flex, Popconfirm, Popover, Tag } from "antd"
import Details from "./details"


export function bookLinkTableConfig(isSelect: boolean, handleCopy?: (value: any) => void, handleDel?: (data: { linkIds: number[] }) => void): ColumnsType<any> {

    const arr: ColumnsType<any> = [
        {
            title: '书城',
            dataIndex: 'platformName',
            key: 'platformName',
            align: 'center',
            width: 70,
            ellipsis: true,
            fixed: 'left',
            render: (value, records) => {
                return <Tag color={records.platform === 'YUE_WEN' ? '#f50' : records.platform === 'FAN_QIE' ? '#2db7f5' : records.platform === 'HUA_SHENG' ? '#87d068' : '#108ee9'}><span style={{ fontSize: 12 }}>{value}</span></Tag>
            }
        },
        {
            title: '公众号',
            dataIndex: 'mpAccountName',
            key: 'mpAccountName',
            align: 'center',
            width: 80,
            fixed: 'left',
            render: (value) => {
                return <span style={{ fontSize: 12 }}>{value}</span>
            }
        },
        {
            title: '渠道名称',
            dataIndex: 'channelName',
            key: 'channelName',
            width: 300,
            ellipsis: true,
            render: (value) => {
                return <span style={{ fontSize: 12 }}>{value || '--'}</span>
            }
        },
        {
            title: '推广类型',
            dataIndex: 'channelType',
            key: 'channelType',
            align: 'center',
            width: 80,
            render: (value, records) => {
                return records.platform === 'WEN_DING' ? <span style={{ fontSize: 12 }}>{value === 0 ? '内推' : value === 1 ? '派单' : '--'}</span> : <span style={{ fontSize: 12 }}>{value === 1 ? '外部' : value === 2 ? '内部' : '--'}</span>
            }
        },
        {
            title: '链接类型',
            dataIndex: 'linkType',
            key: 'linkType',
            align: 'center',
            width: 120,
            render: (value, records) => {
                return records.platform === 'YUE_WEN' ? LINKTYPEEle[value] :
                    records.platform === 'FAN_QIE' ? FANQIELINKTYPEEle[value] :
                        records.platform === 'HUA_SHENG' ? HUASHENGLINKTYPEEle[value] :
                            records.platform === 'WEN_DING' ? WENDINGLINKTYPEELE[value] :
                                '--'
            }
        },
        {
            title: '生成链接',
            dataIndex: 'link',
            key: 'link',
            width: 200,
            ellipsis: true,
            render: (value) => {
                return value ? <a style={{ fontSize: 12 }} onClick={() => copy(value)}>{value}</a> : '--'
            }
        },
        {
            title: '小程序Appid',
            dataIndex: 'wxAppid',
            key: 'wxAppid',
            width: 80,
            ellipsis: true,
            render: (value) => {
                return value ? <a style={{ fontSize: 12 }} onClick={() => copy(value)}>{value}</a> : '--'
            }
        },
        {
            title: '小程序链接',
            dataIndex: 'miniappPagePath',
            key: 'miniappPagePath',
            width: 200,
            ellipsis: true,
            render: (value) => {
                return value ? <a style={{ fontSize: 12 }} onClick={() => copy(value)}>{value}</a> : '--'
            }
        },
        {
            title: '创建人',
            dataIndex: 'createByInfo',
            key: 'createByInfo',
            align: 'center',
            width: 70,
            render: (value) => {
                return <span style={{ fontSize: 12 }}>{value?.nickname}</span>
            }
        },
        {
            title: '创建时间',
            dataIndex: 'createTime',
            key: 'createTime',
            align: 'center',
            width: 140,
            render: (value) => {
                return <span style={{ fontSize: 12 }}>{value}</span>
            }
        },
        {
            title: '操作',
            dataIndex: 'cz',
            key: 'cz',
            align: 'center',
            width: 100,
            fixed: 'right',
            render: (_, records) => {
                return <Flex gap={4} justify='center'>
                    <Popover
                        placement="left"
                        content={<Details linkType={records.linkType} platform={records.platform} linkContent={records.linkContext} />}
                        styles={{ body: { width: 300, overflow: 'hidden', overflowY: 'auto', maxHeight: 400 } }}
                    >
                        <Button type="link" style={{ padding: 0, height: 'auto' }}>详情</Button>
                    </Popover>
                    {!isSelect && <Button type="link" style={{ padding: 0, height: 'auto' }} onClick={() => handleCopy(records)}>复制</Button>}
                    <Popconfirm
                        title="确定删除？"
                        onConfirm={() => { handleDel?.({ linkIds: [records.id] }) }}
                    >
                        <a style={{ color: 'red' }}>删除</a>
                    </Popconfirm>
                </Flex>
            }
        },
    ]
    return arr
}