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

import {SearchPage} from '../../contact/search/search';
import {ContactListPage} from '../../contact/contactList/contactList';
import {AddBlockPage} from "../../contact/add-block/add-block";
import {BlockListPage} from "../../contact/block-list/block-list";

@Component({
    selector: 'page-contact',
    templateUrl: 'contact.html'
})
export class ContactPage {

    //组织部门列表
    organizations: Array<string> = [];
    ownerList: Array<string> = [];

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

    }

    //ionViewWillEnter
    ionViewDidEnter(): void {
        //加载组织列表：initOrganizeList
        this.initOrganizeList();
        this.initOwnerList();
    }

    initOrganizeList(): void {
        //this.appService.ObserverHttpGet("/wisdomgroup/app/getAllOrganization",null)
        //隐藏显示
        this.appService.ObserverHttpGetData("/wisdomgroup/sysmanagement/org/getAllOrganization", {order: 0})
            .subscribe((res: Response) => {
                    let data = res.json();
                    this.organizations = data;

                    //移除父节点
                    for (let index = 0; index < this.organizations.length; index++) {
                        const element = this.organizations[index];
                        if (element["organizationLevel"] == '1') {
                            var flag = index;
                        }
                    }
                    this.organizations.splice(flag, 1);
                }, error => {
                    this.appService.alert('网络异常！');
                }
            );
    }

    //我的分组
    initOwnerList() {
        const data = {
            pageSize: '100',
            pageNumber: '1'
        };
        this.appService.ObserverHttpGetData("/wisdomgroup/modules/telpBook", data)
            .subscribe((res: Response) => {
                    let data = res.json();
                    this.ownerList = data.data.list;
                }, error => {
                    this.appService.alert('网络异常！');
                }
            );
    }

    search() {
        this.navCtrl.push("SearchPage");
    }

    //跳转至部门下联系人列表页面
    usualContactOrgPersons(organization) {
        this.navCtrl.push("ContactListPage", {"orgid": organization.id, "orgName": organization.name});
    }

    //跳转至部门下联系人列表页面
    usualContactBlockPersons(organization) {
        this.navCtrl.push("BlockListPage", {"orgid": organization.id, "orgName": organization.usergroupName});
    }

    //跳转至常用联系人列表
    usualContactPersons() {
        this.navCtrl.push("ContactListPage");
    }

    //添加分组
    usualContactAdd() {
        this.navCtrl.push('AddBlockPage');
    }

    removeItem(item) {
        let alert = this.alertCtrl.create({
            title: '删除分组',
            message: '确定删除该分组及分组下的联系人?',
            buttons: [
                {
                    text: '取消',
                    role: 'cancel',
                    handler: () => {
                    }
                },
                {
                    text: '确定',
                    handler: () => {
                        this.appService.ObserverHttpDeteleteOption('/wisdomgroup/modules/telpBook/',item.id)
                            .subscribe((res)=>{
                                if(res.json().errcode == '0'){
                                    this.initOwnerList();
                                }
                            })
                    }
                }
            ]
        });
        alert.present();
    }

}
