import {Component} from '@angular/core';
import {IonicPage, ModalController, NavController, NavParams, ToastController} from 'ionic-angular';
import {AppGlobal, AppService} from "../../../service/appHttpService";
import {ContactModalPage} from "../contact-modal/contact-modal";
import {PersonListPage} from "../person-list/person-list";
import {message} from "../../../app/main";


@IonicPage()
@Component({
    selector: 'page-block-list',
    templateUrl: 'block-list.html',
})
export class BlockListPage {

    orgid;
    orgName;
    contactPersons;
    picture: string = AppGlobal.picture;
    constructor(public navCtrl: NavController, public navParams: NavParams,
                public appService: AppService, public modalCtrl: ModalController,
                public toastCtrl: ToastController) {
    }

    ionViewDidLoad() {
        this.orgid = this.navParams.get("orgid");
        this.orgName = this.navParams.get("orgName");
        this.initList();
    }

    initList() {
        const data = {
            'groupId': this.orgid
        }
        this.appService.ObserverHttpGetOption('/wisdomgroup/modules/telpBookPerson', data)
            .subscribe((res) => {
                if (res.json().data) {
                    this.contactPersons = res.json().data.list;
                }
            })
    }

    showPersonModal() {
        const toast = this.toastCtrl.create(message);
        let peoModal = this.modalCtrl.create(PersonListPage);
        peoModal.onDidDismiss(res => {
            if (res) {
                const data = {
                    'telpBookPersonList': [{
                        'telpbookId': this.orgid,
                        'linkUserid': res.id,
                        'linkUsername': res.name
                    }]
                };

                this.appService.ObserverHttpPostData('/wisdomgroup/modules/telpBook', data)
                    .subscribe((res) => {
                        if (res.json().apiResult.errcode == '0') {
                            toast.setMessage('添加成功');
                            toast.present();
                            this.initList();
                        } else {
                            toast.setMessage(res.json().apiResult.data);
                            toast.present();
                        }
                    })
            }
        });
        peoModal.present();
    }


    removeItem(item) {
        const data = {
            userId: item.id
        };
        this.appService.ObserverHttpDeteleteData('/wisdomgroup/modules/telpBookPerson/', this.orgid, data)
            .subscribe((res) => {
                if (res.json().errcode == '0') {
                    this.initList();
                }
            })
    }

    //人员详情
    contactPersonInfo(contactPerson) {
        this.navCtrl.push("ContractPersoninfoPage", {"id": contactPerson.id});
    }

}
