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";
import { DomSanitizer } from "@angular/platform-browser";
import {ActivityConfirmPage} from "../activityConfirm/activityConfirm";

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

    isFlag = false;    //是否过截止日期
    batch={
        batchEndDate:'',
        signUp:true,
        order:null,
        batchName:'',
        batchNotice:null
    };
    activity;
    order = {
        orderid:''
    };
    swiper;
    swiperIndex = 0;
  constructor(public navCtrl: NavController, public navParams: NavParams,
              public appService:AppService, public sanitizer:DomSanitizer) {
  }

  ionViewDidLoad() {
      this.batch =  this.navParams.get('batch');
      let con = this.batch.batchNotice.replace(/\r?\n/g, "<br />");
      this.batch.batchNotice = this.sanitizer.bypassSecurityTrustHtml(con);
      this.activity =  this.navParams.get('activity');
      if( this.batch.order ){
          this.order = this.batch.order;
      }

      //判断是否过截止日期
      const nowDate = new Date().getTime();
      const endDate = new Date(this.batch.batchEndDate).getTime();
      console.log(this.batch);
      if( nowDate  > endDate ){
          this.isFlag = true;   //已过截止日期
      }

  }

  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();
        });
    }

    //订单确认
    reelectBatch(): void {
        this.appService.ObserverHttpPost("/wisdomgroup/modules/order/findMyOrderByOrderId", {"orderid": this.order.orderid})
            .subscribe((res: Response) => {
                    let data = res.json();
                    this.navCtrl.push('ActivityConfirmPage',{activity:data.activity,order:data});
                }, error => {
                    this.appService.alert('网络异常！');
                }
            );
    }

    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('网络异常！');
                }
            );

    }

}
