import {Component, Input, OnInit, Output, EventEmitter} from '@angular/core';
import {IPrize, IUser} from '../../model';
import {ModalService, ToastService} from 'ng-zorro-antd-mobile';
import {ConfigService} from '../../service/config.service';
import {AreaData} from '../../utils/areas';
import {RegService} from '../../service/reg.service';
import {HttpService} from '../../service/http.service';

@Component({
  selector: 'app-get-reward',
  templateUrl: './get-reward.component.html',
  styleUrls: ['./get-reward.component.less']
})
export class GetRewardComponent implements OnInit {
  @Input() list: IPrize[] = [];
  @Input() visible: boolean = false;
  @Output() visibleChange = new EventEmitter<any>();
  areas = AreaData;
  selectedArea: any[] = [];
  userInfo: IUser | undefined = undefined;
  selectedProduct: IPrize | undefined = undefined;
  receiver: string = '';
  receivePhone: string = '';
  receiveAddress: string = '';
  step: number = 1;
  updateParams: any = null;

  constructor(
    private toast: ToastService,
    public config: ConfigService,
    private reg: RegService,
    private http: HttpService,
    private modal: ModalService
  ) {
  }

  ngOnInit(): void {
    this.userInfo = this.config.getUserInfo();
  }

  setVisible(visible: boolean): void {
    this.visible = visible;
    this.visibleChange.emit(this.visible);
  }

  setReward() {
    this.toast.hide();
    if (!this.receiver.trim()) {
      this.toast.show('请输入收件人', 1000);
      return;
    }
    if (!this.reg.mobileReg.test(this.receivePhone)) {
      this.toast.show('请输入11位有效手机号', 1000);
      return;
    }
    if (!this.selectedArea.length) {
      this.toast.show('请选择收获地址', 1000);
      return;
    }
    if (!this.receiveAddress.trim()) {
      this.toast.show('请输入详细地址', 1000);
      return;
    }
    if (!this.selectedProduct) {
      this.toast.show('请选择奖品', 1000);
      return;
    }
    const campaignId = localStorage.getItem('campaignId');
    if (this.userInfo && campaignId) {
      let address = '';
      this.selectedArea.map(item => address += item.label);
      this.updateParams = {
        participantId: this.userInfo.participantId,
        campaignId: campaignId,
        reqChannel: this.config.reqChannel,
        receiver: this.receiver,
        receivePhone: this.receivePhone,
        receiveAddress: address + ' ' + this.receiveAddress,
        productId: this.selectedProduct.productId
      };
      this.step = 2;
    }

  }

  // 申请奖品
  update() {
    if (this.updateParams) {
      this.http.post(this.http.apis.applyReward, {
        data: this.updateParams,
        success: (res: any) => {
          this.modal.alert('提示', '领取成功', [
            {
              text: '确定', onPress: () => {
                this.step = 1;
                this.setVisible(false);
              }
            }
          ]);
        }
      });
    }
  }

  setProductId(item: IPrize) {
    this.selectedProduct = item;
  }

  cancel() {
    this.setVisible(false);
    this.step = 1;
  }

}



