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 { asyncSMessage, asyncDelConfirm } from '@src/services/AsyncTools';
// import { ArticleChannel } from '../../../../Constant';
import { Ready } from '../../../stores/ready';

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

export default class extends Page {
  init() {
    this.categoryMap = {};
    this.categoryList = [];
    this.filterForm = [{
      key: 'parentCategoryId',
      type: 'select',
      allowClear: true,
      name: '一级标题',
      placeholder: '请选择',
      select: [],
      number: true,
      onChange: (value) => {
        this.changeSearch(this.filterForm, this, value);
      },
    }, {
      key: 'categoryId',
      type: 'select',
      allowClear: true,
      name: '二级标题',
      select: [],
      number: true,
      placeholder: '请选择',
    }];
    this.actionList = [{
      key: 'add',
      type: 'primary',
      name: '创建',
      render: (item) => {
        return <Link to='/ready/article/detail'><Button>{item.name}</Button></Link>;
      },
    }];
    this.columns = [{
      title: '一级标题',
      dataIndex: 'parentCategoryId',
      render: (text) => {
        return this.categoryMap[text];
      },
    }, {
      title: '二级标题',
      dataIndex: 'categoryId',
      render: (text) => {
        return this.categoryMap[text];
      },
    }, {
      title: '文章标题',
      dataIndex: 'title',
    }, {
      title: '更新时间',
      dataIndex: 'updateTime',
      render: (text) => {
        return formatDate(text);
      },
    }, {
      title: '操作',
      dataIndex: 'handler',
      render: (text, record) => {
        return <div className="table-button">
          {<Link to={`/ready/article/detail/${record.id}`}>编辑</Link>}
          {<a onClick={() => {
            this.deleteAction(record);
          }}>删除</a>}
        </div>;
      },
    }];
    Ready.allCategory().then(result => {
      this.categoryList = result.map(row => {
        row.value = row.id;
        return row;
      });
      this.categoryMap = getMap(result, 'id', 'title');
      this.filterForm[0].select = this.categoryList.filter(row => row.parentId === 0);
      this.onChangeSearch(this.filterForm, this, this.state.search.parentCategoryId);
      this.initData();
    });
  }

  changeSearch(list, component, value) {
    if (value) {
      list[1].disabled = false;
      list[1].select = this.categoryList.filter(row => row.parentId === value);
    } else {
      list[1].disabled = true;
      list[1].select = [];
    }
    component.setState({ load: false });
  }

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

  deleteAction(row) {
    asyncDelConfirm('删除确认', '是否删除选中?', () => {
      const handler = Ready.delArticle({ id: row.id });
      return handler.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
        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>;
  }
}