import { Component, OnInit } from '@angular/core';
import {environment} from '../../../../environments/environment';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';

import {IconService} from '../../../services/icon.service';
import {NzMessageService, NzNotificationService} from 'ng-zorro-antd';
import {Router} from '@angular/router';
import {CommonsService} from '../../../services/commons.service';
import {RechargeService} from '../../../services/recharge.service';
import {DiscountService} from '../../../services/discount.service';
import {CouponService} from '../../../services/coupon.service';
import {ADMIN_INFO_OBJECT} from "../../../services/local-storage.namespace";
import {LocalStorageService} from "../../../services/local-storage.service";

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

  FILE_URL = environment.imageUrl;
  searchForm: FormGroup; // 搜索框
  validateForm: FormGroup; // 添加框
  requestData = []; // 列表数据

  total: number; // 页码
  pageNum = 1; // 页码
  pageSize = 10; // 条码
  loading = true;
  isVisible = false;
  id: number;
  edit = false;
  data = {};
  private timer; // 定时器
  constructor(
      private form: FormBuilder,
      private recharge: RechargeService,
      private discount: DiscountService,
      private coupon: CouponService,
      private store: LocalStorageService, // 数据请求
      private iconService: IconService,
      private message: NzMessageService,
  ) {
  }

  ngOnInit(): void {
    this.init();
    console.log(typeof  this.store.get(ADMIN_INFO_OBJECT)['bsCompany'].id);
  }



  public init(): void {
    this.searchForm = this.form.group({
      status: [null],
      orderNo: [null],
      rechargeModel: [null],
      rechargeType: [null],
      rechargeContent: [null],
      idCard: [null],
      userPhone: [null],
      payTime: [null],
      payType: [null],
      createTime: [null],
    });
    this.validateForm = this.form.group({
      type: [null, [Validators.required]],
      price: [null, [Validators.required]],
      realPrice: [null, [Validators.required]],
    });

    this.getRequest(true, this.searchForm.value);
  }

  // 查询列表
  public getRequest(reset: boolean = false, whereObject: object) {
    if (this.store.get(ADMIN_INFO_OBJECT)['highAgent'] != null) {
      whereObject['code'] = this.store.get(ADMIN_INFO_OBJECT)['highAgent'].id;
    }

    if (this.store.get(ADMIN_INFO_OBJECT)['bsCompany'].id === 3) {
      whereObject['code'] = 60;
    }
    if (whereObject['payTime'] != null && whereObject['payTime'].length !== 0) {
      whereObject['payTimeS'] = whereObject['payTime'][0].getTime();
      whereObject['payTimeE'] = whereObject['payTime'][1].getTime();
    }

    if (whereObject['createTime'] != null && whereObject['createTime'].length !== 0) {
      whereObject['createTimeS'] = whereObject['createTime'][0].getTime();
      whereObject['createTimeE'] = whereObject['createTime'][1].getTime();
    }
    this.loading = false;
    if (reset) {
      this.pageNum = 1;
    }
    whereObject['pageNum'] = this.pageNum;
    whereObject['pageSize'] = this.pageSize;
    this.recharge.getOrderList(whereObject, data => {
      if (data['return_code'] === '000000') {
        this.requestData = data['return_data'].list;
        this.total = data['return_data'].total;
      } else {
        this.message.error(data['return_msg']);
      }
    });
  }

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

  // 详情
  public getDetail(id: number): void {
    this.id = id;
    this.recharge.getOrderById(id , data => {
      if (data['return_code'] === '000000') {
        console.log(data);
        this.data = data['return_data'];
      } else {
        this.message.error(data['return_msg']);
      }
    });

    this.isVisible = true;
  }


  handleCancel(): void {
    this.isVisible = false;
  }

  // 下载模板
  downloadTemplate(whereObject) {
    if (this.store.get(ADMIN_INFO_OBJECT)['highAgent'] != null) {
      whereObject['code'] = this.store.get(ADMIN_INFO_OBJECT)['highAgent'].id;
    }
    if (whereObject['payTime'] != null && whereObject['payTime'].length !== 0) {
      whereObject['payTimeS'] = whereObject['payTime'][0].getTime();
      whereObject['payTimeE'] = whereObject['payTime'][1].getTime();
    }
    if (whereObject['createTime'] != null && whereObject['createTime'].length !== 0) {
      whereObject['createTimeS'] = whereObject['createTimeS'][0].getTime();
      whereObject['createTimeE'] = whereObject['createTimeE'][1].getTime();
    }
    this.recharge.exportOrderList(whereObject, data => {
      if (data['return_code'] === '000000') {
        window.location.href = this.FILE_URL + data['return_data'];
      } else {
        this.message.error(data['return_msg']);
      }
    });
  }


}
