import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import {Response} from "@angular/http";
import {AppService} from "../../../service/appHttpService";


@IonicPage()
@Component({
  selector: 'page-order-edit',
  templateUrl: 'order-edit.html',
})
export class OrderEditPage {
    order;  //订单信息
    activity ;    //活动
    batch;    //批次
    batchList = [];
    submitOrder = {
        Orderid: '',         //订单id
        Personnumber: 0,    //携带人数
        Orderbz: '',         //报名备注
        Activityid: '',      //关联活动id
        Batchid: '',        //关联批次id
        orderbz_end:''
    };
  constructor(public navCtrl: NavController, public navParams: NavParams,
              public appService: AppService) {
  }

  ionViewDidLoad() {
      this.order = this.navParams.get('item');
      this.activity = this.navParams.get('item').activity;
      this.batch = this.navParams.get('item').batch;
      this.submitOrder.Batchid = this.order.batchid;
      this.submitOrder.Orderbz = this.order.orderbz;
      this.submitOrder.Personnumber = this.order.personnumber;
      console.log(this.navParams.get('item'));
      this.getAllBatch();
  }

  getAllBatch(){
      this.appService.ObserverHttpGet("/wisdomgroup/modules/activity/findAllActivityForAppWithXQ", {"id":this.order.activityid})
          .subscribe((res :Response) => {
                  this.batchList = res.json().batchList;
              }, error => {
                  this.appService.alert('网络异常！');
              }
          );
  }

    //携带人数 数字加减
    add() {
        if(this.submitOrder.Personnumber < this.batch.batchPerNumber){
            this.submitOrder.Personnumber++;
        }
    }

    next() {
        if (this.submitOrder.Personnumber >= 1) {
            this.submitOrder.Personnumber--;
        }
    }

    submit(){
        console.log('报名操作');
       const data  = {
            Orderid: this.order.orderid,         //订单id
            Personnumber: this.submitOrder.Personnumber,    //携带人数
            Orderbz: this.submitOrder.Orderbz,         //报名备注
            Activityid: this.order.activityid,      //关联活动id
            Batchid: this.order.batchid,        //关联批次id
            orderbz_end:''
        };
        this.appService.ObserverHttpPost("/wisdomgroup/modules/order/reelectBatchForOrder", data)
            .subscribe((res: Response) => {
                    if (true) {
                        setTimeout(() => {
                            this.navCtrl.setRoot("MyActivityListPage");
                            this.navCtrl.push("MyActivityListPage");
                        }, 1000);
                    } else {

                    }
                }, error => {
                    this.appService.alert('网络异常！');
                }
            );
    }

}
