import { Component, OnInit } from '@angular/core';
import {NzMessageService, NzModalService} from 'ng-zorro-antd';
import {LocalStorageService} from '../../../../services/local-storage.service';
import {CommonsService} from '../../../../services/commons.service';
import {Router} from '@angular/router';
import {FormBuilder, FormGroup} from '@angular/forms';
import {ADMIN_INFO_OBJECT} from '../../../../services/local-storage.namespace';
import {environment} from '../../../../../environments/environment';
import {OrderManageService} from '../../../../services/order/order-manage.service';
import {OrderCouponService} from '../../../../services/order/order-coupon.service';

@Component({
  selector: 'app-order-coupon-list',
  templateUrl: './order-coupon-list.component.html',
  styleUrls: ['./order-coupon-list.component.scss']
})
export class OrderCouponListComponent implements OnInit {

  FILE_URL = environment.imageUrl;
  roleType;
  adminFlag;
  loadingObject = {
    spinning: false,
    msg: '加载中'
  };

  dataObject: any = {};
  tableLoading = true;
  searchForm: FormGroup;
  pageNum = 1;
  pageSize =  10;
  whereObject: any = {};
  isCollapse = false;

  positionTypeArray = [];

  orderDetailModal = false;
  orderDetailModalLoading = false;
  orderDetailObject: any = {
    tradeOrder: {},
    gasOrder: {},
  };

  constructor(private modal: NzModalService,
              private message: NzMessageService,
              private store: LocalStorageService,
              private orderManageService: OrderManageService,
              private orderCouponService: OrderCouponService,
              private commonsService: CommonsService,
              private router: Router,
              private form: FormBuilder) {
    this.roleType = Number(this.store.get(ADMIN_INFO_OBJECT)['secRole'].roleType);
    this.adminFlag = Number(this.store.get(ADMIN_INFO_OBJECT)['secUser'].adminFlag);
  }


  ngOnInit(): void {
    this.searchForm = this.form.group({
      orderNo: [null],
      memPhone: [null],
      couponName: [null],
      payType: [null],
      status: [null],
      createTime: [null],
      payTime: [null],
      finishTime: [null],
    });
    this.requestData(1);
  }

  /**
   * 请求数据
   */
  requestData(pageNum) {
    this.tableLoading = true;
    this.whereObject['pageNum'] = pageNum;
    this.whereObject['pageSize'] = this.pageSize;
    this.orderCouponService.getOrderList(this.whereObject, data => {
      if (data['return_code'] === '000000') {
        this.dataObject = data['return_data'];
      } else {
        this.modal.error(data['return_msg']);
      }
      this.tableLoading = false;
    });
  }

  /**
   * 搜索
   * @param whereObject 条件
   */
  search(whereObject: object) {
    if (whereObject['createTime'] != null) {
      whereObject['createTimeS'] = whereObject['createTime'][0].getTime();
      whereObject['createTimeE'] = whereObject['createTime'][1].getTime();
    } else {
      whereObject['createTimeS'] = null;
      whereObject['createTimeE'] = null;
    }

    if (whereObject['payTime'] != null) {
      whereObject['payTimeS'] = whereObject['payTime'][0].getTime();
      whereObject['payTimeE'] = whereObject['payTime'][1].getTime();
    } else {
      whereObject['payTimeS'] = null;
      whereObject['payTimeE'] = null;
    }

    if (whereObject['finishTime'] != null) {
      whereObject['finishTimeS'] = whereObject['finishTime'][0].getTime();
      whereObject['finishTimeE'] = whereObject['finishTime'][1].getTime();
    } else {
      whereObject['finishTimeS'] = null;
      whereObject['finishTimeE'] = null;
    }
    this.whereObject = whereObject;
    this.requestData(1);
  }

  /**
   * 重置
   */
  resetForm(): void {
    this.searchForm.reset();
  }

  /**
   * 搜索条件折叠
   */
  toggleCollapse(): void {
    this.isCollapse = !this.isCollapse;
  }

  /**
   * 打开订单详情
   */
  showOrderDetail(orderNo: string) {
    this.orderDetailModal = true;
    this.orderDetailModalLoading = true;

    this.orderCouponService.getCouponOrderDetail(orderNo, data => {
      if (data['return_code'] === '000000') {
        this.orderDetailObject = data['return_data'];
      } else {
        this.modal.error({
          nzTitle: '提示',
          nzContent: data['return_msg']
        });
      }
      this.orderDetailModalLoading = false;
    });
  }

  /**
   * 关闭订单详情
   */
  closeOrderDetail() {
    this.orderDetailModal = false;
  }

  /**
   * 导出Excel
   */
  exportExcel(): void {
    this.loadingObject.spinning = true;
    this.loadingObject.msg = '处理中...';

    this.orderCouponService.exportOrderCouponExcel(this.whereObject, data => {
      this.loadingObject.spinning = false;
      if (data['return_code'] === '000000') {
        window.location.href = this.FILE_URL + data['return_data'];
      }
    });
  }

  /**
   * 分页
   * @param pageSize
   */
  pageSizeChang(pageSize: number) {
    this.pageSize = pageSize;
    this.requestData(1)
  }
}
