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} from 'ng-zorro-antd';
import {AgentService} from '../../../services/agent.service';
import {LocalStorageService} from '../../../services/local-storage.service';
import {ADMIN_INFO_OBJECT} from '../../../services/local-storage.namespace';

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

  searchForm: FormGroup; // 搜索框
  validateFormPost: FormGroup; // 搜索框
  requestData = []; // 列表数据
  total: number; // 页码
  pageNum = 1; // 页码
  pageSize = 10; // 条码
  loading = true;

  requestDataCode = []; // 列表数据
  totalCode: number; // 页码
  pageNumCode = 1; // 页码
  pageSizeCode = 10; // 条码
  loadingCode = true;
  isVisible = false;
  isVisibleS = false;
  id: number;
  loadingS = false;
  constructor(
      private form: FormBuilder,
      private agent: AgentService,
      private iconService: IconService,
      private message: NzMessageService,
      private store: LocalStorageService, // 数据请求
  ) {
  }

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

  public init(): void {
    this.searchForm = this.form.group({
      status: [null],
    });
    this.validateFormPost = this.form.group({
      status: [null, [Validators.required]],
      minNumber: [null, [Validators.required]],
      maxNumber: [null, [Validators.required]],
    });
    this.getRequest(true, {});
  }

  // 查询列表
  public getRequest(reset: boolean = false, whereObject: object) {
    this.loading = false;
    if (reset) {
      this.pageNum = 1;
    }
    whereObject['agentId'] = this.store.get(ADMIN_INFO_OBJECT)['highAgent'].id;
    whereObject['pageNum'] = this.pageNum;
    whereObject['pageSize'] = this.pageSize;
    this.agent.getDiscountAgentList(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.getRequestCode(false , this.searchForm.value);
    this.isVisible = true;
  }

  // 查询列表
  public getRequestCode(reset: boolean = false, whereObject: object) {
    this.loadingCode = true;
    if (reset) {
      this.pageNumCode = 1;
    }
    this.searchForm.value['discountAgentId'] = this.id;
    whereObject['pageNum'] = this.pageNumCode;
    whereObject['pageSize'] = this.pageSizeCode;
    this.agent.getDiscountCodeList(whereObject, data => {
      this.loadingCode = false;
      if (data['return_code'] === '000000') {
        this.requestDataCode = data['return_data'].list;
        this.totalCode = data['return_data'].total;
      } else {
        this.message.error(data['return_msg']);
      }
    });
  }



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

  handleCancelS(): void {
    this.isVisibleS = false;
  }

  getEditStatus() {
    this.isVisibleS = true;
  }

  handleOk(): void {
    // tslint:disable-next-line:forin
    for (const i in this.validateFormPost.controls) {
      this.validateFormPost.controls[i].markAsDirty();
      this.validateFormPost.controls[i].updateValueAndValidity();
      if (this.validateFormPost.controls[i].errors != null) {
        this.message.error('必填项不能为空');
        return;
      }
    }
    const id = this.message.loading('正在提交中', { nzDuration: 0 }).messageId;
    this.loadingS = true;
    this.agent.getDiscountStatus(this.validateFormPost.value , data => {
      if (data['return_code'] === '000000') {
        this.isVisibleS = false;
        this.loadingS = false;
        this.getRequestCode(false , this.searchForm.value);
        this.message.remove(id);
        this.message.success('成功');
      } else {
        this.message.error(data['return_msg']);
      }

    });
  }
}
