import { Component, OnInit } from '@angular/core';
import {FormBuilder, FormGroup} from '@angular/forms';
import {AgentService} from '../../../services/agent.service';
import {IconService} from '../../../services/icon.service';
import {NzMessageService} from 'ng-zorro-antd';
import {LocalStorageService} from '../../../services/local-storage.service';
import {environment} from '../../../../environments/environment';
import {CouponService} from '../../../services/coupon.service';
import {ADMIN_INFO_OBJECT} from '../../../services/local-storage.namespace';

@Component({
  selector: 'app-order-coupon',
  templateUrl: './order-coupon.component.html',
  styleUrls: ['./order-coupon.component.scss']
})
export class OrderCouponComponent implements OnInit {
  gridStyle = {
    width: '33.3333%',
    textAlign: 'center'
  };
  searchForm: FormGroup; // 搜索框
  codeForm: FormGroup; // 搜索框
  requestData = []; // 列表数据
  total: number; // 页码
  pageNum = 1; // 页码
  pageSize = 10; // 条码
  loading = true;
  isVisible = false;
  id;
  type = 1;
  countData = {};
  constructor(
      private form: FormBuilder,
      private agent: AgentService,
      private iconService: IconService,
      private message: NzMessageService,
      private store: LocalStorageService, // 数据请求
      private coupon: CouponService
  ) {
  }

  ngOnInit(): void {
    this.searchForm = this.form.group({
      status: [null],
      salesCode: [null],
    });
    this.getAgentCount(this.type);
  }

  public getAgentCount(type) {
    this.agent.getAgentCount(type , data => {
      this.countData = data['return_data'];
    });
  }

  // 查询列表
  public getRequest(reset: boolean = false, whereObject: object) {

    this.loading = false;
    if (reset) {
      this.pageNum = 1;
    }
    whereObject['couponId'] = this.id;
    whereObject['agentId'] = this.store.get(ADMIN_INFO_OBJECT)['highAgent'].id;
    whereObject['pageNum'] = this.pageNum;
    whereObject['pageSize'] = this.pageSize;
    this.coupon.getCouponCodeList(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 getDetail(id): void {
    this.id = id;
    this.getRequest(false , this.searchForm.value);
    this.isVisible = true;
  }

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

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


}
