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";
import {ActivitySearchPage} from "../activity-search/activity-search";

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

    items;

    users: object[];

    isNoSignFlag: boolean = false;


    acitivityid: string;
    acitivityName: string;

    type = 1;
    //全部
    allPerson: number;
    allList = [];
    //已报名
    signUp: number;
    signList = [];
    //不参加
    noJoin: number;
    noJoinList = [];
    //未报名
    notSign: number;
    notSignList = [];

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

    ionViewDidLoad() {
        // this.notJoin();
    }

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

        // this.noSign();
        this.sign();
        this.signAllService();
        // this.init();
    }

    // //全部
    // signAll() {
    //     this.type = '1';
    // }

    changeType(type) {
        this.type = type;
    }

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

    getList() {
        this.notSignList = [];
        this.noJoinList = [];
        this.allList = this.items;
        this.items.forEach((res) => {
            if (res.isSignUp == 0 && res.isActivityNo == 0) {
                this.notSignList.push(res);
            } else if (res.isActivityNo == 1) {
                this.noJoinList.push(res);
            }
        });
    }

    //未报名
    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.signList = resultback;
                }, error => {
                    this.appService.alert('网络异常！');
                }
            );
    }

    //不参加
    notJoin() {
        this.isNoSignFlag = true;
        // this.type = '4';
        this.notJoinService();
    }

    notJoinService() {
        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})
    }

    confirmSayNo(item) {
        this.appService.alert('确定设置该用户不参加？', (res) => {
            this.sayNo(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.signAllService();
                }, error => {
                    this.appService.alert('网络异常！');
                }
            );
    }

    search() {
        this.navCtrl.push('ActivitySearchPage', {id: this.acitivityid});
    }
}
