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 { Course } from '../../../stores/course';

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, 'YYYY-MM-DD HH:mm:ss')} - ${formatDate(row.expireTime, 'YYYY-MM-DD HH:mm:ss')}`;
          }).join(<br />);
        },
      },
      {
        title: '创建时间',
        dataIndex: 'createTime',
        render: (text) => {
          return formatDate(text, 'YYYY-MM-DD HH:mm:ss');
        },
      }, {
        title: '操作',
        dataIndex: 'handler',
        render: (text, record) => {
          return <div className="table-button">
            {(
              <Link to={`/user/detail/${record.id}`}>查看</Link>
            )}
          </div>;
        },
      },
    ];
    Course.list({ 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 => {
          data.page = 1;
          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.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>;
  }
}