import {Component, OnDestroy, OnInit} from '@angular/core';
import {environment} from '../../../../environments/environment';

import {DiscountService} from '../../../services/discount.service';
import {CouponService} from '../../../services/coupon.service';
import {IconService} from '../../../services/icon.service';

import {CommonsService} from '../../../services/commons.service';
import {RechargeService} from '../../../services/recharge.service';
import {NzMessageService, NzNotificationService} from 'ng-zorro-antd';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {Router} from '@angular/router';
import {AuditService} from '../../../services/audit.service';
import {OrderService} from '../../../services/order.service';

@Component({
  selector: 'app-refund-audit',
  templateUrl: './refund-audit.component.html',
  styleUrls: ['./refund-audit.component.scss']
})
export class RefundAuditComponent implements OnInit {

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

  total: number; // 页码
  pageNum = 1; // 页码
  pageSize = 10; // 条码
  loading = true;
  isVisible = false;
  id: number;
  refundSource: number;
  edit = false;
  data = {};
  private timer; // 定时器
  constructor(
      private form: FormBuilder,
      private recharge: RechargeService,
      private discount: DiscountService,
      private coupon: CouponService,
      private audit: AuditService,
      private iconService: IconService,
      private message: NzMessageService,
      private router: Router,
      private notification: NzNotificationService,
      private order: OrderService,
      private common: CommonsService
  ) {
  }

  ngOnInit(): void {
    this.init();

  }



  public init(): void {
    this.searchForm = this.form.group({
      status: [null],
      orderNo: [null],
      rechargeModel: [null],
      rechargeContent: [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 (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.audit.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 , refundSource: number): void {
    this.id = id;
    this.refundSource = refundSource;
    if (refundSource === 1) {
      this.recharge.getOrderById(id , data => {
        if (data['return_code'] === '000000') {
          this.data = data['return_data'];
        } else {
          this.message.error(data['return_msg']);
        }
      });
    } else {
      this.order.getOrderById(id , data => {
        if (data['return_code'] === '000000') {
          this.data = data['return_data'];
        } else {
          this.message.error(data['return_msg']);
        }
      });
    }

    this.isVisible = true;
  }


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



  public orderToRefund(id , refundSource: number): void {
    this.common.showConfirm('确认订单是否退款', item => {
      if (item) {
        if (refundSource === 1) {
          this.recharge.orderToRefund(id, data => {
            if (data['return_code'] === '000000') {
              this.message.success('退款成功');
              this.getRequest(false, this.searchForm.value);
            } else {
              this.message.error(data['return_msg']);
            }
          });
        } else {
          this.recharge.orderToRefundPre(id, data => {
            if (data['return_code'] === '000000') {
              this.message.success('退款成功');
              this.getRequest(false, this.searchForm.value);
            } else {
              this.message.error(data['return_msg']);
            }
          });
        }

      }
    });
  }

  public getRefundReject(id): void {
    this.common.showConfirm('确认订单拒绝退款', item => {
      if (item) {
        this.audit.getRefundReject(id, data => {
          if (data['return_code'] === '000000') {
            this.message.success('操作成功');
            this.getRequest(false, this.searchForm.value);
          } else {
            this.message.error(data['return_msg']);
          }
        });
      }
    });
  }



  public postRefund(orderNo): void {
    this.common.showConfirm('确认订单申请退款', item => {
      if (item) {
        this.recharge.postRefund({
          refundSource: 1 ,
          sourceOrderNo: orderNo
        }, data => {
          if (data['return_code'] === '000000') {
            this.message.success('退款成功');
            this.getRequest(false, this.searchForm.value);
          } else {
            this.message.error(data['return_msg']);
          }
        });
      }
    });
  }



  // 下载模板
  downloadTemplate(whereObject) {
    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']);
      }
    });
  }


}
