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


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

    activityId;  //活动Id
    personInfo;   //报名人信息
    activity;  //活动信息
    batchList; //批次列表
    item ;      //批次信息
    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() {
    console.log('ionViewDidLoad ReplaceApplyPage');
  }

  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;
                  // for( let i = 0;i<this.batchList.length;i++ ){
                  //     let endDate = this.batchList[i].batchEndDate + " 24:00:00";
                  //     endDate = new Date(endDate).getTime() +'';     //判断是否过截止日期
                  //     if( now > Number(endDate) ){      //已截止
                  //         this.batchList[i].signUp = false;
                  //     }else{    //
                  //         this.batchList[i].signUp = true;
                  //     }
                  // }
              }, (error) => {
                  this.appService.alert('网络异常！');
              }
          );
  }

    //携带人数 数字加减
    add() {
      if(!this.item){
          return false;
      }
        console.log("1:"+this.order.Personnumber);
        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});
        modal.onDidDismiss(res=>{
            this.item = res;
            this.itemName = res.batchName;
        });
        modal.present();
    }

    // 提交订单
    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
        };
        this.order.Batchid = this.item.id;
        this.order.Activityid = this.item.activityId;
        this.appService.ObserverHttpPost("/wisdomgroup/modules/order/create", order)
            .subscribe((res: Response) => {
                    this.navCtrl.pop();
                }, error => {
                    this.appService.alert('网络异常！');
                }
            );
    }

}
