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

import {Http, Response,} from '@angular/http';
import {AppService,AppGlobal} from "../../../../service/http.service";
import {ReplaceApplyPage} from "../replace-apply/replace-apply";
import {ActivitySearchPage} from "../activity-search/activity-search";
import {message} from "../../../../app/main";

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

    items;

    users: object[];

    batchList;   //批次列表
    batchId = '';   //批次ID

    isNoSignFlag: boolean = false;


    acitivityid: string;
    acitivityName: string;

    loading;

    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 loadingCtrl:LoadingController,
                public appService: AppService,
                public toast: ToastController,
    ) {

    }

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

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

        this.findActivityDetail();
    }

    //活动详情
    findActivityDetail() {
        this.appService.ObserverHttpGet("/wisdomgroup/modules/activity/findAllActivityForAppWithXQ", {"id": this.acitivityid})
            .subscribe((res: Response) => {
                    this.batchList =res.json().batchList;
                }, error => {
                }
            );
    }

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

    signAllService() {
        this.loading = this.loadingCtrl.create({
            content: '请稍等...',
            dismissOnPageChange: true,		//打开新的页面则关闭 正在登录
            enableBackdropDismiss: true		//点击页面其他地方则关闭 正在登录
        });
        this.loading.present();
        this.items = [];
        const data = {
            batchId:this.batchId,
        };
        this.appService.ObserverHttpGetAddBody("/wisdomgroup/modules/activity/signAll/", this.acitivityid,data)
            .subscribe((res: Response) => {
                    let resultback = res.json();
                    this.items = resultback;
                    this.getList();
                    this.allPerson = this.items.length;
                }, error => {
                }
            );
    }

    /**
     * 不参加 1， 参加： 0             "isActivityNo",
     *报名 ： 1，不报名 ： 0        "isSignUp"
     */
    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);
            }
        });
        this.loading.dismiss();
    }

    //已报名
    sign() {
        this.items = [];
        this.isNoSignFlag = false;
        const data = {
            batchId:this.batchId,
        }
        this.appService.ObserverHttpGetAddBody("/wisdomgroup/modules/activity/signActivityList/", this.acitivityid,data)
            .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});
    }

    //选择批次
    changeBatch(){
        this.sign();
        this.signAllService();
    }
}
