import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams, ToastController, Content} from 'ionic-angular';
import {Response} from '@angular/http';
import {Storage} from '@ionic/storage';
import {AppGlobal, AppService} from '../../../service/appHttpService';

import {ContractPersoninfoPage} from '../contract-personinfo/contract-personinfo';


@IonicPage()
@Component({
    selector: 'page-contactList',
    templateUrl: 'contactList.html'
})

export class ContactListPage {

    //联系人列表
    //contactPersons: Array<string> = [];
    contactPersons: Array<string>[];

    //常用联系人列表（只存联系人id）
    picture: string = AppGlobal.picture;
    generalContactPersons: Array<string> = [];

    //部门id
    orgid: string;
    orgName: string;

    constructor(public navCtrl: NavController,
                public navParams: NavParams,
                public storage: Storage,
                public appService: AppService,
                public toast: ToastController,) {

    }

    ionViewDidEnter(): void {

        this.orgid = this.navParams.get("orgid");
        this.orgName = this.navParams.get("orgName");

        //加载联系人(常用联系人，部门下人员)
        if (this.orgid != null && this.orgid != '') {
            this.getGeneralContactPersons();  //常用联系人ids数组
            this.initOrganizationPersons(); //加载部门下人员
        } else {
            this.initGeneralContactPersons();   //加载常用联系人
        }
    }

    //加载常用联系人
    initGeneralContactPersons() {
        const data = {
            'order': 1
        };
        this.appService.ObserverHttpGetOption("/wisdomgroup/app/contact/getGeneralContactPersons", data)
            .subscribe((res: Response) => {
                    let data = res.json();
                    this.contactPersons = data;
                    this.contactPersons.forEach(element => {
                        //常用联系人的部门信息
                        this.getOrgName(element["id"], result => {
                            element["orgName"] = result["orgName"];
                        });
                        //是否已添加到常用联系人中：
                        //false:按钮为：添加常用联系人
                        //常用联系人：默认为true 有移除常用联系人按钮
                        element["hasgeneralpersonsflag"] = true;
                    });
                }, error => {
                }
            );
    }

    //根据用户id获取部门信息
    getOrgName(id, callback?): any {
        this.appService.ObserverHttpPost("/wisdomgroup/app/getOrgName", {"userid": id})
            .toPromise()
            .then(res => {
                var data = res.json();
                callback(data == null ? "[]" : data);
            })
            .catch(error => {
            });
    }

    //加载部门下人员
    initOrganizationPersons() {
        //this.appService.ObserverHttpGet("/wisdomgroup/app/getAllUserByOrgid",{"orgId":this.orgid})
        this.appService.ObserverHttpGet("/wisdomgroup/sysmanagement/user/getAllUserByOrgid", {"orgId": this.orgid})
            .subscribe((res: Response) => {
                    let data = res.json();
                    this.contactPersons = data;
                    this.contactPersons.forEach(element => {
                        //部门人员的部门信息
                        element["orgName"] = this.orgName;
                        //暂时设置为false：添加联系人按钮
                        //后期要判断是否已添加过该联系人
                        //默认是添加按钮：false
                        element["hasgeneralpersonsflag"] = false;

                        //判断是否在常用联系人中存在：移除按钮
                        for (let index = 0; index < this.generalContactPersons.length; index++) {
                            const strid = this.generalContactPersons[index];
                            if (element["id"] == strid) {
                                element["hasgeneralpersonsflag"] = true;
                                break;
                            }
                        }

                    });
                }, error => {
                }
            );
    }

    //跳转联系人信息页面
    contactPersonInfo(contactPerson) {
        this.navCtrl.push("ContractPersoninfoPage", {"id": contactPerson.id});
    }

    // search(){
    //   if(this.orgid != null && this.orgid != ''){
    //       this.navCtrl.push("SearchPage",{"orgid":this.orgid,"orgName":this.orgName});  //组织部门下搜索
    //   }else{        //全局搜索
    //       this.navCtrl.push("SearchPage");
    //   }
    // }

    //获取常用联系人ids,数组：此方法主要是在部门人员列表中判断：
    //当前人员是否在常用联系人中来判别：添加/移出联系人按钮
    getGeneralContactPersons() {
        const data = {
            'order': 1
        };
        this.appService.ObserverHttpGetOption("/wisdomgroup/app/contact/getGeneralContactPersons", data)
            .subscribe((res: Response) => {
                    let data = res.json();
                    data.forEach(element => {
                        this.generalContactPersons.push(element["id"]);
                    });
                }, error => {
                }
            );
    }

    goBack() {
        this.navCtrl.popToRoot();
    }

}
