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 {ICompany, IPrize, IUser} from '../../model';
import {ActivatedRoute, Router} from '@angular/router';
import {PostMsgService} from '../../service/post-msg.service';

@Component({
  selector: 'app-share-invite',
  templateUrl: './share-invite.component.html',
  styleUrls: ['./share-invite.component.less']
})
export class ShareInviteComponent implements OnInit {
  showModal: boolean = false; // 更多奖品modal
  visible: boolean = false; // 领取奖品modal
  shareCompany: ICompany[] = []; // 累计邀请成功的企业
  total: number = 0; // 累计邀请成功的企业数量
  userInfo: IUser | undefined = undefined;
  list: ICompany[] = [];
  prizeList: IPrize[] = [];

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

  showVisible() {
    this.visible = true;
  }

  ngOnInit(): void {
    this.userInfo = this.config.getUserInfo();
    if (!this.userInfo) {
      // location.replace(this.config.rootUrl + '/login');
      this.router.navigate(['/login']).then();
      return;
    }
    this.getList();
  }

  setShowModal() {
    this.showModal = !this.showModal;
  }

  // 申请企业列表
  getList() {
    const campaignId = localStorage.getItem('campaignId');
    if (this.userInfo && campaignId) {
      this.http.get(this.http.apis.findPageByParticipant, {
        data: {
          campaignId: campaignId,
          participantId: this.userInfo.participantId,
          status: '2'
        },
        success: (res: any) => {
          this.shareCompany = res.records || [];
          this.total = res.total;
          if (res.total > 0) {
            this.getProductList(res.total);
          }
        }
      });
    }
  }

  // 获取活动奖品列表
  getProductList(total: number) {
    setTimeout(() => {
      const campaignId = localStorage.getItem('campaignId');
      if (campaignId) {
        this.http.get(this.http.apis.productPage, {
          data: {
            pageNum: 1,
            pageSize: 20,
            campaignId: campaignId
          },
          success: (res: any) => {
            this.prizeList = res.records.filter((item: IPrize) => item.meetCnt <= total).sort((a: any, b: any) => a.meetCnt - b.meetCnt);
          }
        });
      }
    }, 500);

  }

  exit() {
    this.modal.alert('提示', '确定注销登陆吗?', [
      {text: '取消'},
      {
        text: '确定', onPress: () => {
          this.userInfo = this.config.exitUser();
          // location.replace(this.config.rootUrl + '/index');
          this.router.navigate(['/index']).then();
        }
      }
    ]);
  }

  share(type: string) {
    const userInfo = this.config.getUserInfo();
    this.postMsgService.share(type + '&' + userInfo.participantId);
  }
}
