import React from 'react';
// import { Link } from 'react-router-dom';
import { Button, Switch, Modal } from 'antd';
import './index.less';
import Page from '@src/containers/Page';
import Block from '@src/components/Block';
import EditTableCell from '@src/components/EditTableCell';
// 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, asyncForm } from '@src/services/AsyncTools';
// import { SwitchSelect } from '../../../../Constant';
import { Ready } from '../../../stores/ready';

export default class extends Page {
  init() {
    this.structMap = {};
    this.actionList = [{
      key: 'add',
      type: 'primary',
      name: '创建',
      // render: (item) => {
      //   return <Link to='/ready/read/detail'><Button>{item.name}</Button></Link>;
      // },
    }, {
      key: 'struct',
      name: '管理板块',
    }];
    this.columns = [{
      title: '板块',
      dataIndex: 'plate',
      render: (text) => {
        return this.structMap[text] || '';
      },
    }, {
      title: '文章标题',
      dataIndex: 'title',
    }, {
      title: '更新时间',
      dataIndex: 'updateTime',
      render: (text) => {
        return formatDate(text, 'YYYY-MM-DD HH:mm:ss');
      },
    }, {
      title: '操作',
      dataIndex: 'handler',
      render: (text, record) => {
        return <div className="table-button">
          {<a onClick={() => {
            this.editAction(record);
          }}>编辑</a>}
          {<a onClick={() => {
            this.deleteAction(record);
          }}>删除</a>}
        </div>;
      },
    }];

    this.itemList = [{
      key: 'id',
      type: 'hidden',
    }, {
      key: 'plate',
      type: 'select',
      name: '板块',
      select: [],
      required: true,
    }, {
      key: 'title',
      type: 'input',
      name: '标题',
      required: true,
    }, {
      key: 'link',
      type: 'input',
      name: '链接',
      required: true,
    }];

    this.structColumns = [{
      title: '板块名称',
      dataIndex: 'plate',
      render: (text, record, index) => {
        return <EditTableCell value={text} onChange={(v) => {
          this.changeStruct(index, 'plate', v);
        }} />;
      },
    }, {
      title: '增加跳转',
      dataIndex: 'jump',
      render: (text, record, index) => {
        return <Switch onChange={(value) => {
          this.changeStruct(index, 'jump', value ? 1 : 0);
        }} checked={!!text} />;
      },
    }, {
      title: '跳转地址',
      dataIndex: 'link',
      render: (text, record, index) => {
        return <EditTableCell value={text} onChange={(v) => {
          this.changeStruct(index, 'link', v);
        }} />;
      },
    }, {
      title: '按钮名称',
      dataIndex: 'text',
      render: (text, record, index) => {
        return <EditTableCell value={text} onChange={(v) => {
          this.changeStruct(index, 'text', v);
        }} />;
      },
    }];
    Ready.getReadyRead().then(result => {
      return this.refreshStruct(result);
    }).then(() => {
      this.initData();
    });
  }

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

  refreshStruct(result) {
    result = result || {};
    result.plates = (result.plates || []).map((row, index) => {
      row.title = row.plate;
      row.value = `${index + 1}`;
      return row;
    });
    this.structMap = getMap(result.plates, 'value', 'plate');
    this.setState({ struct: result });
  }

  structAction() {
    const { struct = {} } = this.state;
    this.open(struct);
  }

  changeStruct(index, field, value, other) {
    const { detail } = this.state;
    if (other !== undefined) {
      detail.plates.forEach((row) => {
        row[field] = other;
      });
    }
    detail.plates[index][field] = value;
    this.setState({ detail });
  }

  submitStruct() {
    const { detail } = this.state;
    Ready.setReadyRead(detail).then(() => {
      asyncSMessage('保存成功');
      this.close(false, 'detail');
      return this.refreshStruct(detail);
    }).then(() => {
      return this.initData();
    });
  }

  addAction() {
    this.itemList[1].select = this.state.struct.plates;
    asyncForm('创建', this.itemList, {}, data => {
      return Ready.addRead(data).then(() => {
        asyncSMessage('添加成功!');
        this.refresh();
      });
    }).then(component => {
      this.formF = component;
    });
  }

  editAction(row) {
    this.itemList[1].select = this.state.struct.plates;
    asyncForm('编辑', this.itemList, row, data => {
      return Ready.editRead(data).then(() => {
        asyncSMessage('编辑成功!');
        this.refresh();
      });
    }).then(component => {
      this.formF = component;
    });
  }

  deleteAction(row) {
    asyncDelConfirm('删除确认', '是否删除选中?', () => {
      const handler = Ready.delRead({ id: row.id });
      return handler.then(() => {
        asyncSMessage('删除成功!');
        this.refresh();
      });
    });
  }

  renderView() {
    return <Block flex>
      {/* <FilterLayout
        show
        itemList={this.filterForm}
        data={this.state.search}
        onChange={data => {
          data.page = 1;
          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}
      />

      {this.state.detail && <Modal visible closable title='编辑章节' onCancel={() => {
        this.close(false, 'detail');
      }} onOk={() => {
        this.submitStruct();
      }}>
        <TableLayout
          rowKey={'plate'}
          columns={this.structColumns}
          list={this.state.detail.plates || []}
          pagination={false}
        />
        <Button onClick={() => {
          const { detail } = this.state;
          detail.plates.push({});
          this.setState({ detail });
        }}>增加板块</Button></Modal>}
    </Block>;
  }
}