import {useEffect, useState} from "react";
import {pageCmsArticle} from "@/api/cms/cmsArticle";
import {CmsArticle} from "@/api/cms/cmsArticle/model";
import Taro from '@tarojs/taro'
import {useRouter} from '@tarojs/taro'
import {Image} from '@nutui/nutui-react-taro'
import {InfiniteLoading} from '@nutui/nutui-react-taro'
import {getCmsNavigation} from "@/api/cms/cmsNavigation";
import {CmsNavigation} from "@/api/cms/cmsNavigation/model";

/**
 * 文章终极列表
 * @constructor
 */
const Index = () => {
    const {params} = useRouter();
    const [navigation, setNavigation] = useState<CmsNavigation>()
    const [list, setList] = useState<CmsArticle[]>([])

    const reload = async () => {
        // 获取栏目ID
        const categoryId = Number(params.id);
        // 当前栏目信息
        const navs = await getCmsNavigation(categoryId);
        // 终极新闻列表
        const articles = await pageCmsArticle({categoryId,limit: 50});

        // 当前栏目信息
        if (navs) {
            setNavigation(navs);
        }
        // 新闻列表
        if (articles) {
            setList(articles?.list || [])
        }
    }

    useEffect(() => {
        reload()
    }, [])

    return (
        <InfiniteLoading className={'bg-red-200 h-full'}>
            <div style={{padding: navigation?.span + 'px'}}>
                <Image src={navigation?.style} width={'100%'}
                       height={'auto'}/>
            </div>
            <div className={'rounded-lg py-3 px-2'}>
                <div className={'grid grid-cols-1 gap-3'}>
                    {
                        // 终极文章列表
                        list.map((item, index) => {
                            return (
                                <div
                                    key={index}
                                    className={'flex items-center cursor-pointer px-3'}
                                    style={{
                                        border: '3px solid #F2FE03',
                                        backgroundColor: '#C01717',
                                        color: '#F2FE03',
                                        borderRadius: '16px',
                                    }}
                                    onClick={() => Taro.navigateTo({url: `./detail?id=${item.articleId}`})}
                                >
                                    {/* 图片容器 */}
                                    {/*<div className={'w-full m-3 flex justify-center'}*/}
                                    {/*     style={{*/}
                                    {/*         width: '108px',*/}
                                    {/*         height: '160px',*/}
                                    {/*     }}>*/}
                                    {/*    <img*/}
                                    {/*        className={'object-cover'}*/}
                                    {/*        src={item.image}*/}
                                    {/*        alt={item.title || ''}*/}
                                    {/*    />*/}
                                    {/*</div>*/}
                                    {/* 标题 */}
                                    <div className={'flex flex-col items-start my-3 text-sm leading-tight'} style={{
                                        color: '#F2FE03'
                                    }}>
                                        <p className={'font-bold py-1'}>{item.title}</p>
                                    </div>
                                </div>
                            )
                        })
                    }
                </div>
            </div>
        </InfiniteLoading>
    )
}
export default Index
