import React from 'react';
import './index.less';
import Page from '@src/containers/Page';
import { sortListWithOrder } from '@src/services/Tools';
import { AskTarget } from '../../../../Constant';
import { Question } from '../../../stores/question';
import Detail from './detail';

export default class extends Page {
  initState() {
    return {
      step: 0,
      hideAnalysis: true,
      analysisTab: 'official',
      showAnswer: false,
      noteField: AskTarget[0].key,
      showIds: false,

      // question: {
      //   content: {
      //     typeset: 'one',
      //   },
      //   // questionType: 'awa',
      //   answer: {
      //     subject: [[{ text: 'like', uuid: 'hKyz' }]],
      //     options: ['parallel'],
      //   },
      //   stem: "<p><span uuid='kBJe'>I</span> <span uuid='hKyz'>like</span> <span uuid='fQXh'>book</span></p>",
      // },
      // userQuestion: {
      //   userAnswer: {
      //     subject: [{ text: 'I', uuid: 'kBJe' }],
      //     options: ['compare'],
      //   },
      //   no: 2,
      // },
      // paper: {
      //   title: '长难句练习',
      //   questionNumber: 20,
      // },
      // report: {
      //   paperModule: 'sentence',
      // },

    };
  }

  initData() {
    const { id } = this.params;
    const { search } = this.state;
    Question.getDetailById(id).then(userQuestion => {
      const { question, questionNos, paper, note, report, setting, questionStatus } = userQuestion;
      let { questionNo } = userQuestion;
      if (!questionNo) ([questionNo] = questionNos);
      if (!question.answer) question.answer = { questions: [] };
      if (!question.answerDistributed) question.answerDistributed = { questions: [] };
      if (!userQuestion.userAnswer) userQuestion.userAnswer = { questions: [] };
      if ((report.setting || {}).disorder) {
        const { content } = question;
        // 还原做题顺序
        content.questions.forEach((q, i) => {
          q.select = sortListWithOrder(q.select, setting.questions[i]);
        });
        question.answer.questions.forEach((q, i) => {
          Object.keys(q).forEach((k) => {
            if (q[k]) q[k] = sortListWithOrder(q[k], setting.questions[i]);
          });
        });
        question.answerDistributed.questions.forEach((q, i) => {
          Object.keys(q).forEach((k) => {
            if (q[k]) q[k] = sortListWithOrder(q[k], setting.questions[i]);
          });
        });
        userQuestion.userAnswer.questions.forEach((q, i) => {
          Object.keys(q).forEach((k) => {
            if (q[k]) q[k] = sortListWithOrder(q[k], setting.questions[i]);
          });
        });
      }
      // 只显示单个提问
      if (search.askId) {
        const askId = Number(search.askId);
        userQuestion.asks = (userQuestion.asks || []).filter(row => row.askId === askId);
      }
      this.setState({ userQuestion, question, questionNo, note, paper, report, questionNos, questionStatus });
    });
  }

  renderView() {
    return (
      <Detail {...this.state} detail flow={this} />
    );
  }
}