import React, { PureComponent } from 'react'
import { Tag } from '../../../common'
import Course from '@/common/course-base'
import { http, api } from '@/utils'
import { Toast } from "antd-mobile";
import classnames from 'classnames'
import {isEmpty} from 'lodash'


import './free-courses.scss'

const Bottom = (props) => (
    <div className='bottom'>
        <Tag className={props.className}>{props.text}</Tag>
        <span>{props.audience}人观看</span>
    </div>
)

class FreeCourse extends PureComponent {
    state = {
        courses: [],
        live: [],
        page: 1,
        num: 10
    }

    componentDidMount() {
        this.getFreeCourses()
            .then(res => {
                let data = res.data
                if (data.code == 200) {
                    this.setState({
                        courses: data.data,
                    })
                } else {
                    Toast.info(data.msg)
                }
            })
        this.getFreeLive()
            .then(res => {
                let data = res.data
                if (data.code == 200) {
                    this.setState({
                        live: isEmpty(data.data) ? [] : data.data
                    })
                } else {
                    Toast.info(data.msg, 2, null, false)
                }
            })
    }

    handleClick = id => {
        this.props.history.push(`/play?id=${id}`)
    }

    getFreeCourses = () => {
        return http.get(`${api.home}/m/free_course/${this.state.page}/${this.state.num}`)
    }

    getFreeLive = () => {
        return http.get(`${api.home}/m/live/free_list`)
    }

    render() {
        return (
            <ul className='free-courses'>
                {
                    this.state.live.map((item, index) => {
                        const Bottom = (
                            <div className="bottom">
                                <div className="animation-box">
                                    {new Array(4).fill('a').map((item, index) => {
                                        return <i key={index} className={classnames('column', `column-${index + 1}`)}/>
                                    })}
                                </div>
                                <div className="time">{`${item['live_start_time']}`}</div>
                            </div>
                        )
                        return (
                            <Course
                                img={item['live_cover']}
                                title={item['live_title']}
                                top={
                                    <Tag className={'tag-starting top'}>即将开始</Tag>
                                }
                                bottom={Bottom}
                                key={item['live_id']}
                                className={'live'}
                                handleClick={this.handleClick}
                                id={index}
                            />
                        )
                    })
                }
                {
                    this.state.courses.map((item, index) => (
                        <Course
                            img={item.logo}
                            title={item['video_course_name']}
                            handleClick={this.handleClick}
                            bottom={
                                <Bottom audience={item['play_times']} className={'tag-category'} text={item.category}/>
                            }
                            id={item['v_course_id']}
                            key={index}
                            className={'course-item'}
                        />
                    ))
                }
            </ul>
        )
    }
}


export default FreeCourse