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

import {Http, Response} from '@angular/http';

import {AppGlobal, AppService} from '../../../service/appHttpService';
import {JoinDetailPage} from '../../activity-track/joinDetail/joinDetail';
import {BatchDetailPage} from "../batch-detail/batch-detail";

@IonicPage()
@Component({
    selector: 'page-activityDetail',
    templateUrl: 'activityDetail.html',
})
export class ActivityDetailPage {

    activityId;  //活动ID
    batches: object[];
    activity = {
        batchList: [],
        activityIntro:''
    };
    picture: string = AppGlobal.picture;
    batchList = [];
    //页面报名保存展示
    isCover: boolean = false;
    //活动详情显示
    introduce: string;
    hasOrder = false;


    constructor(public navCtrl: NavController,
                public navParams: NavParams,
                public alertCtrl: AlertController,
                public http: Http,
                public appService: AppService, public toast: ToastController) {

    }

    ionViewDidEnter() {
        const index = this.navCtrl.length() -3;
       if(index > 1){
           this.navCtrl.remove(3, index);
       }
        this.activityId = this.navParams.get('id');
        this.findMyOrderByOrderId();
    }

    findMyOrderByOrderId() {
        let now = new Date().getTime();
        this.appService.ObserverHttpGet("/wisdomgroup/modules/activity/findAllActivityForAppWithXQ", {"id": this.activityId})
            .subscribe((res: Response) => {
                    this.activity = res.json();
                    this.batchList = this.activity.batchList;
                    this.subIntroduce(this.activity.activityIntro);
                    for (let i = 0; i < this.batchList.length; i++) {
                        let endDate = this.batchList[i].batchEndDate + " 24:00:00";
                        endDate = new Date(endDate).getTime() + '';     //判断是否过截止日期
                        if (now > Number(endDate)) {      //已截止
                            this.batchList[i].signUp = false;
                        } else {    //
                            this.batchList[i].signUp = true;
                        }
                        if( this.batchList[i].order ){
                            this.hasOrder = true;
                        }
                    }
                }, error => {
                }
            );
    }


    goJoinDetail() {
        this.navCtrl.push("JoinDetailPage");
    }

    //批次详情
    goBatchDetail(item) {
        console.log(this.activity);
        this.navCtrl.push('BatchDetailPage', {batch: item, activity: this.activity});
    }

    //活动介绍限定65个字符串，多余以省略号代替
    subIntroduce(str: string) {
        if (!str) this.introduce = '';
        if (str.length <= 65) this.introduce = str;
        if (str.length > 65) this.introduce = str.substr(0, 65) + "...";
    }

    //具体介绍
    hidden() {
        this.isCover = false;
    }

    show() {
        this.isCover = true;
    }

    //不参加
    sayNo() {

        if(this.hasOrder){
            this.appService.popToastView('您已报名，请先取消报名再点击','middle',1500);
            return false;
        }

        const confirm = this.alertCtrl.create({
            title: '确定不参加该活动?',
            buttons: [
                {
                    text: '取消',
                    handler: () => {
                        console.log('Disagree clicked');
                    }
                },
                {
                    text: '确定',
                    handler: () => {
                        this.appService.ObserverHttpPostAdd("/wisdomgroup/modules/activityNon/admin/", this.activityId)
                            .subscribe((res: Response) => {
                                    this.navCtrl.pop();
                                }, error => {
                                }
                            );
                    }
                }
            ]
        });
        confirm.present();
    }

    //参加
    sayYes(){
        const confirm = this.alertCtrl.create({
            title: '确定参加该活动?',
            buttons: [
                {
                    text: '取消',
                    handler: () => {
                        console.log('Disagree clicked');
                    }
                },
                {
                    text: '确定',
                    handler: () => {
                        this.appService.ObserverHttpPostAdd("/wisdomgroup/modules/activityNon/delete/", this.activityId)
                            .subscribe((res: Response) => {
                                    this.navCtrl.pop();
                                }, error => {
                                }
                            );
                    }
                }
            ]
        });
        confirm.present();
    }

}
