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

const SwitchSelectMap = getMap(SwitchSelect, 'value', 'label');
const ServiceKeyMap = getMap(ServiceKey, 'value', 'label');
export default class extends Page {
  constructor(props) {
    super(props);
    this.filterF = null;
  }

  init() {
    this.filterForm = [
      {
        key: 'keyword',
        type: 'input',
        name: 'ID/手机号',
        placeholder: '请输入',
      },
      {
        key: 'real',
        type: 'select',
        allowClear: true,
        name: '实名认证',
        select: SwitchSelect,
        placeholder: '请选择',
        number: true,
      },
    ];
    this.columns = [
      {
        title: 'ID',
        dataIndex: 'id',
      },
      {
        title: '手机号',
        dataIndex: 'mobile',
      },
      {
        title: '注册时间',
        dataIndex: 'createTime',
      },
      {
        title: '实名认证',
        dataIndex: 'realStatus',
        render: (text) => {
          return SwitchSelectMap[text ? 1 : 0];
        },
      },
      {
        title: '备考信息',
        dataIndex: 'prepareStatus',
        render: (text) => {
          return SwitchSelectMap[text ? 1 : 0];
        },
      }, {
        title: '邀请人数',
        dataIndex: 'inviteNumber',
      }, {
        title: '学习时长',
        dataIndex: 'time',
      }, {
        title: '服务中',
        dataIndex: 'services',
        render: (text) => {
          return (text || []).map(row => ServiceKeyMap[row.service]).join(', ');
        },
      }, {
        title: '消费金额',
        dataIndex: 'totalMoney',
        render: (text) => {
          return formatMoney(text);
        },
      }, {
        title: '操作',
        dataIndex: 'handler',
        render: (text, record) => {
          return <div className="table-button">
            {(
              <Link to={`/user/detail/${record.id}`}>查看</Link>
            )}
          </div>;
        },
      },
    ];
  }

  initData() {
    User.list(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>;
  }
}