import React from 'react';
import { Modal, 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 { getMap, formatDate } from '@src/services/Tools';
import { asyncSMessage, asyncDelConfirm } from '@src/services/AsyncTools';
import { FeedbackStatus, FeedbackModule } from '../../../../Constant';
import { User } from '../../../stores/user';

const FeedbackStatusMap = getMap(FeedbackStatus, 'value', 'label');
const FeedbackModuleMap = getMap(FeedbackModule, 'value', 'label');
export default class extends Page {
  init() {
    this.actionList = [{
      key: 'handle',
      type: 'danger',
      name: '批量处理',
      needSelect: 1,
    }, {
      key: 'ignore',
      type: 'danger',
      name: '批量忽略',
      needSelect: 1,
    }];
    this.filterForm = [{
      key: 'module',
      type: 'select',
      allowClear: true,
      name: '类型',
      select: FeedbackModule,
      placeholder: '请选择',
    }, {
      key: 'status',
      type: 'select',
      allowClear: true,
      name: '状态',
      select: FeedbackStatus,
    }, {
      key: 'title',
      type: 'input',
      name: '材料名称',
      allowClear: true,
      placeholder: '输入名称搜索',
    }];
    this.columns = [
      {
        title: '类型',
        dataIndex: 'module',
        render: (text) => {
          return FeedbackModuleMap[text];
        },
      },
      {
        title: '提交时间',
        dataIndex: 'createTime',
        render: (text) => {
          return formatDate(text);
        },
      },
      {
        title: '提问者',
        dataIndex: 'user.nickname',
      },
      {
        title: '材料名称',
        dataIndex: 'title',
      }, {
        title: '处理状态',
        dataIndex: 'status',
        render: (text) => {
          return FeedbackStatusMap[text] || text;
        },
      }, {
        title: '操作',
        dataIndex: 'handler',
        render: (text, record) => {
          return <div className="table-button">
            {(
              <a onClick={() => {
                this.detailAction(record);
              }}>编辑</a>
            )}
          </div>;
        },
      },
    ];
  }

  initData() {
    User.listFeedbackError(this.state.search).then(result => {
      result.list = result.list.map(row => {
        row.position = row.position.split(',');
        return row;
      });
      this.setTableData(result.list, result.total);
    });
  }

  detailAction(row) {
    this.setState({ detail: row });
  }

  handleDetail() {
    const { detail } = this.state;
    asyncDelConfirm('处理确认', '是否处理选中记录?', () => {
      return User.editFeedbackError({ id: detail.id, status: 1 });
    }).then(() => {
      asyncSMessage('操作成功!');
      this.setState({ detail: null });
      this.refresh();
    });
  }

  ignoreDetail() {
    const { detail } = this.state;
    asyncDelConfirm('忽略确认', '是否忽略选中记录?', () => {
      return User.editFeedbackError({ id: detail.id, status: 2 });
    }).then(() => {
      asyncSMessage('操作成功!');
      this.setState({ detail: null });
      this.refresh();
    });
  }

  handleAction() {
    const { selectedKeys } = this.state;
    asyncDelConfirm('处理确认', '是否处理选中记录?', () => {
      return Promise.all(selectedKeys.map(row => User.editFeedbackError({ id: row, status: 1 })));
    }).then(() => {
      asyncSMessage('操作成功!');
      this.refresh();
    });
  }

  ignoreAction() {
    const { selectedKeys } = this.state;
    asyncDelConfirm('忽略确认', '是否忽略选中记录?', () => {
      return Promise.all(selectedKeys.map(row => User.editFeedbackError({ id: row, status: 2 })));
    }).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
        select
        columns={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 title='勘误详情' footer={null} closable onCancel={() => {
        this.setState({ detail: null });
      }}>
        <p>类型:{FeedbackModuleMap[this.state.detail.module]}</p>
        <p>材料名称:{this.state.detail.title}</p>
        {this.state.detail.position.length > 0 && <p>错误位置:{this.state.detail.position[0]}页,{this.state.detail.position[1]}行</p>}
        <p>错误内容:{this.state.detail.originContent}</p>
        <p>应修改为:{this.state.detail.content}</p>
        <p><Button type="primary" onClick={() => {
          this.handleDetail();
        }}>已处理</Button><Button type="ghost" onClick={() => {
          this.ignoreDetail();
        }}>忽略</Button></p>
      </Modal>}
    </Block>;
  }
}