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

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

import {AppService} from '../../../service/appHttpService';
import {ActivityStatisticService} from '../../../service/activityStatisticService';
import {ReplaceApplyPage} from "../replace-apply/replace-apply";
import {message} from "../../../app/main";

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

    items: object[];

    users: object[];

    isNoSignFlag: boolean = false;


    acitivityid: string;
    acitivityName: string;

    type = '1';
    //全部
    allPerson: number;
    //已报名
    signUp: number;
    //不参加
    noJoin: number;
    //未报名
    notSign: number;

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

    ionViewDidEnter() {
        /** 订单状态   （1.待出行(已报名)、2.待确认（已报名未确认）、3.报名未出行、4.已完成  （3，4均为确认状态））*/
        this.acitivityid = this.navParams.get("id"); //活动id
        this.acitivityName = this.navParams.get("name"); //活动名称
        //初始化加载未报备
        this.items = [];
        this.noSign();
        this.sign();
        this.notJoin();
        this.signAll();
    }

    //全部
    signAll(){
        this.type = '1';
        this.appService.ObserverHttpGetAdd("/wisdomgroup/modules/activity/signAll/", this.acitivityid)
            .subscribe((res: Response) => {
                    let resultback = res.json();
                    this.items = resultback;
                    this.allPerson = this.items.length;
                }, error => {
                    this.appService.alert('网络异常！');
                }
            );
    }

    //未报名
    noSign() {
        this.type = '2';
        this.items = [];
        this.isNoSignFlag = false;
        this.appService.ObserverHttpPost("/wisdomgroup/modules/activity/notSignActivityList", {activityid: this.acitivityid})
            .subscribe((res: Response) => {
                    let resultback = res.json();
                    this.items = resultback;
                    this.notSign = this.items.length;
                }, error => {
                    this.appService.alert('网络异常！');
                }
            );
    }

    //已报名
    sign() {
        this.type = '3';
        this.items = [];
        this.isNoSignFlag = false;
        this.appService.ObserverHttpGetAdd("/wisdomgroup/modules/activity/signActivityList/", this.acitivityid)
            .subscribe((res: Response) => {
                    let resultback = res.json();
                    this.items = resultback;
                    this.signUp = this.items.length;
                }, error => {
                    this.appService.alert('网络异常！');
                }
            );
    }

    //不参加
    notJoin() {
        this.isNoSignFlag = true;
        this.type = '4';
        this.appService.ObserverHttpGetAdd("/wisdomgroup/modules/activityNon/", this.acitivityid)
            .subscribe((res: Response) => {
                    this.items = res.json().data;
                    this.noJoin = this.items.length;
                }, error => {
                    this.appService.alert('网络异常！');
                }
            );
    }

    //代报名
    goSignUp(item) {
        this.navCtrl.push('ReplaceApplyPage', {id: this.acitivityid, personInfo: item})
    }

    //不参加
    sayNo(item) {
        let msg = this.toast.create(message);
        const data = {
            'userId':item.id,
            'userName':item.name
        };
        this.appService.ObserverHttpForm("/wisdomgroup/modules/activityNon/", this.acitivityid,data)
            .subscribe((res: Response) => {
                    msg.setMessage('已保存该用户不参加');
                    msg.present();
                    this.signAll();
                    this.notJoin();
                }, error => {
                    this.appService.alert('网络异常！');
                }
            );
    }


}
