import {Component, OnInit} from '@angular/core';
import {HttpService} from '../../service/http.service';
import {RegService} from '../../service/reg.service';
import {ModalService, ToastService} from 'ng-zorro-antd-mobile';
import {ConfigService} from '../../service/config.service';
import {ActivatedRoute} from '@angular/router';

const lock: any[] = [];
for (let i = 0; i < 13; i++) {
  lock.push(i);
}

@Component({
  selector: 'app-share-application',
  templateUrl: './share-application.component.html',
  styleUrls: ['./share-application.component.less']
})
export class ShareApplicationComponent implements OnInit {
  locks: any[] = lock;
  contactName: string = '';
  contactPhone: string = '';
  entName: string = '';
  params: { participantId: string; campaignId: string; reqChannel: number } | undefined = undefined;

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

  ngOnInit(): void {
    // url获取参数
    this.activatedRoute.queryParams.subscribe(res => {
      if (res) {
        if (res.participantId && res.campaignId && res.reqChannel) {
          // i:participantId 邀请人id；a:campaignId 活动id  ;c: reqChannel：渠道 int
          this.params = {
            participantId: res.participantId,
            campaignId: res.campaignId,
            reqChannel: Number(res.reqChannel),
          };
        }
      }
    });
  }

  update = () => {
    if (!this.params) {
      this.toast.show('参数丢失，请重新进入页面');
      return;
    }
    if (!this.contactName.trim()) {
      this.toast.hide();
      this.toast.show('请输入联系人');
      return;
    }
    if (!this.reg.mobileReg.test(this.contactPhone)) {
      this.toast.hide();
      this.toast.show('请输入11位有效手机号');
      return;
    }
    if (!this.entName.trim()) {
      this.toast.hide();
      this.toast.show('请输入公司名称');
      return;
    }
    this.http.post(this.http.apis.recommendedSave, {
      data: {
        ...this.params,
        contactName: this.contactName,
        contactPhone: this.contactPhone,
        entName: this.entName,
      },
      success: (res: any) => {
        this.modal.alert('提示', '提交成功', [
          {
            text: '确定', onPress: () => {
              history.back();
            }
          }
        ]);

      }
    });
  };

}
