import {Component, OnInit} from '@angular/core';
import {HttpService} from '../../service/http.service';
import {ModalService, ToastService} from 'ng-zorro-antd-mobile';
import {ConfigService} from '../../service/config.service';
import {IPrize, IUser} from '../../model';
import {Router} from '@angular/router';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.less']
})
export class HomeComponent implements OnInit {
  isLogin: boolean = false;
  userInfo: IUser | undefined = undefined;
  prizeList: IPrize[] = [];

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

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

  exit() {
    this.modal.alert('提示', '确定注销登陆吗?', [
      {text: '取消'},
      {
        text: '确定', onPress: () => {
          this.userInfo = this.config.exitUser();
        }
      }
    ]);
  }

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

}
