import {Component} from '@angular/core';
import {IonicPage, ModalController, NavController, NavParams} from 'ionic-angular';
import {Response} from "@angular/http";
import {PersonPage} from "../../surveyManage/modal/person/person";
import {BlockModalPage} from "../block-modal/block-modal";
import {MinePage} from "../../../tabs/mine/mine";
import {AppService} from "../../../../service/http.service";


@IonicPage()
@Component({
    selector: 'page-replace-apply',
    templateUrl: 'replace-apply.html',
})
export class ReplaceApplyPage {

    activityId;  //活动Id
    personInfo;   //报名人信息
    activity;  //活动信息
    batchList; //批次列表
    item;      //批次信息
    selectBatchList = [];   //选择的批次列表
    itemName = '请选择批次';

    //报名信息
    order = {
        Personnumber: 0,    //携带人数
        Orderbz: '',         //报名备注
        Activityid: '',      //关联活动id
        Batchid: '',        //关联批次id
        UserID: '',    //用户id
        Username: '',  //用户名称
        Usertel: '',  //用户手机
    };

    constructor(public navCtrl: NavController, public navParams: NavParams,
                public appService: AppService, public modalCtrl: ModalController) {
    }

    ionViewDidLoad() {
    }

    ionViewDidEnter() {
        this.activityId = this.navParams.get("id");
        this.personInfo = this.navParams.get("personInfo");
        this.getActivety();
    }

    //获取活动信息
    getActivety() {
        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;
                }, (error) => {
                }
            );
    }

    //携带人数 数字加减
    add() {
        if (!this.item) {
            return false;
        }
        if (this.order.Personnumber < this.item.batchPerNumber) {
            this.order.Personnumber++;
        }
    }

    next() {
        if (!this.item) {
            return false;
        }
        if (this.order.Personnumber >= 1) {
            this.order.Personnumber--;
        }
    }

    //选择批次
    selectblock() {
        let modal = this.modalCtrl.create(BlockModalPage, {blockList: this.batchList,applyCount:this.activity.applyCount});
        modal.onDidDismiss(res => {
            let minPerNumber = 5;
            if (res) {
                this.selectBatchList = res;
                this.selectBatchList.forEach((data)=>{
                    if(minPerNumber > data.batchPerNumber){
                        minPerNumber = data.batchPerNumber;
                        this.item = data;
                    }
                });
                this.itemName = '已选择';
            }
        });
        modal.present();
    }

    //报名
    confirmOrder(orderId) {
        //验证信息：
        /**0.对于暂未有批次的。
         1.判断携带人数是否超过限额人数。
         leaveNum: 剩余人数
         applyNum：报名批次数量
         **/

        if (!this.item) {
            this.appService.popToastView("请选择报名批次", 'middle', 1000);
            return false;
        }
        if (new Date().getTime() > new Date(this.item.batchEndDate).getTime()) {
            this.appService.popToastView("该批次已过截止时间", 'middle', 1000);
            return false;
        }
        let leaveNum = this.item.batchLimitNumber - this.item.hasSignUpCount;

        //报名次数 applyCount 1,2,3,4,5,0-> 不限
          if (Number(this.order.Personnumber + 1) > Number(leaveNum)) {
                this.appService.popToastView("报名人数超过了该批次的剩余人数", 'middle', 1000);
                return false;
            } else {
                this.submitOrder();
            }
    }

    // 提交订单
    submitOrder() {
        const order = {
            Personnumber: this.order.Personnumber,    //携带人数
            Orderbz: this.order.Orderbz,         //报名备注
            Activityid: this.activityId,      //关联活动id
            Batchid: this.item.id,        //关联批次id
            UserID: this.personInfo.id,
            Username: this.personInfo.name,
            Usertel: this.personInfo.mobile,
            batchlist:''
        };

        this.selectBatchList.forEach(res=>{
            order.batchlist += res.id + ',';
        });
        this.appService.ObserverHttpPost("/wisdomgroup/modules/order/create", order)
            .subscribe((res: Response) => {
                    this.navCtrl.pop();
                }, error => {
                }
            );
    }

}
