import React from 'react';
import { Link } from 'react-router-dom';
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, bindSearch, formatDate } from '@src/services/Tools';
import { QuestionType, AskStatus, MoneyRange, SwitchSelect, AskTarget } from '../../../../Constant';
import { User } from '../../../stores/user';
import { Question } from '../../../stores/question';

const QuestionTypeMap = getMap(QuestionType, 'value', 'label');
const AskStatusMap = getMap(AskStatus, 'value', 'label');
const SwitchSelectMap = getMap(SwitchSelect, 'value', 'label');
export default class extends Page {
  init() {
    this.filterForm = [{
      key: 'type',
      type: 'select',
      allowClear: true,
      name: '题型',
      select: QuestionType,
      placeholder: '请选择',
      number: true,
    }, {
      key: 'status',
      type: 'select',
      allowClear: true,
      name: '状态',
      select: AskStatus,
    }, {
      key: 'money',
      type: 'select',
      allowClear: true,
      name: '消费金额',
      select: MoneyRange,
    }, {
      key: 'showStatus',
      type: 'select',
      allowClear: true,
      name: '展示状态',
      select: SwitchSelect,
    }, {
      key: 'target',
      type: 'select',
      allowClear: true,
      name: '提问内容',
      select: AskTarget,
    }, {
      key: 'questionNoId',
      type: 'select',
      allowClear: true,
      name: '题目ID',
      select: [],
      number: true,
      placeholder: '请输入',
    }, {
      key: 'userId',
      type: 'select',
      name: '用户',
      allowClear: true,
      select: [],
      number: true,
      placeholder: '请输入',
    }];
    this.columns = [
      {
        title: '题型',
        dataIndex: 'type',
        render: (text, record) => {
          return QuestionTypeMap[record.question.type];
        },
      },
      {
        title: '题目id',
        dataIndex: 'questionNo.no',
      },
      {
        title: '提问时间',
        dataIndex: 'createTime',
        render: (text) => {
          return formatDate(text);
        },
      },
      {
        title: '提问摘要',
        dataIndex: 'content',
      }, {
        title: '提问者',
        dataIndex: 'user.nickname',
      }, {
        title: '回答状态',
        dataIndex: 'answerStatus',
        render: (text) => {
          return AskStatusMap[text] || text;
        },
      }, {
        title: '回答者',
        dataIndex: 'manager.username',
      }, {
        title: '回答时间',
        dataIndex: 'answerTime',
      }, {
        title: '展示状态',
        dataIndex: 'showStatus',
        render: (text) => {
          return SwitchSelectMap[text] || text;
        },
      }, {
        title: '操作',
        dataIndex: 'handler',
        render: (text, record) => {
          return <div className="table-button">
            {(
              <Link to={`/user/ask/detail/${record.id}`}>编辑</Link>
            )}
          </div>;
        },
      },
    ];
    bindSearch(this.filterForm, 'user_id', this, (search) => {
      return User.list(search);
    }, (row) => {
      return {
        title: `${row.nickname}(${row.mobile})`,
        value: row.id,
      };
    }, this.state.search.user_id ? [Number(this.state.search.user_id)] : [], null);
    bindSearch(this.filterForm, 'question_no_id', this, (search) => {
      return Question.searchNo(search);
    }, (row) => {
      return {
        title: row.no,
        value: row.id,
      };
    }, this.state.search.question_no_id ? [Number(this.state.search.question_no_id)] : [], null);
  }

  initData() {
    User.listAsk(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 => {
          if (data.time.length > 0) {
            data.time = [data.time[0].format('YYYY-MM-DD HH:mm:ss'), data.time[1].format('YYYY-MM-DD HH:mm:ss')];
          }
          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}
      />
    </Block>;
  }
}
