import React from 'react';
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 { bindSearch, formatDate } from '@src/services/Tools';
import { asyncSMessage, asyncDelConfirm } from '@src/services/AsyncTools';
// import { QuestionType, AskStatus, MoneyRange, SwitchSelect, AskTarget } from '../../../../Constant';
import { User } from '../../../stores/user';

export default class extends Page {
  init() {
    this.filterForm = [{
      key: 'userId',
      type: 'select',
      allowClear: true,
      name: '用户',
      select: [],
      number: true,
      placeholder: '请输入',
    }, {
      key: 'totalAlert',
      type: 'number',
      allowClear: true,
      name: '警告次数',
      number: true,
    }, {
      key: 'time',
      type: 'daterange',
      name: '注册时间',
    }];
    this.columns = [{
      title: '登录时间',
      dataIndex: 'createTime',
      render: (text) => {
        return formatDate(text);
      },
    }, {
      title: '用户id',
      dataIndex: 'user.id',
    }, {
      title: '手机号',
      dataIndex: 'user.mobile',
    }, {
      title: '昵称',
      dataIndex: 'user.nickname',
    }, {
      title: '注册时间',
      dataIndex: 'user.createTime',
      render: (text) => {
        return formatDate(text);
      },
    }, {
      title: '注册ip',
      dataIndex: 'user.registerIp',
    }, {
      title: '注册地',
      dataIndex: 'user.registerCity',
    }, {
      title: '本次登录ip',
      dataIndex: 'loginIp',
    }, {
      title: '本次登录地',
      dataIndex: 'loginCity',
    }, {
      title: '累计警告次数',
      dataIndex: 'user.totalAlert',
    }, {
      title: '操作',
      dataIndex: 'handler',
      render: (text, record) => {
        return <div className="table-button">
          {record.isIgnore && '已忽略'}
          {record.isAlert && '已警告'}
          {record.isAlert && record.user.isFrozen && (
            <a onClick={() => {
              this.noFrozenAction(record);
            }}>取消封禁</a>
          )}
          {!record.isIgnore && !record.isAlert && (
            <a onClick={() => {
              this.alertAction(record);
            }}>警告</a>
          )}
          {!record.isIgnore && !record.isAlert && (
            <a onClick={() => {
              this.frozenAction(record);
            }}>封禁</a>
          )}
          {!record.isIgnore && !record.isAlert && (
            <a onClick={() => {
              this.ignoreAction(record);
            }}>忽略</a>
          )}
        </div>;
      },
    }];
    bindSearch(this.filterForm, 'userId', this, (search) => {
      return User.list(search);
    }, (row) => {
      return {
        title: `${row.nickname}(${row.mobile})`,
        value: row.id,
      };
    }, this.state.search.userId ? Number(this.state.search.userId) : null, null);
  }

  initData() {
    const { search } = this.state;
    const data = Object.assign({}, search);
    if (data.time) {
      data.startTime = data.time[0] || '';
      data.endTime = data.time[1] || '';
    }
    User.listAbnormal(data).then(result => {
      this.setTableData(result.list, result.total);
    });
  }

  alertAction(row) {
    asyncDelConfirm('警告确认', '是否警告选中记录?', () => {
      return User.handleAbnormal({ id: row.id, isAlert: 1 }).then(() => {
        asyncSMessage('操作成功!');
        this.refresh();
      });
    });
  }

  ignoreAction(row) {
    asyncDelConfirm('忽略确认', '是否忽略选中记录?', () => {
      return User.handleAbnormal({ id: row.id, isIgnore: 1 }).then(() => {
        asyncSMessage('操作成功!');
        this.refresh();
      });
    });
  }

  frozenAction(row) {
    asyncDelConfirm('封禁确认', '是否封禁选中用户?', () => {
      return User.frozen({ id: row.userId }).then(() => {
        asyncSMessage('操作成功!');
        this.refresh();
      });
    });
  }

  noFrozenAction(row) {
    asyncDelConfirm('取消封禁确认', '是否取消封禁选中用户?', () => {
      return User.noFrozen({ id: row.userId }).then(() => {
        asyncSMessage('操作成功!');
        this.refresh();
      });
    });
  }

  renderView() {
    return <Block flex>
      <FilterLayout
        show
        itemList={this.filterForm}
        data={this.state.search}
        onChange={data => {
          if (data.time.length > 0) {
            data.time = [data.time[0].format('YYYY-MM-DD HH:mm:ss'), data.time[1].format('YYYY-MM-DD HH:mm:ss')];
          }
          this.search(data);
        }} />
      {/* <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>;
  }
}