import { Component, OnInit } from '@angular/core';
import {environment} from '../../../../environments/environment';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {NzMessageService, NzModalService} from 'ng-zorro-antd';
import {LocalStorageService} from '../../../services/local-storage.service';
import {OilCardService} from '../../../services/oil-card.service';
import {OrganizationService} from '../../../services/organization.service';
import {CompanyAccountService} from '../../../services/company-account.service';
import {ActivatedRoute, Router} from '@angular/router';
import {ADMIN_INFO_OBJECT} from '../../../services/local-storage.namespace';
import {ValidatorsService} from '../../../services/validators.service';
import {MerAccountService} from '../../../services/mer-account.service';
import {MerAccountRecordService} from '../../../services/mer-account-record.service';

@Component({
  selector: 'app-mer-account-record-list',
  templateUrl: './mer-account-record-list.component.html',
  styleUrls: ['./mer-account-record-list.component.scss']
})
export class MerAccountRecordListComponent implements OnInit {
  FILE_URL = environment.imageUrl;
  roleType;
  adminFlag;
  loadingObject = {
    spinning: false,
    msg: '加载中'
  };

  dataObject: any = {};
  tableLoading = true;
  searchForm: FormGroup;
  pageNum: number;
  whereObject: any = {};

  rechargeModal = false;
  rechargeForm: FormGroup; // 充值模态框
  merId: number;
  accountObject = {
    amounts: 0
  };

  constructor(private modal: NzModalService,
              private message: NzMessageService,
              private store: LocalStorageService,
              private merAccountService: MerAccountService,
              private merAccountRecordService: MerAccountRecordService,
              private router: Router,
              private activatedRoute: ActivatedRoute,
              private form: FormBuilder) {
    this.activatedRoute.queryParams.subscribe(queryParams => {
      if (queryParams.merId != null) {
        this.merId = queryParams.merId;
      }
    });
    this.roleType = Number(this.store.get(ADMIN_INFO_OBJECT)['secRole'].roleType);
    this.adminFlag = Number(this.store.get(ADMIN_INFO_OBJECT)['secUser'].adminFlag);
  }

  ngOnInit(): void {
    this.searchForm = this.form.group({
      orgId: [null],
      cardNo: [null],
      contactName: [null],
      contactPhone: [null],
      createTime: [null],
      createTimeS: [null],
      createTimeE: [null],
      bindTime: [null],
      bindTimeS: [null],
      bindTimeE: [null],
      isConfigContact: [null],
      status: [null],
      bindStatus: [null],
    });

    this.rechargeForm = this.form.group({
      merId: [null, [Validators.required]],
      amount: [null, [Validators.required]],
      marketingStatus: [null, [Validators.required]],
      marketingType: [null],
      marketingPrice: [null],
    });

    if (this.roleType === 5 && this.adminFlag === 0) {
      this.getAccount();
    }
    this.requestData(1);
  }

  /**
   * 请求数据
   */
  requestData(pageNum) {
    this.tableLoading = true;

    if (this.merId != null) {
      this.whereObject['merId'] = this.merId;
    }
    this.whereObject['pageNum'] = pageNum;
    this.whereObject['pageSize'] = 10;
    this.merAccountRecordService.getRecordList(this.whereObject, data => {
      if (data['return_code'] === '000000') {
        this.dataObject = data['return_data'];
      } else {
        this.modal.error(data['return_msg']);
      }
      this.tableLoading = false;
    });
  }

  /**
   * 查询账户
   */
  getAccount() {
    /*this.merAccountService.getAccount(data => {
      this.accountObject = data['return_data'];
    });*/
  }

  /**
   * 搜索
   * @param whereObject 条件
   */
  search(whereObject: object) {
    this.whereObject = whereObject;
    if (this.searchForm.value.createTime != null) {
      this.searchForm.value.createTimeS = new Date(this.searchForm.value.createTime[0]).getTime();
      this.searchForm.value.createTimeE = new Date(this.searchForm.value.createTime[1]).getTime();
    } else {
      this.searchForm.value.createTimeS = null;
      this.searchForm.value.createTimeE = null;
    }
    this.requestData(1);
  }

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

  showRechargeModal(merId: number) {
    this.rechargeForm.patchValue({ merId});
    this.rechargeModal = true;
  }

  closeRechargeModal() {
    this.rechargeModal = false;
  }

  submitRecharge() {
    for (const i in this.rechargeForm.controls) {
      this.rechargeForm.controls[i].markAsDirty();
      this.rechargeForm.controls[i].updateValueAndValidity();
    }
    if (this.rechargeForm.status == null || this.rechargeForm.status !== 'VALID') {
      this.modal.warning({
        nzTitle: '提示',
        nzContent: '请填写所有必填项',
      });
      return;
    }

    this.merAccountService.recharge(this.rechargeForm.value, data => {
      if (data['return_code'] === '000000') {
        this.modal.success({
          nzTitle: '提示',
          nzContent: '充值成功',
        });
        this.closeRechargeModal();
      } else {
        this.modal.error({
          nzTitle: '提示',
          nzContent: data['return_msg'],
        });
      }
    });
  }

}
