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 { System } from '../../../stores/system';
import { Textbook } from '../../../stores/textbook';
// import { Slient } from '../../../stores/slient';

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

const filterForm = [
  {
    key: 'questionType',
    type: 'select',
    allowClear: true,
    name: '题型',
    select: TextbookType,
    placeholder: '请选择',
    number: true,
  },
  {
    key: 'paperId',
    type: 'select',
    allowClear: true,
    name: '练习册',
    select: [],
    placeholder: '请选择',
    number: true,
  },
  {
    key: 'id',
    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: 'questionType',
      render: (text, record) => {
        return TextbookTypeMap[record.question.questionType] || text;
      },
    }, {
      title: '练习册',
      dataIndex: 'paper.title',
    }, {
      title: '题目ID',
      dataIndex: 'title',
    }, {
      title: '修改时间',
      dataIndex: 'updateTime',
      render: (text, record) => {
        return formatDate(record.question.updateTime, 'YYYY-MM-DD HH:mm:ss');
      },
    }, {
      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 Textbook.listPaper(search);
    }, (row) => {
      return {
        title: row.title,
        value: row.id,
      };
    }, this.state.search.paperId ? Number(this.state.search.paperId) : null, null);
    bindSearch(filterForm, 'id', this, (search) => {
      return Textbook.searchQuestion(search);
    }, (row) => {
      return {
        title: row.title,
        value: row.id,
      };
    }, this.state.search.id ? Number(this.state.search.id) : null, null);
  }

  initData() {
    const { search } = this.state;
    const data = Object.assign({}, search);
    Textbook.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 => {
          data.page = 1;
          this.search(data);
        }} />
      <ActionLayout
        itemList={this.actionList}
        selectedKeys={this.state.selectedKeys}
        onAction={key => this.onAction(key)}
      />
      <TableLayout
        columns={this.tableSort(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>;
  }
}