import React, { Component } from 'react';
import './index.scss'
import { HeaderBar, CallApp, CaptchaAli } from "@common/index"
import { http } from "@/utils"
import { Toast } from "antd-mobile";
import { Link } from 'react-router-dom'


class Problems extends Component {

  state = {
    isShowAnswer: false,
    isShowCaptcha: false,
    instance: null,
    problem: {},
  }

  componentDidMount() {
    this.getProblem()
  }

  componentDidUpdate(prevProps, prevState) {
    if (prevProps.match.params.id !== this.props.match.params.id) {
      this.setState({
        problem: {},
      });
      this.getProblem()
    }
  }


  onVerify = data => {
    http.post(`${API.home}/m/safety`, data)
      .then(res => {
        const {code} = res.data
        if (code === 200) {
          this.setState({
            isShowCaptcha: false,
          })
          this.getProblem()
        } else {
          this.state.instance && this.state.instance.reset()
        }
      })
  }

  getInstance = (instance) => {
    this.setState({
      instance,
    });
  }

  getProblem = () => {
    http.get(`${API.home}/m/question/${this.props.match.params.id}`)
      .then(res => {
        const {code, msg, data} = res.data
        if (code === 200) {

          this.setState({
            problem: data,
          });

        } else if (code === 1) {
          this.setState({
            isShowCaptcha: true,
          });
        } else {
          Toast.info(msg)
        }
      })
  }


  render() {
    const {isShowAnswer, problem: {quesInfo, between}, isShowCaptcha} = this.state
    return (
      quesInfo || isShowCaptcha ? <article id={'problems'}>
        {
          !isShowCaptcha ?
            <>
              <HeaderBar title={quesInfo && quesInfo.category} arrow={true} cart={false}></HeaderBar>
              <div className="topic">
                <span>{quesInfo && quesInfo.order_id}.</span>
                <span dangerouslySetInnerHTML={{__html: quesInfo && quesInfo.ques}}></span>
              </div>
              {
                !isShowAnswer &&
                <div className="btn" onClick={() => {
                  this.setState({
                    isShowAnswer: true,
                  });
                }}>
                  <button>查看解析 <i className={'iconfont iconiconfront-69'}></i></button>
                </div>
              }
              {
                isShowAnswer &&
                <div className={`content ${isShowAnswer ? 'slideInDown' : ''}`}>
                  <div className={'title'}>解析</div>
                  <span dangerouslySetInnerHTML={{__html: quesInfo.analysis}}></span>
                </div>
              }
              <footer>
                <div className="app">更多题目请 <CallApp text={'前往APP'}/></div>
                <div className="op">
                  {
                    between && !!between.up && <Link to={`/problems/${between.up}`} className={'nav'}>上一题</Link>
                  }
                  {
                    between && !!between.down && <Link to={`/problems/${between.down}`} className={'nav'}>下一题</Link>
                  }
                </div>
              </footer>
            </>
            : <div className="captcha-container">
              <div>亲，系统正忙，滑动一下马上回来</div>
              <CaptchaAli getInstance={this.getInstance} onVerify={this.onVerify}/>
            </div>
        }
      </article> : null

    );
  }
}

export default Problems;