import React from 'react';
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, formatDate } from '@src/services/Tools';
// import { SwitchSelect, ServiceKey } from '../../../../Constant';
import { User } from '../../../stores/user';
import { Class } from '../../../stores/class';

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

  init() {
    this.filterForm = [
      {
        key: 'keyword',
        type: 'input',
        name: 'ID/手机号',
        placeholder: '请输入',
      },
      {
        key: 'courseId',
        type: 'select',
        allowClear: true,
        name: '课程',
        select: [],
        placeholder: '请选择',
        number: true,
      },
    ];
    this.columns = [
      {
        title: '手机号',
        dataIndex: 'mobile',
      },
      {
        title: '用户姓名',
        dataIndex: 'realName',
      },
      {
        title: '开通科目',
        dataIndex: 'classes',
        render: (text, record) => {
          return (record.classes || []).map(row => {
            return this.courseMap[row.courseId];
          }).join(<br />);
        },
      },
      {
        title: '开通时间',
        dataIndex: 'time',
        render: (text, record) => {
          return (record.classes || []).map(row => {
            return `${formatDate(row.startTime)} - ${formatDate(row.expireTime)}`;
          }).join(<br />);
        },
      }, {
        title: '操作',
        dataIndex: 'handler',
        render: (text, record) => {
          return <div className="table-button">
            {(
              <Link to={`/user/detail/${record.id}`}>查看</Link>
            )}
          </div>;
        },
      },
    ];
    Class.listCourse({ size: 100 }).then(result => {
      this.filterForm[1].select = result.list.map(row => {
        return {
          title: row.title,
          value: row.id,
        };
      });
      this.courseMap = getMap(result.list, 'id');
    });
  }

  initData() {
    User.listStudent(this.state.search).then(result => {
      this.setTableData(result.list, result.total);
    });
  }

  renderView() {
    return <Block flex>
      <FilterLayout
        show
        itemList={this.filterForm}
        data={this.state.search}
        onChange={data => {
          this.search(data);
        }}
        ref={(ref) => {
          if (ref) this.filterF = ref;
        }} />
      {/* <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}
      />
    </Block>;
  }
}
