import React from 'react';
import './index.less';
import Page from '@src/containers/Page';
import Block from '@src/components/Block';
import FilterLayout from '@src/layouts/FilterLayout';
// import ActionLayout from '@src/layouts/ActionLayout';
import TableLayout from '@src/layouts/TableLayout';
import { getMap, bindSearch, formatDate } from '@src/services/Tools';
import { Exercise } from '../../../stores/exercise';
import { Preview } from '../../../stores/preview';
import { User } from '../../../stores/user';
// import { System } from '../../../stores/system';

export default class extends Page {
  constructor(props) {
    super(props);
    this.categoryMap = {};
    this.filterF = null;
  }

  init() {
    this.filterForm = [
      {
        key: 'category',
        type: 'select',
        allowClear: true,
        name: '课程',
        placeholder: '请选择',
        number: true,
        onChange: (value) => {
          this.refreshPreview(value);
          this.filterF.setFieldsValue({ preview_id: '' });
        },
      },
      {
        key: 'preview_id',
        type: 'select',
        name: '练习册',
        allowClear: true,
        select: [],
        number: true,
        placeholder: '请选择',
      },
      {
        key: 'user_id',
        type: 'select',
        name: '用户',
        allowClear: true,
        select: [],
        number: true,
        placeholder: '请输入',
      },
      {
        key: 'time',
        type: 'daterange',
        name: '做题时间',
      },
    ];
    this.columns = [{
      title: '用户id',
      dataIndex: 'userId',
      render: (text, record) => {
        return record.user.id;
      },
    }, {
      title: '用户名称',
      dataIndex: 'user.realName',
      render: (text, record) => {
        return record.user.realName;
      },
    }, {
      title: '做题时间',
      dataIndex: 'createTime',
      render: (text) => {
        return formatDate(text);
      },
    }, {
      title: '学科',
      dataIndex: 'category',
      render: (text) => {
        return this.categoryMap[text] || text;
      },
    }, {
      title: '练习册',
      dataIndex: 'title',
      render: (text, record) => {
        return record.preview.title;
      },
    }, {
      title: '完成进度',
      dataIndex: 'finish',
      render: (text, record) => {
        return `${record.report.userNumber * 100 / record.report.totalNumber}`;
      },
    }, {
      title: '错误率',
      dataIndex: 'correct',
      render: (text, record) => {
        return `${record.report.correctNumber * 100 / record.questionNo.totalNumber}%`;
      },
    }, {
      title: '时长',
      dataIndex: 'time',
      render: (text, record) => {
        return `${record.report.userTime}s`;
      },
    }, {
      title: '操作',
      dataIndex: 'handler',
      render: (text, record) => {
        return <div className="table-button">
          {(
            <a href={`/subject/preview/detail/${record.id}`} target='_blank'>查看</a>
          )}
        </div>;
      },
    }];
    Exercise.allStruct().then(result => {
      this.filterForm[0].select = result.filter(row => row.level === 2).map(row => { row.title = `${row.titleZh}/${row.titleEn}`; row.value = row.id; return row; });
      this.categoryMap = getMap(this.filterForm[0].select, 'id', 'title');
      this.setState({ exercise: result });
    });
    bindSearch(this.filterForm, 'userId', this, (search) => {
      return User.list(search);
    }, (row) => {
      return {
        title: `${row.nickname}(${row.mobile})`,
        value: row.id,
      };
    }, this.state.search.userId ? Number(this.state.search.userId) : null, null);
    this.refreshPreview();
  }

  // 筛选条件
  refreshPreview(category) {
    Preview.list({ category })
      .then(result => {
        this.filterForm[1].select = result.list.map(row => { row.value = row.id; row.label = row.title; return row; });
        this.setState({ load: false });
      });
  }

  initData() {
    const { search } = this.state;
    const data = Object.assign({}, search);
    if (data.time) {
      data.startTime = data.time[0] || '';
      data.endTime = data.time[1] || '';
    }
    Preview.list(data).then(result => {
      this.setTableData(result.list, result.total);
    });
  }

  renderView() {
    return <Block flex>
      <FilterLayout
        show
        itemList={this.filterForm}
        data={this.state.search}
        onChange={data => {
          if (data.time.length > 0) {
            data.time = [data.time[0].format('YYYY-MM-DD HH:mm:ss'), data.time[1].format('YYYY-MM-DD HH:mm:ss')];
          }
          this.search(data);
        }}
        ref={(ref) => {
          if (ref) this.filterF = ref;
        }} />
      <TableLayout
        select
        columns={this.columns}
        list={this.state.list}
        pagination={this.state.page}
        loading={this.props.core.loading}
        onChange={(pagination, filters, sorter) => this.tableChange(pagination, filters, sorter)}
        onSelect={(keys, rows) => this.tableSelect(keys, rows)}
        selectedKeys={this.state.selectedKeys}
      />
    </Block>;
  }
}
