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 { getMap, bindSearch, formatDate } from '@src/services/Tools';
import { asyncSMessage, asyncForm } from '@src/services/AsyncTools';
import { ChannelModule, SwitchSelect } from '../../../../Constant';
import { System } from '../../../stores/system';
import { User } from '../../../stores/user';

const SwitchSelectMap = getMap(SwitchSelect, 'value', 'label');
const ChannelModuleMap = getMap(ChannelModule, 'value', 'label');
export default class extends Page {
  init() {
    this.actionList = [{
      key: 'add',
      type: 'primary',
      name: '创建',
    }];
    this.itemList = [{
      key: 'id',
      type: 'hidden',
    }, {
      key: 'channel',
      type: 'select',
      allowClear: true,
      name: '频道',
      select: ChannelModule,
      placeholder: '请选择',
    }, {
      key: 'position',
      type: 'select',
      allowClear: true,
      name: '位置',
      select: ChannelModule,
      placeholder: '请选择',
    }, {
      key: 'nickname',
      type: 'input',
      name: '学员昵称',
    }, {
      key: 'avatar',
      type: 'image',
      name: '学员头像',
      onUpload: ({ file }) => {
        return System.uploadImage(file).then(result => { return result; });
      },
    }, {
      key: 'content',
      type: 'textarea',
      name: '评价内容',
    }];
    this.userItemList = [{
      key: 'id',
      type: 'hidden',
    }, {
      key: 'channel',
      type: 'select',
      allowClear: true,
      name: '频道',
      select: ChannelModule,
      placeholder: '请选择',
    }, {
      key: 'position',
      type: 'select',
      allowClear: true,
      name: '位置',
      select: ChannelModule,
      placeholder: '请选择',
    }, {
      key: 'content',
      type: 'textarea',
      name: '评价内容',
    }];
    this.filterForm = [{
      key: 'channel',
      type: 'select',
      allowClear: true,
      name: '频道',
      select: ChannelModule,
      placeholder: '请选择',
    }, {
      key: 'position',
      type: 'select',
      allowClear: true,
      name: '位置',
      select: ChannelModule,
      placeholder: '请选择',
    }, {
      key: 'userId',
      type: 'select',
      allowClear: true,
      name: '用户',
      select: [],
      number: true,
      placeholder: '请输入',
    }, {
      key: 'isSpecial',
      type: 'select',
      allowClear: true,
      name: '精选',
      number: true,
      select: SwitchSelect,
    }];
    this.columns = [
      {
        title: '频道',
        dataIndex: 'channel',
        render: (text, record) => {
          return ChannelModuleMap[record.channel];
        },
      },
      {
        title: '位置',
        dataIndex: 'position',
      },
      {
        title: '内容',
        dataIndex: 'content',
      },
      {
        title: '用户',
        dataIndex: 'user',
        render: (text, record) => {
          let extend = '';
          if (record.isSystem) extend = '系统创建';
          else if (!record.userId) extend = '未注册';
          return `${text.nickname || record.nickname}${extend ? `(${extend})` : ''}`;
        },
      }, {
        title: '时间',
        dataIndex: 'createTime',
        render: (text) => {
          return formatDate(text);
        },
      }, {
        title: '精选',
        dataIndex: 'isSpecial',
        render: (text) => {
          return SwitchSelectMap[text] || text;
        },
      }, {
        title: '操作',
        dataIndex: 'handler',
        render: (text, record) => {
          return <div className="table-button">
            {(
              <a onClick={() => {
                this.editAction(record);
              }}>编辑</a>
            )}
            {!!record.isSpecial && (
              <a onClick={() => {
                this.special(record, 0);
              }}>取消精选</a>
            )}
            {!record.isSpecial && (
              <a onClick={() => {
                this.special(record, 1);
              }}>精选</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);
  }

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

  addAction() {
    asyncForm('创建评价', this.itemList, {}, data => {
      return System.addComment(data).then(() => {
        asyncSMessage('添加成功!');
        this.refresh();
      });
    });
  }

  editAction(row) {
    let item = this.itemList;
    if (row.userId) {
      item = this.userItemList;
    }
    asyncForm('编辑', item, row, data => {
      return System.editComment(data).then(() => {
        asyncSMessage('编辑成功!');
        this.refresh();
      });
    });
  }

  special(row, isSpecial) {
    System.editComment({ id: row.id, isSpecial }).then(() => {
      asyncSMessage('操作成功!');
      this.refresh();
    });
  }

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