import React, {Component} from 'react'
import {VList} from '../../common'
import {Tabs, WhiteSpace} from 'antd-mobile'
import './index.scss'
import HeaderSearch from '../../common/HeaderSearch/index'
import {http, api} from "@/utils"
import {Link} from 'react-router-dom'
import {Toast} from 'antd-mobile'

class Preferential extends Component {
    constructor(props) {
        super(props)
        this.state = {
            dataList: [],
            courseStatus: 0,
        }
    }

    componentDidMount() {
        this.specialSale()
    }

    // 限时特惠
    specialSale = () => {
        http.get(`${api.home}/m/home/weekDiscounts`).then((res) => {
            if (res.data.code === 200) {
                this.setState({
                    dataList: res.data.data
                })
            } else {
                Toast.info(res.data.msg, 2)
            }

        })
    }
    // 砍价专区
    bargain = () => {
        http.get(`${api.home}/m/home/bargainZone`).then((res) => {
            if (res.data.code === 200) {
                this.setState({
                    dataList: res.data.data
                })
            } else {
                Toast.info(res.data.msg, 2)
            }
        })
    }
    // 一键拼团
    group = () => {
        http.get(`${api.home}/m/home/grouponList`).then((res) => {
            if (res.data.code === 200) {
                this.setState({
                    dataList: res.data.data
                })
            } else {
                Toast.info(res.data.msg, 2)
            }
        })
    }
    // tab 切换
    ontabclick = (tab, index) => {
        console.log(tab, index)
        this.state.courseStatus = index
        switch (index) {
            case 0:
                this.specialSale()
                break
            case 1:
                this.bargain()
                break
            default:
                this.group()
        }
    }

    handleClick = (courseId) => {
        console.log(courseId)
    }

    render() {
        const tabs = [
            {title: '限时特惠'},
            {title: '砍价专区'},
            {title: '一键拼团'}
        ]
        return (
            <div className='preferential'>
                <HeaderSearch></HeaderSearch>
                <div className='class-content'>
                    <WhiteSpace/>

                    <Tabs
                        tabs={tabs}
                        animated={false}
                        onChange={(tab, index) => this.ontabclick(tab, index)}
                    >
                        <div className='tabs'>
                            <ul>
                                {this.state.dataList.map((item, index) => {
                                    const Info = (
                                        <div className="info">
                                            <p className='title'>
                                                <Link to={`/detail?id=${item.course_id}`}>
                                                    {item.course_title}
                                                </Link>
                                            </p>
                                            <p className='contact text-overflow-2'>{item.course_desc}</p>
                                            <div className='des'>
                                                {!item.is_buy &&
                                                <p className="course-price">
                                                    {this.state.courseStatus === 0 &&
                                                    <span className='price'>特惠价：</span>
                                                    }
                                                    <span className="new">¥{item.price1}</span>
                                                    <span className="old">¥{item.price0}</span>
                                                </p>
                                                }
                                                {item.is_buy &&
                                                <a href="/#" className="isbuy">已购买</a>
                                                }
                                            </div>
                                        </div>
                                    )
                                    const status = (
                                        <div>
                                            {this.state.courseStatus === 1 &&
                                            <p className='course-status'>砍价减{item.price1}元</p>
                                            }
                                            {this.state.courseStatus === 2 &&
                                            <p className='course-status'>拼团减{item.price}元</p>
                                            }
                                        </div>
                                    )
                                    return (
                                        <VList handleClick={this.handleClick} key={index} img={item.image_name}
                                               id={item.course_id} status={status}
                                               info={Info}></VList>
                                    )
                                })}
                            </ul>
                        </div>
                    </Tabs>
                    <WhiteSpace/>
                </div>
            </div>
        )
    }

}

export default Preferential
