import React from 'react';
import { Button } from 'antd';
import { Link } from 'react-router-dom';
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 { asyncSMessage } from '@src/services/AsyncTools';
import { TextbookType } from '../../../../Constant';
import { Exercise } from '../../../stores/exercise';
// import { System } from '../../../stores/system';
import { Question } from '../../../stores/question';
// import { Slient } from '../../../stores/slient';
// import Association from '../../../components/Association';

const TextbookTypeMap = getMap(TextbookType, 'value', 'label');

const filterForm = [
  {
    key: 'type',
    type: 'select',
    allowClear: true,
    name: '题型',
    select: TextbookType,
    placeholder: '请选择',
    number: true,
  },
  {
    key: 'paperId',
    type: 'select',
    allowClear: true,
    name: '练习册',
    select: [],
    placeholder: '请选择',
    number: true,
  },
  {
    key: 'questionNoId',
    type: 'select',
    allowClear: true,
    name: '题目ID',
    select: [],
    number: true,
    placeholder: '请输入',
  },
];
export default class extends Page {
  constructor(props) {
    super(props);
    this.actionList = [{
      key: 'add',
      name: '新建',
      render: (item) => {
        return <Button onClick={() => {
          linkTo('/subject/textbook/question');
        }}>{item.name}</Button>;
      },
    }];
    this.categoryMap = {};
    this.columns = [{
      title: '题型',
      dataIndex: 'type',
      render: (text, record) => {
        return TextbookTypeMap[record.question.type] || text;
      },
    }, {
      title: '练习册',
      dataIndex: 'paper',
      render: (text) => {
        return text.title;
      },
    }, {
      title: '题目ID',
      dataIndex: 'title',
    }, {
      title: '修改时间',
      dataIndex: 'updateTime',
      render: (text, record) => {
        return formatDate(record.question.updateTime);
      },
    }, {
      title: '操作',
      dataIndex: 'handler',
      render: (text, record) => {
        return <div className="table-button">
          {(
            <Link to={`/subject/text/question/${record.id}`}>编辑</Link>
          )}
        </div>;
      },
    }];
  }

  init() {
    bindSearch(filterForm, 'paperId', this, (search) => {
      return Exercise.listPaper(search);
    }, (row) => {
      return {
        title: row.title,
        value: row.id,
      };
    }, this.state.search.paperId ? Number(this.state.search.paperId) : null, null);
    bindSearch(filterForm, 'questionNoId', this, (search) => {
      return Question.searchNo(search);
    }, (row) => {
      return {
        title: row.no,
        value: row.id,
      };
    }, this.state.search.questionNoId ? Number(this.state.search.questionNoId) : null, null);
  }

  initData() {
    const { search } = this.state;
    const data = Object.assign({}, search);
    Exercise.listQuestion(data).then(result => {
      this.setTableData(result.list, result.total || 1);
    });
  }

  renderView() {
    return <Block flex>
      <FilterLayout
        show
        itemList={filterForm}
        data={this.state.search}
        onChange={data => {
          this.search(data);
        }} />
      <ActionLayout
        itemList={this.actionList}
        selectedKeys={this.state.selectedKeys}
        onAction={key => this.onAction(key)}
      />
      <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}
      />
      {/* {this.state.detail && <Association {...this.state.detail} />} */}
    </Block>;
  }
}
