import React from 'react';
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, bindSearch } from '@src/services/Tools';
import { asyncSMessage, asyncDelConfirm } from '@src/services/AsyncTools';
import { SwitchSelect, InvoiceType } from '../../../../Constant';
import { User } from '../../../stores/user';

const SwitchSelectMap = getMap(SwitchSelect, 'value', 'label');
const InvoiceTypeMap = getMap(InvoiceType, 'value', 'label');

export default class extends Page {
  init() {
    this.exerciseMap = {};
    this.filterForm = [{
      key: 'userId',
      type: 'tree',
      allowClear: true,
      tree: [],
      name: '用户',
      placeholder: '用户',
    }, {
      key: 'isDownload',
      type: 'select',
      allowClear: true,
      name: '下载状态',
      select: SwitchSelect,
      number: true,
    }, {
      key: 'isFinish',
      type: 'select',
      allowClear: true,
      name: '开票状态',
      select: SwitchSelect,
      number: true,
    }];
    this.actionList = [{
      key: 'download',
      name: '下载',
    }, {
      key: 'finish',
      name: '批量开票',
    }];
    this.columns = [{
      title: '申请时间',
      dataIndex: 'createTime',
      render: (text) => {
        return formatDate(text);
      },
    }, {
      title: '申请用户',
      dataIndex: 'userId',
      render: (text, record) => {
        return `${record.user.nickname}`;
      },
    }, {
      title: '发票金额',
      dataIndex: 'order.invoiceMoney',
    }, {
      title: '抬头类型',
      dataIndex: 'invoiceType',
      render: (text) => {
        return InvoiceTypeMap[text] || text;
      },
    }, {
      title: '抬头',
      dataIndex: 'title',
    }, {
      title: '纳税人识别号',
      dataIndex: 'identity',
    }, {
      title: '邮箱',
      dataIndex: 'user.email',
    }, {
      title: '开票状态',
      dataIndex: 'isFinish',
      render: (text) => {
        return SwitchSelectMap[text ? 1 : 0];
      },
    }, {
      title: '下载状态',
      dataIndex: 'isDownload',
      render: (text) => {
        return SwitchSelectMap[text ? 1 : 0];
      },
    }];

    bindSearch(this.filterForm, 'userId', this, (search) => {
      return User.list(search);
    }, (row) => {
      return {
        title: `${row.nickname}(${row.mobile})`,
        value: row.id,
      };
    }, this.state.search.userId ? Number(this.state.search.userId) : null, null);
  }

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

  downloadAction() {
    const { selectedKeys } = this.state;
    asyncDelConfirm('下载确认', '是否下载选中记录?', () => {
      return User.downloadInvoice({ ids: selectedKeys }).then(() => {
        asyncSMessage('操作成功!');
        this.refresh();
      });
    });
  }

  finishAction() {
    const { selectedKeys } = this.state;
    asyncDelConfirm('开票确认', '是否开票选中记录?', () => {
      return User.finishInvoice({ ids: selectedKeys }).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.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>;
  }
}