import React from 'react';
import { Form, Button, Row, Col, List, Icon, Switch, Typography } from 'antd';
import './index.less';
import Editor from '@src/components/Editor';
import Page from '@src/containers/Page';
import Block from '@src/components/Block';
import DragList from '@src/components/DragList';
// import FileUpload from '@src/components/FileUpload';
import { formatFormError, formatDate, getMap } from '@src/services/Tools';
import { asyncSMessage } from '@src/services/AsyncTools';
import { AskTarget, QuestionType } from '../../../../Constant';
import { User } from '../../../stores/user';

const QuestionTypeMap = getMap(QuestionType, 'value', 'label');
const AskTargetMap = getMap(AskTarget, 'value', 'label');
export default class extends Page {
  init() {
    // Exercise.allStruct().then(result => {
    //   result = result.filter(row => row.level === 2).map(row => { row.title = `${row.titleZh}/${row.titleEn}`; row.value = row.id; return row; });
    //   this.setState({ exercise: result });
    // });
  }

  initData() {
    const { id } = this.params;
    let handler;
    if (id) {
      handler = User.getAsk({ id });
    } else {
      handler = Promise.resolve({ others: [{ content: 123123123, answer: 123123 }, {}] });
    }
    handler
      .then(result => {
        const { getFieldDecorator, setFieldsValue } = this.props.form;
        getFieldDecorator('id');
        getFieldDecorator('answer');
        getFieldDecorator('showStatus');
        setFieldsValue({ id: result.id, answer: result.answer, showStatus: result.showStatus });
        this.setState({ data: result });
      });
  }

  orderQuestion(oldIndex, newIndex) {
    const { data } = this.state;
    const { other = [] } = data;
    const tmp = other[oldIndex];
    other[oldIndex] = other[newIndex];
    other[newIndex] = tmp;
    this.setState({ other });
  }

  submit() {
    const { form } = this.props;
    form.validateFields((err) => {
      if (!err) {
        const data = form.getFieldsValue();
        data.other = this.state.data.others.map(row => row.id);
        User.editAsk(data).then(() => {
          asyncSMessage('保存成功');
        }).catch((e) => {
          if (e.result) form.setFields(formatFormError(data, e.result));
        });
      }
    });
  }

  renderBase() {
    const { data } = this.state;
    const { question = {}, questionNo = {} } = data;
    return <Block>
      <h1>题目信息</h1>
      <Form>
        <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='题型'>
          {QuestionTypeMap[question.type]}
        </Form.Item>
        <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='题目id'>
          <a href='' target='_blank'>{questionNo.no}</a>
        </Form.Item>
      </Form>
    </Block>;
  }

  renderAsk() {
    const { data } = this.state;
    const { user = {}, createTime, target, content } = data;
    return <Block>
      <h1>提问信息</h1>
      <Form>
        <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='用户姓名'>
          {user.realName}
        </Form.Item>
        <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='提问时间'>
          {formatDate(createTime)}
        </Form.Item>
        <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='提问模块'>
          {AskTargetMap[target]}
        </Form.Item>
        <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='提问详情'>
          {content}
        </Form.Item>
      </Form>
    </Block>;
  }

  renderQuestionList() {
    return <Block>
      <h1>该题目的展示中问题</h1>
      <DragList
        loading={this.props.core.loading}
        dataSource={this.state.data.others || []}
        handle={'.icon'}
        onMove={(oldIndex, newIndex) => {
          this.orderQuestion(oldIndex, newIndex);
        }}
        renderItem={(item) => (
          <List.Item actions={[<Icon type='bars' className='icon' />, <Typography.Text copyable={{ text: 'url' }} />]}>
            <Row style={{ width: '100%' }}>
              <Col span={11}>问题：{item.content}</Col>
              <Col span={11} offset={1}>答复：{item.answer}</Col>
            </Row>
          </List.Item>
        )}
      /></Block>;
  }

  renderAnswer() {
    const { getFieldDecorator } = this.props.form;
    return <Block>
      <Form>
        {getFieldDecorator('id')(<input hidden />)}
        <Form.Item label='教师回复'>
          {getFieldDecorator('answer', {
          })(
            <Editor placeholder='输入内容' />,
          )}
        </Form.Item>
        <Row type="flex" justify="center">
          <Col span={12}>
            <Form.Item labelCol={{ span: 12 }} wrapperCol={{ span: 10 }} label='显示状态'>
              {getFieldDecorator('showStatus', {
                valuePropName: 'checked',
              })(
                <Switch checkedChildren='on' unCheckedChildren='off' />,
              )}
            </Form.Item>
          </Col>
        </Row>
      </Form>
    </Block>;
  }

  renderView() {
    return <div flex>
      {this.renderBase()}
      {this.renderAsk()}
      {this.renderQuestionList()}
      {this.renderAnswer()}

      <Row type="flex" justify="center">
        <Col>
          <Button type="primary" onClick={() => {
            this.submit();
          }}>保存</Button>
        </Col>
      </Row>
    </div>;
  }
}
