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, formatTreeData, flattenTree } from '@src/services/Tools';
import { asyncSMessage, asyncForm } from '@src/services/AsyncTools';
import { CommentChannel, SwitchSelect, MoneyRange } from '../../../../Constant';
import { System } from '../../../stores/system';
import { User } from '../../../stores/user';
import { Course } from '../../../stores/course';

const SwitchSelectMap = getMap(SwitchSelect, 'value', 'label');
const CommentChannelTree = formatTreeData(CommentChannel, 'value', 'label', 'parent');
const CommentChannelFlatten = flattenTree(CommentChannelTree, (row, item) => {
  row = Object.assign({}, row);
  row.value = `${item.value}-${row.value}`;
  row.label = `${item.label}-${row.label}`;
  return row;
}, 'children');
const CommentChannelMap = getMap(CommentChannelFlatten, 'value', 'label');
export default class extends Page {
  init() {
    this.itemList = [{
      key: 'id',
      type: 'hidden',
    }, {
      key: 'content',
      type: 'textarea',
      name: '评价内容',
    }];
    this.filterF = null;
    this.filterForm = [{
      key: 'channel',
      type: 'cascader',
      allowClear: true,
      name: '频道',
      select: CommentChannelTree,
      placeholder: '请选择',
      onChange: (value) => {
        this.changeSearch(this.filterForm, this, value.join('-'), null);
      },
    }, {
      key: 'position',
      type: 'select',
      allowClear: true,
      name: '位置',
      select: [],
      placeholder: '请选择',
    }, {
      key: 'userId',
      type: 'select',
      allowClear: true,
      name: '用户',
      select: [],
      number: true,
      placeholder: '请输入',
    }, {
      key: 'moneyRang',
      type: 'select',
      allowClear: true,
      name: '消费金额',
      select: MoneyRange,
      number: true,
    }, {
      key: 'isSpecial',
      type: 'select',
      allowClear: true,
      name: '展示',
      number: true,
      select: SwitchSelect,
    }];
    this.columns = [{
      title: '频道',
      dataIndex: 'channel',
      render: (text, record) => {
        return CommentChannelMap[record.channel];
      },
    }, {
      title: '位置',
      dataIndex: 'position',
    }, {
      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: '评价时间',
      sorter: true,
      dataIndex: 'createTime',
      render: (text) => {
        return formatDate(text, 'YYYY-MM-DD HH:mm:ss');
      },
    }, {
      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, null);
    this.changeSearch(this.filterForm, this, this.state.search.channel, this.state.search.position);
  }

  changeSearch(list, component, key, value) {
    if (key === 'course-video' || key === 'course-vs' || key === 'course-package' || key === 'course_data') {
      bindSearch(list, 'position', component, (search) => {
        if (key === 'course-video') {
          return Course.list(Object.assign({ courseModule: 'video' }, search));
        } if (key === 'course-vs') {
          return Course.list(Object.assign({ courseModule: 'vs' }, search));
        } if (key === 'course-package') {
          return Course.listPackage(search);
        }
        return Course.listData(search);
      }, (row) => {
        return {
          title: row.title,
          value: row.id,
        };
      }, value ? Number(value) : null, null);
      list[1].disabled = false;
    } else {
      list[1].disabled = true;
    }
    component.setState({ load: false });
  }

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

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

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

  renderView() {
    const { search } = this.state;
    return <Block flex>
      <FilterLayout
        show
        ref={(ref) => { this.filterF = ref; }}
        itemList={this.filterForm}
        data={Object.assign({}, search, { channel: search.channel ? search.channel.split('-') : '' })}
        onChange={data => {
          data.channel = data.channel.join('-');
          this.search(data);
        }} />
      {/* <ActionLayout
        itemList={this.actionList}
        selectedKeys={this.state.selectedKeys}
        onAction={key => this.onAction(key)}
      /> */}
      <TableLayout
        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>;
  }
}