import {Image, ScrollView, View } from "@tarojs/components"
import Popup from "@/components/popup"
import classnames from "classnames";
import MCheckbox from "@/components/checkbox";
import LoadingCard from "@/components/loadingCard";
import InfiniteScroll from "@/components/infiniteScroll";
import styles from "./index.module.scss"
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import Taro from "@tarojs/taro";
import { alert, goLink } from "@/common/common";
import {GetShoppingCartApi, DelShoppingCartApi} from "@/api/shopCart"
import { formatHashTag, formatImgUrl, formatPriceDiv } from "@/common/fotmat";
import { setParam } from "@/common/system";
import { debounce } from "@/common/util";

type param = {
    show?: true|false,
    onClose?: () => void
}
export default ({show = false, onClose}: param) => {
    const selectList = [
        // {value:-1, title:'不限', unit:'', eunit:''},
        {value:0, title:'大货', unit:'件', eunit:'kg'},
        {value:1,title:'剪板', unit:'米', eunit:'m'}, 
        {value:2,title:'散剪', unit:'米', eunit:'kg'}, 
        
    ]

    const [selectIndex, setSelectIndex] = useState(0)
    const selectProduct = (index:number) => {
        setSelectIndex(index)
    }

    useEffect(() => {
        resetList()
        setSelectStatus(true)
    }, [selectIndex])

    //重置勾选数据
    const resetList = () => {
        list?.map(item => {
            if(selectIndex == item.sale_mode || selectIndex == -1) {
                item.select = true
            } else {
                item.select = false
            }
        })
        setList([...list])
    }

    //获取数据
    const [list, setList] = useState<any[]>([])
    const [loading, setLoading] = useState(false)
    const {fetchData} = GetShoppingCartApi()
    const getShoppingCart = async () => {
        setLoading(true)
        const {data} = await fetchData()
        let color_list = data.color_list||[]
        color_list.reverse()
        initList(color_list)
        setList(color_list)
        setLoading(false)
    }

    //初始化全部数据
    const initList = (color_list) => {
        color_list?.map(item => {
            if(selectIndex == item.sale_mode) item.select = true
        })
    }

    //显示是展示数据
    useEffect(() => {
        if(!show) {
            setList([])
            setSelectIndex(0)
        } else {
            getShoppingCart()
        }
    }, [show])

    useEffect(() => {
        return () => {
            setList([])
        }
    }, [])

    const [showPopup, setShowPopup] = useState(false)
    useEffect(() => {
        setShowPopup(show)
    }, [show])

    //全选反选
    const [selectStatus, setSelectStatus] = useState(false)
    const selectAll = () => {
        list.map(item => {
            if(selectIndex == item.sale_mode || selectIndex == -1)
                item.select = !selectStatus
        })
        setSelectStatus(!selectStatus)
        setList([...list])
    }

    //checkbox选中回调
    const selectCallBack = (item) => {
        item.select = true
        checkSelect()
        setList([...list])
    }

    //checkbox选中判断是否全部选中，全部选中后是全选，否则反选
    const checkSelect = () => {
        let list_count = 0
        let select_count = 0
        list?.map(item => {
            if(selectIndex == -1 || selectIndex == item.sale_mode) {
                list_count ++ 
                if(item.select) select_count++
            }
        })
        setSelectStatus(select_count == list_count)
    }

    //checkbox关闭回调
    const colseCallBack = (item) => {
        item.select = false
        checkSelect()
        setList([...list])
    }

    //popup关闭
    const closePopup = () => {
        onClose?.()
        setShowPopup(false)
    }

    //删除购物车内容
    const {fetchData:delShopFetchData} = DelShoppingCartApi()
    const delSelect = () => {
        Taro.showModal({
            content: '删除所选商品？',
            success: async function (res) {
                if (res.confirm) {
                    getSelectId()
                    const res = await delShopFetchData({id:selectIds.current})
                    if(res.success) {
                        getShoppingCart()
                        Taro.showToast({
                            title: '成功',
                            icon: 'success',
                        })
                    } else {
                        Taro.showToast({
                            title: res.msg,
                            icon: 'none',
                        })
                    }
                    
                } else if (res.cancel) {
                console.log('用户点击取消')
                }
            }
        })
    }

    //获取面料选中的id
    const selectIds = useRef<number[]>([])
    const getSelectId = () => {
        selectIds.current = []
        list?.map(item => {
            if(selectIndex == -1 || selectIndex == item.sale_mode) {
                item.select&&selectIds.current.push(item.id)
            }
        })
    }

    //格式化金额
    const formatPirce = useCallback((price) => {
        return Number(formatPriceDiv(price))
    }, [])

    //格式化数量
    const formatCount = useCallback((item) => {
        return item.sale_mode == 0? item.roll + '件': (item.length/100) + 'm'
    }, [])

    //预估金额
    const estimatePrice = useMemo(() => {
        let count = 0
        list.map(item => {
            if(item.select) count += item.estimate_amount
        })
        return Number(formatPriceDiv(count))
    },[list])

    //去结算
    const orderDetail = debounce(() => {
        getSelectId()
        if(selectIds.current.length == 0) {
            alert.error('请选择面料')
        } else {
            let ids = selectIds.current.join('-')
            setParam({ids, sale_mode:selectIndex})
            closePopup()
            goLink('/pages/order/comfirm')
        }
    }, 500)
    
    return (
        <View className={styles.shop_cart_main}>
            <Popup showTitle={false} show={showPopup} onClose={() => closePopup()} >
                <View className={styles.popup_con}>
                    <View className={styles.header}>
                        <View onClick={selectAll}>{!selectStatus?'全选':'反选'}</View>
                        <View onClick={delSelect}>
                            <text className={classnames('iconfont', 'icon-shanchu', styles.miconfont)}></text>
                            删除所选
                        </View>
                    </View>
                    <View className={styles.search}>
                        {selectList.map((item) => {
                            return <View key={item.value} onClick={() => selectProduct(item.value)} className={classnames(styles.search_item, (selectIndex==item.value)&&styles.search_item_select)}>{item.title}</View>
                        })}
                    </View>
                    <View className={styles.con}>
                        {loading&&<LoadingCard/>}
                        {!loading&&list?.length > 0&&<InfiniteScroll moreStatus={false} >
                            <View className={styles.product_list}>
                                {list?.map((item, index) => {
                                    return <View key={index} className={classnames(styles.product_item, (selectIndex!=-1&&selectIndex!= item.sale_mode)&&styles.no_product_item_select)}>
                                        <View className={styles.checkbox}>
                                            <MCheckbox disabled={selectIndex!=-1&&selectIndex!=item.sale_mode} status={item.select} onSelect={() => selectCallBack(item)} onClose={() => colseCallBack(item)}/>
                                        </View>
                                        <View className={styles.img}>
                                            <Image mode="aspectFill" src={formatImgUrl(item.texture_url)}/>
                                        </View>
                                        <View className={styles.des}>
                                            <View className={styles.title}>{formatHashTag(item.product_code, item.product_name)}</View>
                                            <View className={styles.subtitle}>{item.product_color_code +' ' + item.product_color_name}</View>
                                            <View className={styles.tag}>{item.sale_mode_name}</View>
                                        </View>
                                        <View className={styles.count}>
                                            <View className={styles.price}><text>￥</text>{formatPirce(item.sale_price)}<text>/{selectList[selectIndex].eunit}</text></View>
                                            <View className={styles.long}>×{formatCount(item)}</View>
                                        </View>
                                    </View>
                                })}
                            </View>
                        </InfiniteScroll>}
                        {!loading&&list?.length == 0 &&<View className={styles.empty}>
                            <View className={styles.title}>暂未选择商品</View>
                            <View className={styles.btn}>去选购</View>
                        </View>}
                        
                    </View>
                    <View className={styles.buy_btn}>
                        <View className={styles.buy_con}>
                            <View className={styles.icon}>
                                <View className={classnames('iconfont', 'icon-gouwuche', styles.miconfont)}></View>
                            </View>
                            <View className={styles.price_con}>
                                <View className={styles.price_real}><text>￥</text>{estimatePrice}</View>
                                <View className={styles.price_forecast}>预估金额</View>
                            </View>
                            <View className={styles.goPay} onClick={() => orderDetail()}>
                                去结算
                            </View>
                        </View>
                    </View>
                </View>
            </Popup>
        </View>
    )
}

