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 '../../mine/myActivityList/myActivityList';
import {JoinPersonsPage} from '../../joinPersons/joinPersons';
import {MinePage} from "../../tabs/mine/mine";


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

    picture: string;
    isLoad: boolean = false;
    onebatch: any;
    activity: any;
    orderInfo: any;
    order = {
        orderfgconent: '',
        orderstate: null,
        activityid: '',
        orderid: '',
        personnumber:'',
        orderbz:''
    };
    batch;
    //页面按钮为默认取消报名和重选批次
    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 = 4;

    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,
        });
    }


    ngOnInit(): void {
        this.activity = this.navParams.get('order').activity;
        this.order = this.navParams.get('order');
        this.batch = this.navParams.get('order').batch;
        this.order.orderstate = 4;
    }

    selectLevel(type) {
        this.orderStatisfyLevel = type;
    }

    //分隔字符串
    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;
    }


    //重选批次（更新订单）
    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"];  //备注说明

    }

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

    //第二层弹框，参加
    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 => {
                }
            );

    }

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

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

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


        const data = {
            orderfgconent:'',
            orderid: this.order.orderid,
            batchid: this.batch.id,
            activityid: this.order.activityid,
            orderpjlevel: this.orderStatisfyLevel,
            orderstate: '',
            orderconfirm: '1',
            personnumber:this.order.personnumber,
            orderbz:this.order.orderbz
        };
        if (this.order.orderstate == 3) {
            data.orderstate = '3';
        } else if (this.order.orderstate == 4) {
            data.orderstate = '4';
        }

        console.log(data);
        this.appService.ObserverHttpPost("/wisdomgroup/modules/order/confirmEvaluationOrder",data)
        .subscribe((res: Response) => {
            let resultback = res.json();
            if(resultback.status == '200'){
                 //待确认列表移除，全部列表移除该订单
                const index = this.navCtrl.getActive().index;
                this.navCtrl.remove(0, index);
                this.navCtrl.setRoot(MinePage);
                this.navCtrl.parent.select(3);
                this.navCtrl.push("MyActivityListPage");
            }else{
              //失败
            }
          }, error => {
          }
        );
    }

}
