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

import { Http, Response, } from '@angular/http';

import { AppService, AppGlobal } from '../../service/appHttpService';
import { MyActivityListPage } from '../myActivityList/myActivityList';
import { JoinPersonsPage } from '../joinPersons/joinPersons';


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

  picture: string;
  isLoad:boolean = false;
  onebatch:any;
  activity:any;
  orderInfo:any;
  order = {
    orderfgconent:'',
  }; 
  //页面按钮为默认取消报名和重选批次
  modelflag = 1;
  showParticipantBtn: boolean = false; 
  //页面报名保存展示
  //isCover: boolean = false;
  //活动详情显示
  introduce: string;
  isCover: boolean = false; //是否显示弹出框
  isIntroduce: boolean = false; //是否显示具体介绍
  first: boolean = false;  //第一层弹框
  second: boolean = false;  //第二层弹框
  third: boolean = false;  //第三层弹框
  fourth: boolean = false; //第四层弹框
  isSuccess: boolean = false; //是否提交成功
  isError: boolean = false; //是否提交失败

  activityTypeFlag:Array<String> = ['未知','体检','疗休养','培训','工会活动'];  
  orderStatisfyLevel: Array<String> = ['未知', '非常满意', '满意', '一般','不满意'];

  noticeArry: object[];

  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public alertCtrl: AlertController,
    public http: Http,
    public appService: AppService,
    public toast: ToastController,) {
   
  }

  //跳转人员参与情况列表页面
  joinPeople(order) {
    this.navCtrl.push("JoinPersonsPage",{
      batchid:order.batchid,
      batch:order.batch,
    });
  }


   //活动介绍限定一行17个字符串，两行57个字符串，多余以省略号代替
   subIntroduce(str: string) {
    if (!str) this.introduce = '';
    if (str.length <=17) this.introduce = str;
    if (str.length >17 && str.length <=25) this.introduce = str.substr(0, 17) + "...";
    if(str.length>25 && str.length <=42) this.introduce = str;
    if (str.length > 42) this.introduce = str.substr(0, 42) + "...";
  }


 ngOnInit():void{
    this.orderInfo = this.navParams.get("lookOrderDetails");
    console.log("order:"+this.orderInfo);
    this.activity = this.orderInfo["activity"];

    if (this.activity.activityIMG) {
      this.picture = AppGlobal.picture + this.activity.activityIMG;
      this.isLoad = true;
    }
    
    this.onebatch = this.orderInfo["batch"];

    this.noticeArry =  this.strSplit(this.onebatch.batchNotice);

    this.subIntroduce(this.activity["activityIntro"]) ;
    if(this.orderInfo["orderstate"]==1){ //批次报名截止日期小于当前日期
      let signflag =  Date.parse(this.orderInfo.batch.batchEndDate.toString())<Date.parse(new Date().toString());
      if(signflag){
        this.showParticipantBtn = true;
      }
    }
    //页面按钮为确认评价
    if(this.orderInfo["orderstate"]==2){
        this.modelflag = 2;
    }
    if(this.orderInfo["orderstate"]==3){
      this.modelflag = 3;
    }
    if(this.orderInfo["orderstate"]==4){
      this.modelflag = 4;
    }

  }

   //分隔字符串
   strSplit(str):object[]{
    let temp = str.split(/[\n]/g);
        for(let i =0;i<temp.length;i++){
          if(temp[i] == ""){
              temp.splice(i, 1);
              //删除数组索引位置应保持不变
              i--;
          }
      }
      return temp;
  }

  hidden() {
    this.isCover = false;
    this.isIntroduce = false;
  }
  show() {
    this.isCover = true;
    this.isIntroduce = true;
  }

  //取消报名（订单）
  sureCancelOrder(orderid): void {
    this.appService.alert('确定取消报名么？', res => {
      this.cancelOrder(orderid);
    });
  }

  cancelOrder(orderid): void {

    this.appService.ObserverHttpGet("/wisdomgroup/modules/order/deleteOrder", { "id": orderid })
      .subscribe((res: Response) => {
        //let data = res.json();
            this.appService.alert('报名已取消！');         
            this.navCtrl.push("MyActivityListPage");    
        }, error => {        
            this.appService.alert('网络异常！');    
        }   
      );

  }

  //重选批次（更新订单）
  reelectBatch(order): void {
    this.navCtrl.push('ActivityApplyPage', { changeOrderInfo: order, item: order.activity });
  }


   //确认评价按钮,第一层弹框
   evaluation(item) {
    this.isCover =true;
    this.first =true;
    this.order["orderid"] =  item["orderid"];
    this.order["batchid"] =  item["batchid"];
    this.order["activityid"] =  item["activityid"];
    this.order["orderstate"] = '2';  //确认状态

    this.order["personnumber"] =  item["personnumber"]==null?0:item["personnumber"]; //携带人数
    this.order["orderbz"] =  item["orderbz"];  //备注说明

  }


  //第二层弹框，参加
  yes() {
    this.first = false;
    this.second = true;
  }

  //第三层弹框，未参加
  no() {
    this.first = false;
    this.third = true;
  }

  //确定未参加
  sure() {
    this.third = false;
    this.isCover = false;
    //未出行（更改订单状态为：报名未出行；确认方式为app）  
    this.order["orderstate"] = '3';  //报名未出行
    this.order["orderconfirm"] = '1' ;  //手机app确认
    
    this.appService.ObserverHttpPost("/wisdomgroup/modules/order/confirmEvaluationOrder",this.order)
    .subscribe((res: Response) => {
        let resultback = res.json();
        if(resultback.status == '200'){
             //待确认列表移除，全部列表移除该订单
            //let orderid = this.order["orderid"];
            //this.allOrder.splice(this.allOrder.indexOf(orderid),0)["orderstate"]  = '3';
            //this.allOrder.splice(this.allOrder.indexOf(orderid),1)    
            //this.waitSureOrder.splice(this.waitSureOrder.indexOf(orderid),1)  
            this.navCtrl.push("MyActivityListPage");    
        }else{
          //失败
        }             
      }, error => {        
          this.appService.alert('网络异常！');    
      }   
    );
    
  }

  //否定为参加，跳到第一层
  not() {
    this.third = false;
    this.first = true;
  }

  //no是对应的满意度，跳转到第四层
  next(no: number) {
    this.second = false;
    this.fourth = true;
    this.order["orderpjlevel"] = no; //满意度评价等级
  }

  //提交建议 (确认出行)
  commit(orderfgconent: HTMLInputElement){
    //确认出行：更改订单状态为完成，订单满意度评价，意见，确认方式
    this.order["orderstate"] = '4';  //已完成
    this.order["orderconfirm"] = '1' ;  //手机app确认

     let temp_Orderbz =  encodeURIComponent(this.order["orderbz"]) ;
     this.order["orderbz"] = temp_Orderbz;

    //  let temp_orderfgconent =  encodeURIComponent(this.order.orderfgconent) ;
    //  this.order["orderfgconent"] = temp_orderfgconent;

    let temp_orderfgconent =  encodeURIComponent(orderfgconent.value) ;
    this.order["orderfgconent"] = temp_orderfgconent;


    this.appService.ObserverHttpPost("/wisdomgroup/modules/order/confirmEvaluationOrder",this.order)
    .subscribe((res: Response) => {
        let resultback = res.json();
        if(resultback.status == '200'){
             //待确认列表移除，全部列表移除该订单
            //let orderid = this.order["orderid"];
            //this.allOrder.splice(this.allOrder.indexOf(orderid),1) 
            //this.waitSureOrder.splice(this.waitSureOrder.indexOf(orderid),1)  
            this.navCtrl.push("MyActivityListPage");
            if(true){
              this.fourth =false;
              this.isSuccess=true;
              setTimeout(() => {
                this.isCover=false;
                this.isSuccess=false;
              }, 2000);
            }
            
        }else{
          //失败
        }             
      }, error => {        
          this.appService.alert('网络异常！');    
      }   
    );
  }

}
