import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, AlertController} from 'ionic-angular';

import { Http,Response } from '@angular/http';
import { ActivityApplyPage } from '../activityApply/activityApply'
import { ActivityConfirmPage } from '../activityConfirm/activityConfirm';
import { AppService } from '../../../service/appHttpService';
import {ActivityDetailPage} from "../activityDetail/activityDetail";
@IonicPage()
@Component({
  selector: 'page-activityList',
  templateUrl: 'activityList.html',
})
export class ActivityListPage {

  items: object[];


  activityType:Array<String> = ['未知','体检','疗休养','培训','工会活动'];
  activityFbObject:Array<String> = ['未知','全体人员','用户组','处事部门'];
  constructor(public navCtrl: NavController,
    public navParams: NavParams,
    public alertCtrl: AlertController,
    public http: Http,
    public appService: AppService) {
      
  } 

  //报名按钮
  goApply(item) {   //活动状态：1：待发布（不可报名），2：发布中（可报名）3:已完成（报名截止）
    console.log("报名按钮："+item.activityState);
    if(item.activityState == 2){
      let flag  = false;
      this.isExitOrderWithActivity(item,result=>{ 
          flag = result;
          if(flag){
            this.appService.toast("您已经报名该活动，请在我的活动中查看！");
          }else{
            this.navCtrl.push("ActivityApplyPage", {item:item});
          }
          //this.navCtrl.push("ActivityApplyPage", {item:item});
      });
      
    }else{
      if(item.activityState == 1){
        this.appService.toast("报名暂未开通！");
      }else{
        this.appService.toast("活动已完成！");
      }
    }
  }

  isExitOrderWithActivity(activity,callback?): any {
    this.appService.ObserverHttpPost("/wisdomgroup/modules/order/isExitOrderWithActivity",activity)
    .toPromise() 
    .then(res => { 
      var data = res.json();     
      callback(data == null ? "[]" : data); 
    }) 
    .catch(error => {    
    });
  }

  //初始化显示活动列表
  ionViewDidEnter(): void {
    this.getActivityList();
    this.addRecodings();
  }

  getActivityList(){   
   

    this.appService.ObserverHttpPost("/wisdomgroup/modules/activity/findAllActivityForAppWithUsergroup",null)

    .subscribe((res: Response) => {
      let data = res.json();
      console.log("activityListData------->",data);    
        this.items =data;
      }, error => {        
          this.appService.alert('网络异常！');    
      }   
    );
  }

  //查看详情
  goDetail(item){
    console.log(item);
    let id = item.id;
    this.navCtrl.push("ActivityDetailPage",{id:id});
  }


  goApplyDetails(item){
    console.log(item);
      // if(item.activityState==2 && item.orderList.length>0){
          this.goDetail(item);
      // }else{
      //     this.goApply(item);
      // }
  }

  doRefresh(refresher) {
    console.log('Begin async operation', refresher);
    setTimeout(() => {
      this.getActivityList();
      console.log('Async operation has ended');
      refresher.complete();
    }, 2000);
  }

  goBack(){
    this.navCtrl.popToRoot();
  }

  //增加阅读记录
  addRecodings(){
    this.appService.ObserverHttpGet("/wisdomgroup/modules/activity/addRecodings", null)
    .subscribe((res: Response) => {
     
    }, error => {
      this.appService.alert('网络异常！');
    }
    );
  }

}
