import React, {Component} from 'react'
import './index.scss'
import {HeaderBar} from '../../common'
import OrderList from '@/common/OrderList'
import {http, api} from "@/utils"
import {Link} from 'react-router-dom'
import {Modal, Toast} from 'antd-mobile'
import Loading from '@/common/Loading'

const alert = Modal.alert


class MyOrders extends Component {
    constructor(props) {
        super(props)
        this.state = {
            data: [],
            isLoading: true
        }
    }

    componentDidMount() {
        this.getList()
    }

    // 获取订单
    getList = () => {
        http.get(`${API.home}/m/my/orders/1/1`,).then((res) => {
            if (res.data.code === 200) {
                this.setState({
                    data: res.data.data,
                    isLoading: false
                })
            } else {
                Toast.info(res.data.msg, 2);
            }
        })
    }

    // 取消订单
    cancel = (oid) => {
        alert('确认取消订单？', '订单取消后，优惠券和抵扣的余额会返回到您的账户。', [
            {text: '取消', onPress: () => console.log('cancel')},
            {
                text: '确认',
                onPress: () => {
                    let data = {
                        order_id: oid
                    }
                    http.post(`${API.home}/m/cancel_order`, data).then((res) => {
                        if (res.data.code === 200) {
                            this.getList()
                        } else {
                            Toast.info(res.data.msg, 2);
                        }
                    })
                }
            }
        ])
    }


    render() {

        return (
            <div className='myorders-box'>
                <HeaderBar title='我的订单' arrow={true} cart={false}></HeaderBar>
                <Loading isLoading={this.state.isLoading}>
                    {
                        this.state.data && this.state.data.length > 0 ?
                            this.state.data.map((item, index) => {
                                return (
                                    <div className="order-body" key={index}>
                                        <OrderInfo item={item}/>
                                        {
                                            item.course && item.course.length > 0 && item.course.map((item, index) => {
                                                const Info = (
                                                    <div className="order-info">
                                                        <p className='order-title text-overflow-one'>
                                                            <Link
                                                                to={`/detail?id=${item.course_id}`}>{item.course_title}</Link>
                                                        </p>
                                                        <p className='order-content text-overflow-2'>{item.description}</p>
                                                        <p className='order-des'>
                                                            <span className='order-newprice'>¥{item.pay_amount}</span>
                                                            <span className='order-price'>¥{item.price0}</span>
                                                        </p>
                                                    </div>
                                                )
                                                return (
                                                    <div className="order-wrap" key={index}>
                                                        <OrderList info={Info} src={item.image_name}
                                                                   id={item.course_id}></OrderList>
                                                    </div>
                                                )
                                            })
                                        }

                                        <PayInfo item={item} cancel={this.cancel}/>
                                    </div>
                                )
                            }) : <div className="cart-tip">
                                <p className='cart-mess'>您还没有订单哦，快去逛逛吧~</p>
                                <Link to='/classify'>去逛逛</Link>
                            </div>
                    }
                </Loading>
            </div>
        )
    }
}


function OrderInfo(props) {
    let btn
    if (props.item.pay_time === '0' && props.item.member_num === 0) {
        btn = <span className='oid-status'>等待支付</span>
    } else if ((props.item.member_num === props.item.pdd_info.length) && props.item.member_num !== 0) {
        btn = <span className='oid-success'>拼团成功</span>
    } else {
        btn = <span className='oid-success'>支付成功</span>
    }
    return (
        <div className='order-head'>
            <span className='oid-num'>订单号：{props.item.oid}</span>
            {btn}
        </div>
    )
}

function PayInfo(props) {
    return (
        <div className='order-btm'>
            <div className='price-info'>
                <p>
                    <span className='payable'>应付：</span>
                    <span className='price'>¥{props.item.pay_amount}</span>
                </p>
                <p>
                    <span className='payable'>已优惠：</span>
                    <span className='price'>¥{props.item.coupon_amount}</span>
                </p>
            </div>

            {
                props.item.pay_time === '0' && props.item.member_num === 0 &&
                <div className='btm-right'>
                    <button className='cancel' onClick={event => props.cancel(props.item.oid)}>取消订单</button>
                    <Link to={`/payorder?oid=${props.item.oid}`}>去支付</Link>
                </div>
            }

            {
                props.item.member_num !== 0 &&
                <div className='btm-right'>
                    <div className="group">
                        {
                            props.item.pdd_info && props.item.pdd_info.length > 0 && props.item.pdd_info.map((item, index) => {
                                return (
                                    <img
                                        src={item.user_avatar === '' ? 'https://julyedu-cdn.oss-cn-beijing.aliyuncs.com/tinypng-spreadtrain8/ellipsis.png' : item.user_avatar}
                                        key={index} alt=""/>
                                )
                            })
                        }
                    </div>
                </div>
            }
        </div>
    )
}

export default MyOrders
