import { 
    GetSaleOrderDetailApi, 
    EditSaleOrderRemarkApi, 
} from "@/api/order";
import { GetOrderPayApi } from "@/api/orderPay";
import { alert, goLink } from "@/common/common";
import { ORDER_STATUS } from "@/common/enum";
import { formatDateTime, formatImgUrl, formatPriceDiv } from "@/common/fotmat";
import OrderBtns from "@/components/orderBtns";
import SearchInput from "@/components/searchInput";
import { Image, Text, Textarea, View } from "@tarojs/components"
import Taro, {useDidShow, usePullDownRefresh, useRouter } from "@tarojs/taro";
import classnames from "classnames";
import { useCallback, useEffect, useMemo, useRef, useState, memo } from "react";
import ContentBox from "./components/contentBox";
import KindList from "./components/kindList";
import OrderState from "./components/orderState";
import styles  from './index.module.scss'
 
 export default () => {

    const router = useRouter()
    const orderId = useRef<number>(Number(router.params.id))
    useDidShow(() => {
        getSaleOrderPreView()
    })

    //获取订单详情
    const [orderDetail, setOrderDetail] = useState<any>() //获取到的原始数据
    const {fetchData: getOrderFetchData} = GetSaleOrderDetailApi()
    const getSaleOrderPreView = async () => {
        if(orderId.current) {
            let res = await getOrderFetchData({id: orderId.current})
            setOrderDetail(res.data)
        }
        Taro.stopPullDownRefresh()       
    }

    //监听获取到的数据
    useEffect(() => {
        if(orderDetail)
            formatData()
    }, [orderDetail])

    //格式化数据格式
    const [formatDetailOrder, setFormatDetailOrder] = useState<any>() //格式化后的数据
    const formatData = () => {  
        setFormatDetailOrder({
            estimate_amount: orderDetail.estimate_amount, //预估金额
            sale_mode: orderDetail.sale_mode,
            sale_mode_name: orderDetail.sale_mode_name, 
            total_colors: orderDetail.total_colors, //总颜色数量
            total_number: orderDetail.total_number, //总数量
            total_fabrics: orderDetail.total_fabrics, //面料数量
            unit: orderDetail.sale_mode == 0?'条':'m', //单位
            list: orderDetail.product_list,
            status: orderDetail.status, //订单状态
            total_sale_price: orderDetail.total_sale_price, //销售金额
            total_should_collect_money: orderDetail.total_should_collect_money, //应收金额
            total_weight_error_discount: orderDetail.total_weight_error_discount, //空差优惠
            actual_amount: orderDetail.actual_amount, //实付金额
            the_previous_status: orderDetail.the_previous_status, //取消订单时的订单状态
        })
    }
    const formatPreViewOrderMemo = useMemo(() => {
        return formatDetailOrder
    }, [formatDetailOrder])


    //获取底部按钮点击, 获取按钮状态
    const orderStateClick = useCallback((val) => {
        if(val == 1) {
            //取消订单
            getSaleOrderPreView()
        }
        if(val == 2) {

        }
    }, [orderDetail])

    //页面下拉刷新
    usePullDownRefresh(() => {
        getSaleOrderPreView()
    })


    //按钮所需数据
    const orderInfo = useMemo(() => {
        return {
            status: orderDetail?.status, //订单状态
            orderId: orderDetail?.id,
            settle_mode: orderDetail?.settle_mode,
            actual_amount: orderDetail?.actual_amount, //实付金额
            wait_pay_amount: orderDetail?.wait_pay_amount, //待付金额
        }
    }, [orderDetail])

    //订单状态枚举
    const {SaleOrderStatusCancel} = ORDER_STATUS

    return (
        <View className={styles.order_main}>
            <OrderState list={orderDetail?.logistics_details} payment_method={orderDetail?.payment_method}/>
            <KindList order={formatPreViewOrderMemo}/>
            <OrderDes/>
            {(orderDetail?.status != SaleOrderStatusCancel.value)&&<View className={styles.submit_order}>
                <OrderBtns orderInfo={orderInfo} onClick={orderStateClick}/>
            </View>}       
            <AfterSalePricture/>
            <View className="common_safe_area_y"></View>
        </View>
    )
 }

 const OrderDes = memo(({val = ''}:{val?:string}) => {
    //复制功能
    const clipboardData = () => {
        Taro.setClipboardData({
            data: val,
            success: function (res) {
                Taro.showToast({
                    icon: 'none',
                    title: '复制成功'
                })
            }
        })
    }
    return (
        <View className={styles.order_info} >
            <View className={styles.order_info_title}>订单信息</View>
            <SearchInput showBorder={false} title='售后单号' height={50}>
                <View className={styles.order_num}>
                    <Text>RA-LY-2204240002</Text>
                    <View className={styles.order_num_btn} onClick={() => clipboardData()}>复制</View>
                </View>
            </SearchInput>
            <SearchInput showBorder={false} title='订单号' height={50}>
                <View className={styles.order_num}>
                    <Text>XS-LY-2204210002</Text>
                    <View className={styles.order_num_btn} onClick={() => clipboardData()}>复制</View>
                </View>
            </SearchInput>
            <SearchInput showBorder={false} title='退款说明' height={50}>
                <Text>其他问题</Text>
            </SearchInput>
            <SearchInput showBorder={false} title='其它说明' height={50}>
                <Text>板布疵点太多</Text>
            </SearchInput>
            <SearchInput showBorder={false} title='申请时间' height={50}>
                <Text>2022-04-24 08:32:39</Text>
            </SearchInput>
        </View>
    )
 })

 const AfterSalePricture = memo(() => {
     //预览图片
    const showImage = () => {
        Taro.previewImage({
            current: formatImgUrl(''), // 当前显示
            urls: [formatImgUrl('')] // 需要预览的图片http链接列表
        })
    }
    return (
        <ContentBox title="售后图片">
            <View className={styles.after_sale_picture_list}>
                <View className={styles.after_sale_picture_item} onClick={showImage}>
                    <Image src={formatImgUrl('')} />
                </View>
                <View className={styles.after_sale_picture_item} onClick={showImage}>
                    <Image src={formatImgUrl('')}/>
                </View>
                <View className={styles.after_sale_picture_item}>
                    <Image src={formatImgUrl('')}/>
                </View>
                <View className={styles.after_sale_picture_item}>
                    <Image src={formatImgUrl('')}/>
                </View>
                <View className={styles.after_sale_picture_item}>
                    <Image src={formatImgUrl('')}/>
                </View>
                <View className={styles.after_sale_picture_item}>
                    <Image src={formatImgUrl('')}/>
                </View>
            </View>
        </ContentBox>
    )
 })
