import React, { Component } from 'react';
import './index.scss'
import { HeaderBar } from "@common/index"
import Question from "@components/ai-test/common/question"
import { html, http } from "@/utils"
import { Toast } from "antd-mobile";
import { Link } from "react-router-dom";
import { connect } from "react-redux";
import Recommends from "@components/ai-test/common/recommends"
import classnames from 'classnames'


class Assist extends Component {
  state = {
    question: null,
    answer: {},
    result: null,
    rightAnswer: '',
    userAnswer: '',
  }

  componentDidMount() {
    this.getData()
  }

  getData = () => {
    http.get(`${API.home}/sys/aitest/assist`)
      .then(res => {
        const {code, msg, data} = res.data
        if (code === 200) {
          const answer = {
            [data.id]: undefined,
          }
          this.setState({
            question: data,
            answer,
          });
        } else {
          Toast.fail(msg, 2, null, false)
        }
      })
  }

  selectAnswer = (question, optionId) => {
    this.setState({
      answer: {
        [question.id]: optionId,
      },
    });
  }

  submit = () => {
    const {history, user} = this.props
    if (user.hasError) {
      history.push('/passport')
      return
    }
    const {question, answer} = this.state
    if (!answer[question.id]) {
      Toast.info('请选择后进提交', 2, null, false)
      return
    }
    http.post(`${API.home}/sys/aitest/assistSubmit`, {
      code: this.props.match.params.assistCode,
      question_id: question.id,
      answer_id: answer[question.id],
    }).then(res => {
      const {code, msg, data} = res.data
      this.setAnswer(data.correct_answer)
      if (code === 200) {
        this.setState({
          result: data,
        });
      } else {
        Toast.fail(msg, 2, null, false)
      }
    })
  }

  setAnswer = (rightAnswerId) => {
    this.setState(state => {
      let rightAnswer = '', userAnswer = ''
      const question = {
        ...state.question, ...{
          options: state.question.options.map((item, index) => {
            if (item.id === rightAnswerId) {
              item.is_ans = 1
              rightAnswer = String.fromCharCode(65 + index)
            }
            if (item.id === state.answer[state.question.id]) {
              userAnswer = String.fromCharCode(65 + index)
              item.user_select = 1
            }
            return item
          }),
        },
      }
      return {
        question,
        rightAnswer,
        userAnswer,
      }
    });
  }

  render() {
    const {question, answer, result, rightAnswer, userAnswer} = this.state
    return (
      <div className={'assist'}>
        <HeaderBar title={'AI水平测试'} arrow={true}/>
        {
          question &&
          <Question question={question} category={'机器学习'} answer={answer} selectAnswer={!result && this.selectAnswer}/>
        }
        <div style={{height: '8px', backgroundColor: '#f5f5f5'}}></div>
        {
          result && <div className="content">
            <div class={'info'}>您的选择是{userAnswer}，正确答案是{rightAnswer} 回答{userAnswer === rightAnswer ? '正确' : '错误'}</div>
            <div className="head">
              <i className="icon"></i>
              <span>解析</span>
            </div>
            <div className="analysis-content" dangerouslySetInnerHTML={html(result.analysis)}></div>
          </div>
        }
        {
          result && <>
            <Recommends typeId={question.type_id}/>
            <div className={classnames(['status', {
              end: result.status === 6,
              success: result.status === 1,
              error: result.status === 2 || result.status === 3 || result.status === 4 || result.status === 5,
            }])}>
              {result.desc}
            </div>
          </>
        }

        <div className="btns">
          {
            !result && <button className={'submit'} onClick={this.submit}>提交</button>
          }
          {
            result && (result.status === 6
              ? <Link to={'/'} class={'home'}>返回首页</Link>
              : <Link to={'/ai-test/scores'} class={'test'}>我也要测试</Link>)
          }
        </div>
      </div>
    );
  }
}

export default connect(
  state => state.user,
  null,
)(Assist)