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

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

export class SearchPage {

    searchValue = '';
    picture: string = AppGlobal.picture;
    contactPersons: Array<string>[];

    //组织部门id
    orgId: string = '';
    orgName: string = '';

    constructor(public navCtrl: NavController,
                public navParams: NavParams,
                public appService: AppService) {

        if (!typeof(this.navParams.get("orgid")) == undefined) {
            this.orgId = this.navParams.get("orgid");
        }
        if (!typeof(this.navParams.get("orgName")) == undefined) {
            this.orgName = this.navParams.get("orgName");
        }

    }

    ionViewDidLoad(){
        this.searchUser();
    }

    clearValue() {
        this.searchValue = "";
    }

    searchUser() {
        const data = {
            name: this.searchValue,
            pageNumber: 1,
            pageSize: 100,
        };
        this.appService.ObserverHttpNoForm('/wisdomgroup/app/getUserByName', data)
            .subscribe((res) => {
                this.contactPersons = res.json().list;
            })
    }


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

    //根据用户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 => {
            });
    }
}
