import React, {Component} from 'react'
import './index.scss'
import Bargain from './bargain'
import Group from './group'
import OutLine from './outline'
import {HeaderBar, CallApp} from '../../common'
import ShareRank from "./shareRank"
import Audition from "./audition"
import Single from "./single"
import BtnStatus from "./btnstatus"
import Carouselw from "./carousel"
import {connect} from "react-redux"
import {fetchCoursesListIfNeeded} from "./actions"
import {api, getParam, http} from "@/utils";
import {Toast} from "antd-mobile";


class Detail extends Component {

    constructor(props) {
        super(props)
        this.state = {
            isbuy: 0,
            isvip: 0,
            auditionBox: false,
            singleBox: false,
            shareRank: false,
            singMess: '',
            barInfo: '',
        }
    }

    componentDidMount() {
        this.props.fetchCoursesListIfNeeded()
        this.getBargainInfo()
    }

    componentDidUpdate() {
        // console.log(this.props.courseInfo.course_info)
    }

    // 点击子组件试听按钮
    toAudition = () => {
        this.setState(status => ({
            auditionBox: true,
        }));
    }
    // 点击子组件单集购买按钮
    toSingleset = (item) => {
        this.setState(status => ({
            singleBox: true,
            singMess: item
        }));
    }


    //获取砍价信息
    getBargainInfo = () => {
        let data = {
            courseId: getParam('id')
        }
        http.post(`${api.home}/m/bargain/courseDetail`, data).then((res) => {
            if (res.data.code === 200) {
                this.setState({
                    barInfo: res.data.data
                })
            } else {
                Toast.info(res.data.msg, 2)
            }
        })
    }


    // 自组件传给父组件的boxHide
    boxHide = (val) => {
        this.setState({auditionBox: val, singleBox: val})
    }

    render() {
        let courseInfo = ''
        let service = ''
        if (this.props.courseInfo.course_info) {
            courseInfo = this.props.courseInfo.course_info
            service = this.props.courseInfo.course_info.service
        }
        return (
            <div className='detail-box'>
                <CallApp className='toapp'></CallApp>
                <HeaderBar title='课程详情' arrow={true} cart={true}></HeaderBar>
                {/*弹幕*/}
                <Carouselw></Carouselw>
                {/*课程*/}
                <div className='course-content'>
                    <div className='cover'>
                        <img src={courseInfo.image_name}
                             alt=""/>
                    </div>
                    <div className="info">
                        <p className='title'>{courseInfo.course_title}</p>
                        <p className='contact text-overflow-2'>{courseInfo.simpledescription}</p>
                        <div className='des'>
                            {courseInfo.is_baoming === 0 && <p className="course-price">
                                <span className="new">¥{courseInfo.price1}</span>
                                <span className="old">¥{courseInfo.price0}</span>
                            </p>
                            }
                            {courseInfo.is_baoming === 1 &&
                            <a href="/#" className="isbuy">已购买</a>
                            }
                        </div>
                    </div>
                </div>
                {/*正常课程已购买时显示*/}
                {
                    courseInfo.is_baoming === 1 &&
                    <div className='group'>
                        上课QQ群：{courseInfo.course_qq}，加群请备注您的学号：{courseInfo.uid}
                    </div>
                }
                {/*vip课程显示*/}
                {
                    courseInfo.vip_range &&
                    <div className="vip">
                        <p>已开通年会员：{courseInfo.vip_range}</p>
                        <p>年会员QQ群：{courseInfo.course_qq}，加群请备注您的学号：{courseInfo.uid}</p>
                    </div>
                }

                {/*服务承诺*/}
                <div className='promise'>
                    <label>服务承诺</label>
                    <p>
                        {
                            service && service.length > 0 && service.map((item, index) => {
                                return (
                                    <span key={index}> {item} </span>
                                )
                            })
                        }
                    </p>
                </div>

                {/*试听*/}
                <Audition auditionBox={this.state.auditionBox} boxHide={this.boxHide}/>

                {/*单集购买*/}
                <Single singleBox={this.state.singleBox} boxHide={this.boxHide} data={this.state.singMess}
                        title={courseInfo.course_title}/>

                {/*分享赚钱*/}
                {
                    courseInfo.is_dist &&
                    <ShareRank/>
                }

                {/*拼团*/}
                {
                    courseInfo.group_status === 3 &&
                    <Group/>
                }


                {/*砍价*/}
                {
                    this.props.courseInfo && this.props.courseInfo.is_bargain &&
                    <Bargain/>
                }

                {/*课程介绍、大纲*/}
                <OutLine data={this.props.courseInfo} toAudition={this.toAudition} toSingleset={this.toSingleset}/>

                {/*课程按钮*/}
                <BtnStatus data={this.props.courseInfo} barInfo={this.state.barInfo} getBargainInfo={this.getBargainInfo}></BtnStatus>
            </div>
        )
    }

}

const mapStateToProps = state => ({
    courseInfo: state.courseInfo
})
const mapDispatchToProps = {
    fetchCoursesListIfNeeded
}


export default connect(mapStateToProps, mapDispatchToProps)(Detail)