import React, { Component } from 'react'
import { connect } from 'react-redux';
import { http, getParam } from '@/utils'
import PythonDes from './pythomDes'
import PythonStudy from './pythonStudy'

@connect(state => ({
  user: state.user
}))
class Python extends Component {
  constructor(props) {
    super(props);
    this.state = {
      isPay: 0,
      syllabus: [], // 阶梯学习
      allSyllabus: [], // 阶梯学习
      allSyllabusShow: false,
      practice: [], // 课后实操
      allPractice: [], // 课后实操
      allPracticeShow: false,
    }
  }

  componentDidMount() {
    this.fetchCourseInfo();
  }

  showAll = (key) => {
    if(key === 1) {
      this.setState({
        syllabus: this.state.allSyllabus,
        allSyllabusShow: true,
      })
    }
    if(key === 2) {
      this.setState({
        practice: this.state.allPractice,
        allPracticeShow: true,
      })
    }
  }
  hideSome = (key) => {
    if(key === 1) {
      this.setState({
        syllabus: this.state.allSyllabus.slice(0, 2),
        allSyllabusShow: false,
      })
    }
    if(key === 2) {
      this.setState({
        practice: this.state.allPractice.slice(0, 2),
        allPracticeShow: false,
      })
    }
  }
  fun = (fun, key) => {

  }

  fetchCourseInfo = () => {
    const id = getParam('id');
    http.get(`${API.home}/m/course/detail/${id}`).then((res) => {
      const { data, code } = res.data;
      if (code === 200) {
        this.setState({
          // TODO
          // isPay: data.course_info.is_pay
        })
      }
    });

    http.get(`${API['home']}/web/it_detail?course_id=180`).then(res => {
      const { code, data, msg } = res.data
      if (code == 200) {
        this.setState({
          syllabus: data.syllabus.slice(0, 2),
          allSyllabus: data.syllabus,
          practice: data.practice.slice(0, 2),
          allPractice: data.practice,
        })
      } else {
        Message.warning(msg);
      }
    });
  }

  render() {
    const { isPay, syllabus, allSyllabusShow, practice, allPracticeShow } = this.state;
    const { user: { hasError } } = this.props;
    return (
      <div>
        {
          isPay === 0 && <PythonDes syllabus={syllabus} practice={practice} allSyllabusShow={allSyllabusShow} allPracticeShow={allPracticeShow} show={this.showAll} hide={this.hideSome}></PythonDes>
        }
        {
          isPay === 1 && <PythonStudy></PythonStudy>
        }
      </div>
    )
  }
}

export default Python
