import React from 'react';
import { Link } from 'react-router-dom';
import { Button } from 'antd';
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 { formatDate, getMap } from '@src/services/Tools';
import { ArticleChannel } from '../../../../Constant';
import { System } from '../../../stores/system';

const ArticleChannelMap = getMap(ArticleChannel, 'value', 'label');

export default class extends Page {
  init() {
    this.filterForm = [{
      key: 'channel',
      type: 'select',
      allowClear: true,
      name: '频道',
      select: ArticleChannel,
      placeholder: '请选择',
    }, {
      key: 'position',
      type: 'select',
      allowClear: true,
      name: '位置',
      select: [],
      placeholder: '请选择',
    }];
    this.actionList = [{
      key: 'add',
      type: 'primary',
      name: '创建',
      render: (item) => {
        return <Link to='/show/article/detail'><Button>{item.name}</Button></Link>;
      },
    }];
    this.columns = [{
      title: '频道',
      dataIndex: 'channel',
      render: (text) => {
        return ArticleChannelMap[text] || '';
      },
    }, {
      title: '位置',
      dataIndex: 'position',
    }, {
      title: '文章标题',
      dataIndex: 'title',
    }, {
      title: '更新时间',
      dataIndex: 'updateTime',
      render: (text) => {
        return formatDate(text);
      },
    }, {
      title: '操作',
      dataIndex: 'handler',
      render: (text, record) => {
        return <div className="table-button">
          {<Link to={`/show/article/detail/${record.id}`}>编辑</Link>}
        </div>;
      },
    }];
  }

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

  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>;
  }
}