import { Component, OnInit } from '@angular/core';
import {environment} from '../../../../environments/environment';
import {FormBuilder, FormGroup} from '@angular/forms';
import {CompanyService} from '../../../services/company.service';
import {ActivateService} from '../../../services/activate.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 {DiscountPackageService} from '../../../services/discount-package.service';

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

  searchForm: FormGroup; // 搜索框
  requestData = []; // 列表数据
  total: number; // 页码
  pageNum = 1; // 页码
  pageSize = 10; // 条码
  loading = true;
  usingAttribution = []; // 列表数据
  constructor(
      private form: FormBuilder,
      private company: CompanyService,
      private activate: ActivateService,
      private iconService: IconService,
      private message: NzMessageService,
      private router: Router,
      private discountPackage: DiscountPackageService,
      private commonsService: CommonsService,
  ) {
  }

  ngOnInit(): void {
    this.init();
    this.commonsService.getDictionary('USING_ATTRIBUTION  ', data => {
      this.usingAttribution = data['return_data'];
    });
  }

  public init(): void {
    this.searchForm = this.form.group({
      discountPackageTitle: [null],
      salesType: [null],
      usingAttribution: [null],
      phone: [null],
      recordNo: [null],
    });
    this.getRequest(true, this.searchForm.value);
  }

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

    this.loading = false;
    if (reset) {
      this.pageNum = 1;
    }
    whereObject['pageNum'] = this.pageNum;
    whereObject['pageSize'] = this.pageSize;
    this.discountPackage.getDiscountPackageRecordList(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();
  }

}
