import React from 'react';
import { Tabs } from 'antd';
import './index.less';
import Page from '@src/containers/Page';
import Block from '@src/components/Block';
import ShowImage from '@src/components/ShowImage';
import ActionLayout from '@src/layouts/ActionLayout';
import TableLayout from '@src/layouts/TableLayout';
// import { formatDate } from '@src/services/Tools';
import { asyncSMessage, asyncForm } from '@src/services/AsyncTools';
import { Ready } from '../../../stores/ready';
import { System } from '../../../stores/system';

export default class extends Page {
  init() {
    this.itemList = [{
      key: 'id',
      type: 'hidden',
    }, {
      key: 'title',
      type: 'input',
      name: '标题',
    }, {
      key: 'content',
      type: 'textarea',
      name: '内容',
    }, {
      key: 'cover',
      type: 'image',
      name: '封面图片',
      onUpload: ({ file }) => {
        return System.uploadImage(file).then(result => { return result; });
      },
    }];

    this.columns = [{
      title: '资料封面',
      dataIndex: 'cover',
      render: (text) => {
        return <ShowImage src={text} />;
      },
    }, {
      title: '资料标题',
      dataIndex: 'title',
    }, {
      title: '内容',
      dataIndex: 'content',
    }, {
      title: '操作',
      dataIndex: 'handler',
      render: (text, record) => {
        return <div className="table-button">
          {(
            <a onClick={() => {
              this.editAction(record);
            }}>编辑</a>
          )}
        </div>;
      },
    }];

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

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

  refreshTab(tab) {
    const { search } = this.state;
    search.tab = tab;
    this.setState({ search });
    if (tab === 'official') {
      return this.refreshOfficial();
    }
    if (tab === 'unofficial') {
      return this.refreshUnofficial();
    }
    return Promise.reject();
  }

  refreshOfficial() {
    return Ready.listData({ isOfficial: true }).then((result) => {
      this.setState({ list: result.list, total: result.total });
    });
  }

  refreshUnofficial() {
    return Ready.listData({ isOfficial: false }).then((result) => {
      this.setState({ list: result.list, total: result.total });
    });
  }

  addAction() {
    asyncForm('创建资料', this.itemList, {}, data => {
      data.isOfficial = this.state.search.tab === 'official' ? 1 : 0;
      return Ready.addData(data).then(() => {
        asyncSMessage('添加成功!');
        this.refresh();
      });
    });
  }

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

  renderOfficial() {
    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>;
  }

  renderUnofficial() {
    return this.renderOfficial();
  }

  renderView() {
    const { search } = this.state;
    const { tab } = search;
    return <div>
      <Tabs activeKey={tab || 'official'} onChange={(value) => {
        this.search({ tab: value });
      }}>
        <Tabs.TabPane tab="官方资料" key="official">
          {this.renderOfficial()}
        </Tabs.TabPane>
        <Tabs.TabPane tab="非官方资料" key="unofficial">
          {this.renderUnofficial()}
        </Tabs.TabPane>
      </Tabs>
    </div>;
  }
}