import { Component, OnInit } from '@angular/core';
import {environment} from '../../../../environments/environment';
import {FormBuilder, FormGroup} from '@angular/forms';
import {MerchantService} from '../../../services/merchant.service';
import {IconService} from '../../../services/icon.service';
import {NzMessageService} from 'ng-zorro-antd';
import {Router} from '@angular/router';
import {CommonsService} from '../../../services/commons.service';
import {OrderService} from '../../../services/order.service';
import {CompanyService} from '../../../services/company.service';
import {ADMIN_INFO_OBJECT} from "../../../services/local-storage.namespace";
import {LocalStorageService} from "../../../services/local-storage.service";

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

  WEB_SERVE_URL = environment.baseUrl;
  FILE_URL = environment.imageUrl;
  searchForm: FormGroup; // 搜索框
  requestData = []; // 列表数据
  companyData = []; // 列表数据
  merchantData = []; // 列表数据
  total: number; // 页码
  pageNum = 1; // 页码
  pageSize = 10; // 条码
  loading = true;
  id: number; // 订单ID
  isVisible = false;
  refundContent: string;
  roleType: number;
  constructor(
      private form: FormBuilder,
      private order: OrderService,
      private iconService: IconService,
      private message: NzMessageService,
      private router: Router,
      private company: CompanyService,
      private merchant: MerchantService,
      private store: LocalStorageService, // 数据请求
      private common: CommonsService
  ) {
  }

  ngOnInit(): void {
    this.init();
    this.roleType = this.store.get(ADMIN_INFO_OBJECT)['secRole']['roleType'];
    const whereObject = {};
    whereObject['pageNum'] = 1;
    whereObject['pageSize'] = 10000;
    this.company.selectCompanyList(whereObject, data => {
      if (data['return_code'] === '000000') {
        this.companyData = data['return_data'].list;
      } else {
        this.message.error(data['return_msg']);
      }
    });
    if (this.roleType === 5) {
      this.getMerchantListByCompany(this.store.get(ADMIN_INFO_OBJECT)['bsCompany']['id']);
    } else {
      this.getMerchantListByCompany(this.searchForm.value.companyId);
    }

  }

  public init(): void {
    this.searchForm = this.form.group({
      companyId: [null],
      merchantId: [null],
      orderNo: [null],
      paySerialNo: [null],
      payModel: [null],
      payType: [null],
      memDiscountName: [null],
      memPhone: [null],
      orderStatus: [null],
      payTime: [null]
    });
    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();
    }


    this.loading = false;
    if (reset) {
      this.pageNum = 1;
    }
    whereObject['pageNum'] = this.pageNum;
    whereObject['pageSize'] = this.pageSize;
    this.order.getOrderCouponList(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 getMerchantListByCompany(companyId: number): void {
    this.merchant.getMerchantListByCompany(companyId, data => {
      if (data['return_code'] === '000000') {
        if (data['return_code'] === '000000') {
          this.merchantData = data['return_data'];
        } else {
          this.message.error(data['return_msg']);
        }
      } else {
        this.message.error(data['return_msg']);
      }
    });
  }

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


  // 查看详情
  public getDetail(id: number): void {
    this.router.navigate(['/admin/order/order-detail'], {
      queryParams: {
        merchantId: id
      }
    }).then(r => console.log(r));
  }

  // 下载模板
  downloadTemplate(whereObject) {
    if (whereObject['payTime'] != null && whereObject['payTime'].length !== 0) {
      whereObject['payTimeS'] = whereObject['payTime'][0].getTime();
      whereObject['payTimeE'] = whereObject['payTime'][1].getTime();
    }
    this.order.exportGoodsOrderList(whereObject, data => {
      if (data['return_code'] === '000000') {
        window.location.href = this.FILE_URL + data['return_data'];
      } else {
        this.message.error(data['return_msg']);
      }
    });
  }

  provinceChange(e) {
    console.log(e);
  }

  // 退款
  public orderToRefund(id): void {

  }

  showModal(id): void {
    this.id = id;
    this.isVisible = true;
  }

  handleOk(): void {
    this.common.showConfirm('确认订单是否退款', item => {
      if (item) {
        const params = {
          orderId: this.id ,
          refundContent: this.refundContent
        };
        this.order.refuelingOrderRefund(params, data => {
          if (data['return_code'] === '000000') {
            this.getRequest(false, this.searchForm.value);
            this.isVisible = false;
            this.message.success('退款成功');
          } else {
            this.message.error(data['return_msg']);
          }
        });
      }
    });
  }

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