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, formatDate, formatMoney, bindSearch } from '@src/services/Tools';
import { asyncSMessage, asyncForm } from '@src/services/AsyncTools';
import { ProductTypeMain, RecordBuySource } from '../../../../Constant';
import { User } from '../../../stores/user';

const ProductTypeMainMap = getMap(ProductTypeMain, 'value', 'label');
const RecordBuySourceMap = getMap(RecordBuySource, 'value', 'label');
export default class extends Page {
  init() {
    this.itemList = [{
      key: 'id',
      type: 'hidden',
    }, {
      key: 'transactionNo',
      type: 'input',
      required: true,
      name: '支付流水号',
    }];
    this.filterF = null;
    this.filterForm = [{
      key: 'userId',
      type: 'select',
      allowClear: true,
      name: '用户',
      select: [],
      number: true,
      placeholder: '请输入',
    }, {
      key: 'productType',
      type: 'select',
      allowClear: true,
      name: '种类',
      select: ProductTypeMain,
    }, {
      key: 'payMethod',
      type: 'select',
      allowClear: true,
      name: '购买方式',
      select: RecordBuySource,
    }, {
      key: 'orderId',
      type: 'input',
      allowClear: true,
      name: '订单id',
    }];
    this.columns = [{
      title: '订单号',
      dataIndex: 'id',
    }, {
      title: '下单时间',
      dataIndex: 'createTime',
      render: (text) => {
        return text ? formatDate(text, 'YYYY-MM-DD HH:mm:ss') : '';
      },
    }, {
      title: '用户姓名',
      dataIndex: 'user.nickname',
    }, {
      title: '种类',
      dataIndex: 'productTypes',
      render: (text) => {
        return (text || []).map(row => `${ProductTypeMainMap[row]}`).join(', ');
      },
    }, {
      title: '支付金额',
      dataIndex: 'money',
      render: (text, record) => {
        return `${formatMoney(text)}${text !== record.originMoney ? `(${formatMoney(record.originMoney)})` : ''}`;
      },
    }, {
      title: '支付方式',
      dataIndex: 'payMethod',
      render: (text) => {
        return RecordBuySourceMap[text] || '';
      },
    }, {
      title: '支付时间',
      dataIndex: 'payTime',
      render: (text) => {
        return text ? formatDate(text, 'YYYY-MM-DD HH:mm:ss') : '';
      },
    }, {
      title: '操作',
      dataIndex: 'handler',
      render: (text, record) => {
        return <div className="table-button">
          <Link to={`/user/order/detail/${record.id}`}>查看</Link>
          {record.payStatus === 0 && (
            <a onClick={() => {
              this.finishAction(record);
            }}>确认收款</a>
          )}
        </div>;
      },
    }];
    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.listOrder(Object.assign({}, this.state.search)).then(result => {
      this.setTableData(result.list, result.total);
    });
  }

  finishAction(row) {
    asyncForm('确认收款', this.itemList, row, data => {
      return User.finishOrder(data).then(() => {
        asyncSMessage('支付成功!');
        this.refresh();
      });
    }).then(component => {
      this.formF = component;
    });
  }

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