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 {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 = [];
    //未报名
    notSignList = [];
    picture: string = AppGlobal.picture;

    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 => {
                }
            );
    }

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

    //已报名
    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 => {
                }
            );
    }

    //代报名
    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 => {
                }
            );
    }

    //取消报名
    cancelSignUp(item){
        this.appService.alert('确定取消该活动报名',(res)=>{
            const data = {
                'userid':item.id,
                'id':item.orderId
            }
            this.appService.ObserverHttpGetOption("/wisdomgroup/modules/order/deleteOrder", data).
                subscribe((res)=>{
                  this.appService.popToastView('取消报名成功!','middle',1500);
                  this.sign();
                  this.signAllService();
            })
        })
    }

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