import React from 'react';
import { Tabs } from 'antd';
import { Link } from 'react-router-dom';
import './index.less';
import Page from '@src/containers/Page';
import Block from '@src/components/Block';
import ActionLayout from '@src/layouts/ActionLayout';
import TableLayout from '@src/layouts/TableLayout';
import { formatDate, getMap } from '@src/services/Tools';
import { asyncSMessage, asyncForm } from '@src/services/AsyncTools';
import { System } from '../../../stores/system';
import { MessageCategory, MessageEmailStatus, MessageCustomStatus } from '../../../../Constant';

const MessageCategoryMap = getMap(MessageCategory, 'value', 'label');
const MessageEmailStatusMap = getMap(MessageEmailStatus, 'value', 'label');
const MessageCustomStatusMap = getMap(MessageCustomStatus, 'value', 'label');

export default class extends Page {
  init() {
    this.itemList = [{
      key: 'id',
      type: 'hidden',
    }, {
      key: 'title',
      type: 'input',
      name: '标题',
    }, {
      key: 'content',
      type: 'textarea',
      name: '内容',
    }, {
      key: 'link',
      type: 'input',
      name: '链接地址',
    }, {
      key: 'sendTime',
      type: 'date',
      name: '发送时间',
    }];

    this.columns = [{
      title: '消息标题',
      dataIndex: 'title',
    }, {
      title: '消息内容',
      dataIndex: 'content',
    }, {
      title: '发送时间',
      dataIndex: 'sendTime',
      render: (text) => {
        return formatDate(text, 'YYYY-MM-DD HH:mm:ss');
      },
    }, {
      title: '状态',
      dataIndex: 'sendStatus',
      render: (text) => {
        return MessageCustomStatusMap[text] || '';
      },
    }, {
      title: '操作',
      dataIndex: 'handler',
      render: (text, record) => {
        return <div className="table-button">
          {record.sendStatus === 0 && (
            <a onClick={() => {
              this.editAction(record);
            }}>编辑</a>
          )}
        </div>;
      },
    }];

    this.insideColumns = [{
      title: '站内信标题',
      dataIndex: 'title',
    }, {
      title: '触发场景',
      dataIndex: 'messageCategory',
      render: (text) => {
        return MessageCategoryMap[text] || '';
      },
    }, {
      title: '发送数量',
      dataIndex: 'sendNumber',
    }, {
      title: '状态',
      dataIndex: 'sendStatus',
      render: (text) => {
        return MessageEmailStatusMap[text] || '';
      },
    }, {
      title: '操作',
      dataIndex: 'handler',
      render: (text, record) => {
        return <div className="table-button">
          {(
            <Link to={`/show/message/detail/${record.id}`}>编辑</Link>
          )}
          {record.sendStatus === 0 && (
            <a onClick={() => {
              this.openAction(record);
            }}>开启</a>
          )}
          {record.sendStatus === 1 && (
            <a onClick={() => {
              this.closeAction(record);
            }}>关闭</a>
          )}
        </div>;
      },
    }];

    this.emailColumns = [{
      title: '邮件标题',
      dataIndex: 'title',
    }, {
      title: '触发场景',
      dataIndex: 'messageCategory',
      render: (text) => {
        return MessageCategoryMap[text] || '';
      },
    }, {
      title: '发送数量',
      dataIndex: 'sendNumber',
    }, {
      title: '状态',
      dataIndex: 'sendStatus',
      render: (text) => {
        return MessageEmailStatusMap[text] || '';
      },
    }, {
      title: '操作',
      dataIndex: 'handler',
      render: (text, record) => {
        return <div className="table-button">
          {(
            <Link to={`/show/message/detail/${record.id}`}>编辑</Link>
          )}
          {record.sendStatus === 0 && (
            <a onClick={() => {
              this.openAction(record);
            }}>开启</a>
          )}
          {record.sendStatus === 1 && (
            <a onClick={() => {
              this.closeAction(record);
            }}>关闭</a>
          )}
        </div>;
      },
    }];

    this.actionList = [{
      key: 'add',
      name: '增加',
    }];
  }

  initData() {
    this.refreshTab(this.state.search.tab || 'custom');
  }

  refreshTab(tab) {
    const { search } = this.state;
    search.tab = tab;
    this.setState({ search });
    if (tab === 'inside') {
      return this.refreshInside();
    }
    if (tab === 'custom') {
      return this.refreshCustom();
    }
    if (tab === 'email') {
      return this.refreshEmail();
    }
    return Promise.reject();
  }

  refreshInside() {
    return System.listMessage({ messageMethod: 'inside', needCustom: false }).then((result) => {
      this.setState({ list: result.list, total: result.total });
    });
  }

  refreshCustom() {
    return System.listMessage({ messageMethod: 'inside', messageCategory: 'custom' }).then((result) => {
      this.setState({ list: result.list, total: result.total });
    });
  }

  refreshEmail() {
    return System.listMessage({ messageMethod: 'email', needCustom: false }).then((result) => {
      this.setState({ list: result.list, total: result.total });
    });
  }

  addAction() {
    asyncForm('创建消息', this.itemList, {}, data => {
      return System.addMessage(data).then(() => {
        asyncSMessage('添加成功!');
        this.refreshTab('custom');
      });
    });
  }

  editAction(row) {
    asyncForm('编辑消息', this.itemList, row, data => {
      return System.editMessage(data).then(() => {
        asyncSMessage('编辑成功!');
        this.refreshTab('custom');
      });
    });
  }

  openAction(row) {
    System.editMessage({ id: row.id, sendStatus: 1 }).then(() => {
      asyncSMessage('操作成功!');
      this.refresh();
    });
  }

  closeAction(row) {
    System.editMessage({ id: row.id, sendStatus: 0 }).then(() => {
      asyncSMessage('操作成功!');
      this.refresh();
    });
  }

  renderInside() {
    return <Block flex>
      <TableLayout
        columns={this.tableSort(this.insideColumns)}
        list={this.state.list}
        pagination={false}
        loading={this.props.core.loading}
      />
    </Block>;
  }

  renderCustom() {
    return <Block flex>
      <ActionLayout
        itemList={this.actionList}
        selectedKeys={this.state.selectedKeys}
        onAction={key => this.onAction(key)}
      />
      <TableLayout
        columns={this.tableSort(this.columns)}
        list={this.state.list}
        pagination={false}
        loading={this.props.core.loading}
      />
    </Block>;
  }

  renderEmail() {
    return <Block flex>
      <TableLayout
        columns={this.tableSort(this.emailColumns)}
        list={this.state.list}
        pagination={false}
        loading={this.props.core.loading}
      />
    </Block>;
  }

  renderView() {
    const { search } = this.state;
    const { tab } = search;
    return <div>
      <Tabs activeKey={tab || 'inside'} onChange={(value) => {
        this.search({ tab: value });
      }}>
        {/* <Tabs.TabPane tab="模版消息" key="inside">
          {this.renderInside()}
        </Tabs.TabPane> */}
        <Tabs.TabPane tab="自定义消息" key="custom">
          {this.renderCustom()}
        </Tabs.TabPane>
        {/* <Tabs.TabPane tab="邮件模版" key="email">
          {this.renderEmail()}
        </Tabs.TabPane> */}
      </Tabs>
    </div>;
  }
}