import React, { useState, useEffect } from 'react';
import { Form, Icon, Input, Button, DatePicker, Select, Card, Row, Col, Pagination, Alert, Table, Avatar, Radio, Modal, Descriptions, notification } from 'antd';
import moment from 'moment';
import request from '../../../../utils/request';
import apis from '../../../../services/apis';
import Styles from '../style.less';


const { Option } = Select;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { Meta } = Card;

/**
 * 积分记录
 *
 * @param {*} props
 * @returns
 */
class ModalIntegralRecord extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
       dataSource: { result: { records: [] }, totalPoints: 0 },
       visibleData: { visible: false, customerId: '' },
    }
  }

  // 挂载之后
  componentDidMount() {
    this.getList({ pageNumber: 1, pageSize: 5 })
  }

  componentDidUpdate(preProps, preState) {
    if (this.props.visibleData.customerId !== preState.visibleData.customerId) {
      this.getList({ pageNumber: 1, pageSize: 5 })
      this.setState({ visibleData: this.props.visibleData });
    }
  }

  // 弹框确定按钮
  // eslint-disable-next-line react/sort-comp
  handleOk() {
    this.setState({ visibleData: { visible: false, customerId: '' } })
  }

  // 弹框取消按钮
  handleCancel() {
    this.setState({ visibleData: { visible: false, customerId: '' } })
  }

  openNotificationWithIcon = (type, message) => {
    notification[type]({
      message,
      description:
        '',
    });
  }

  getList(params) {
    const { customerId } = this.state.visibleData
    if (customerId === '' || customerId === undefined) {
      return
    }
    // 网路请求
    const { url, method } = apis.customer.taPointsRecords
    const tempUrl = url.substring(0, url.lastIndexOf('id')).concat(customerId)

    // 网路请求
    request({ url: tempUrl, method, params: { ...params } }).then(res => {
      this.setState({ dataSource: res })
    }).catch(err => {
      this.openNotificationWithIcon('error', err)
    })
  }

   // 分页
  onChange(pageNum) {
    this.getList({ pageNumber: pageNum, pageSize: 5 })
  }

  // 积分类型
  showChangeType(str) { 

    switch (str) {
      case 'goods':
        return '兑换商品'
      case 'checkin':
        return '签到'
      case 'share-poster':
        return '分享'
      case 'signup-agent':
        return '注册经纪人'
      case 'recommend-customer':
        return '推荐客户'
      default:
        return '未知类型'
    }
  }
  
  // 判断是否是 - 开头
  subPoints(points) { 
    let subStr = points.toString().substring(0, 1)
    if (subStr === '-') {
      return true
    }
    return false
  }

  render() {
    const columns = [
      {
        title: '序号',
        dataIndex: 'index',
        key: 'index',
      },
      {
        title: '积分类型',
        dataIndex: 'changeType',
        key: 'changeType',
        render: (_, record) => { this.showChangeType(record.changeType) },
      },
      {
        title: '积分变化',
        dataIndex: 'pointsAmount',
        key: 'pointsAmount',
        render: (_, record) => { <span style={this.subPoints(record.pointsAmount) && {color: 'red'}}>{ record.pointsAmount }</span> },
      },
      {
        title: '发生时间',
        dataIndex: 'createDate',
        key: 'createDate',
        render: (_, record) => { moment(record.createDate).format('YYYY-MM-DD') },
      },
    ]
    return (
      <>
        <Modal
            title={ "当前可用积分：" + this.state.dataSource.totalPoints }
            width={800}
            destroyOnClose="true"
            footer={null}
            visible={this.state.visibleData.visible}
            // onOk={() => this.handleOk()}
            onCancel={(e) => this.handleCancel(e)}
          >
            <Table dataSource={this.state.dataSource.result.records} columns={columns} pagination={{ total: this.state.dataSource.result.total, onChange: e => this.onChange(e) }} />
          </Modal>
      </>
    );
  }
}

export default ModalIntegralRecord
