import {Component, ViewChild} from '@angular/core';
import {IonicPage, NavController, NavParams, Slides} from 'ionic-angular';
import {ActivityApplyPage} from "../activityApply/activityApply";
import {Response} from "@angular/http";
import {AppService} from "../../../service/appHttpService";


declare var Swiper;
@IonicPage()
@Component({
  selector: 'page-batch-detail',
  templateUrl: 'batch-detail.html',
})
export class BatchDetailPage {
    @ViewChild('contentSlides') contentSlides: Slides;

    batch={
        batchEndDate:'',
        signUp:true,
        order:null,
        batchName:''
    };
    activity;
    swiper;
    swiperIndex = 0;
  constructor(public navCtrl: NavController, public navParams: NavParams,
              public appService:AppService) {
  }

  ionViewDidLoad() {
      this.batch =  this.navParams.get('batch');
      this.activity =  this.navParams.get('activity');
  }

  initSwiper(){
      this.swiper = new Swiper('.pageMenuSlides .swiper-container', {
          //设置slider容器能够同时显示的slides数量(
          slidesPreView: 3,
          //slide之间的距离（单位px）
          spaceBetween: 0,
          //断点设定：根据屏幕宽度设置某参数为不同的值
          breakpoints: {
              1024: {
                  slidesPerView: 3,
                  spaceBetween: 0
              },
              768: {
                  slidesPerView: 3,
                  spaceBetween: 0
              },
              640: {
                  slidesPerView: 3,
                  spaceBetween: 0
              },
              320: {
                  slidesPerView: 3,
                  spaceBetween: 0
              }
          }
      });
  }

    selectPageMenu(index) {
        this.swiperIndex = index;
        //切换页面
        this.contentSlides.slideTo(index);
    }

    setStyle(index) {

    }

    slideChanged() {
        //getActiveIndex()获得当前页面的index
        let index = this.contentSlides.getActiveIndex();
        this.setStyle(index);
        //s切换菜单
        this.swiper.slideTo(index, 300);

    }

    goApply(){
      console.log(this.activity);
      this.navCtrl.push('ActivityApplyPage',{
          activity:this.activity,
          item:this.batch
      });
    }

    //取消报名（订单）
    sureCancelOrder(): void {
        this.appService.alert('确定取消报名么？', res => {
            this.cancelOrder();
        });
    }

    cancelOrder(): void {
        this.appService.ObserverHttpGet("/wisdomgroup/modules/order/deleteOrder", { "id": this.batch.order.orderid })
            .subscribe((res: Response) => {
                    //let data = res.json();
                    this.appService.alert('报名已取消！');
                    this.navCtrl.pop();
                }, error => {
                    this.appService.alert('网络异常！');
                }
            );

    }
}
