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 { TextbookFeedbackStatus, TextbookFeedbackTarget, TextbookSubject } from '../../../../Constant';
import { User } from '../../../stores/user';

const TextbookSubjectMap = getMap(TextbookSubject, 'value', 'label');
const TextbookFeedbackStatusMap = getMap(TextbookFeedbackStatus, 'value', 'label');
const TextbookFeedbackTargetMap = getMap(TextbookFeedbackTarget, 'value', 'label');
export default class extends Page {
  init() {
    this.actionList = [{
      key: 'handle',
      type: 'danger',
      name: '批量采纳',
      needSelect: 1,
    }, {
      key: 'nohandle',
      type: 'danger',
      name: '批量不修改',
      needSelect: 1,
    }, {
      key: 'ignore',
      type: 'danger',
      name: '批量忽略',
      needSelect: 1,
    }];
    this.filterForm = [{
      key: 'questionSubject',
      type: 'select',
      allowClear: true,
      name: '单项',
      select: TextbookSubject,
      placeholder: '请选择',
    }, {
      key: 'target',
      type: 'select',
      allowClear: true,
      name: '反馈类型',
      select: TextbookFeedbackTarget,
      placeholder: '请选择',
    }, {
      key: 'status',
      type: 'select',
      name: '处理状态',
      allowClear: true,
      select: TextbookFeedbackStatus,
      number: true,
      placeholder: '请选择',
    }, {
      key: 'no',
      type: 'number',
      name: '序号',
      allowClear: true,
      number: true,
      placeholder: '请输入',
    }];
    this.columns = [{
      title: '单项',
      dataIndex: 'topic.textbookSubject',
      render: (text) => {
        return TextbookSubjectMap[text] || '';
      },
    }, {
      title: '反馈类型',
      dataIndex: 'target',
      render: (text) => {
        return TextbookFeedbackTargetMap[text] || '';
      },
    }, {
      title: '换库表',
      dataIndex: 'library',
      render: (text) => {
        return `${formatDate(text.startDate, 'YYYY-MM-DD')}-${text.endDate ? `${formatDate(text.endDate, 'YYYY-MM-DD')}` : '至今'}`;
      },
    }, {
      title: '题目序号',
      dataIndex: 'topic.no',
    }, {
      title: '关键词',
      dataIndex: 'topic.keyword',
    }, {
      title: '提交时间',
      dataIndex: 'createTime',
      render: (text) => {
        return formatDate(text, 'YYYY-MM-DD HH:mm:ss');
      },
    }, {
      title: '提交人',
      dataIndex: 'user.nickname',
    }, {
      title: '处理状态',
      dataIndex: 'status',
      render: (text) => {
        return TextbookFeedbackStatusMap[text] || text;
      },
    }, {
      title: '操作',
      dataIndex: 'handler',
      render: (text, record) => {
        return <div className="table-button">
          {(
            <a onClick={() => {
              this.detailAction(record);
            }}>查看</a>
          )}
        </div>;
      },
    }];
  }

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

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

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

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

  ignoreDetail() {
    const { detail } = this.state;
    asyncDelConfirm('忽略确认', '是否忽略选中记录?', () => {
      return User.editTextbookFeedback({ 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.editTextbookFeedback({ id: row, status: 1 }))).then(() => {
        asyncSMessage('操作成功!');
        this.refresh();
      });
    });
  }

  nohandleAction() {
    const { selectedKeys } = this.state;
    asyncDelConfirm('不修改确认', '是否不修改选中记录?', () => {
      return Promise.all(selectedKeys.map(row => User.editTextbookFeedback({ id: row, status: 3 }))).then(() => {
        asyncSMessage('操作成功!');
        this.refresh();
      });
    });
  }

  ignoreAction() {
    const { selectedKeys } = this.state;
    asyncDelConfirm('忽略确认', '是否忽略选中记录?', () => {
      return Promise.all(selectedKeys.map(row => User.editTextbookFeedback({ id: row, status: 2 }))).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
        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}
      />
      {this.state.detail && <Modal visible title='勘误详情' footer={null} closable onCancel={() => {
        this.setState({ detail: null });
      }}>
        <p>单项:{TextbookSubjectMap[this.state.detail.topic.questionSubject]}</p>
        <p>换库表:{formatDate(this.state.detail.library.startDate, 'YYYY-MM-DD')}-{this.state.detail.library.endDate ? `${formatDate(this.state.detail.library.endDate, 'YYYY-MM-DD')}` : '至今'}</p>
        <p>反馈题目序号:{this.state.detail.topic.no}</p>
        <p>反馈内容{this.state.detail.content}</p>
        {!this.state.detail.status && <p><Button type="primary" onClick={() => {
          this.handleDetail();
        }}>采纳</Button><Button type="primary" onClick={() => {
          this.nohandleDetail();
        }}>无需修改</Button><Button type="ghost" onClick={() => {
          this.ignoreDetail();
        }}>忽略</Button></p>}
      </Modal>}
    </Block>;
  }
}