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

import { ContactListPage } from '../contactList/contactList';
import {MyBlockPage} from "../my-block/my-block";

@IonicPage()
@Component({
  selector: 'page-contract-personinfo',
  templateUrl: 'contract-personinfo.html',
})
export class ContractPersoninfoPage {

  contactPersonInfo = {
      gender:'',
      telephone:'',
      email:'',
      abbreviation:''
  };
  id;

  userRelEntity = {
    relUserId:'',
    moduleName:'appcontact',
  };
    edit = {
        email:false,
        telephone:false,
        room:false
    };
    email;
    abbreviation;
    telephone;

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

  ionViewDidLoad() {
    this.id = this.navParams.get("id");
    this.getPersonInfo();
  }

  getPersonInfo(){
      const data = {
          'userId':this.id
      };
      this.appService.ObserverHttpGet("/wisdomgroup/modules/telpBookPerson",data)
          .subscribe((res: Response) => {
                  this.contactPersonInfo = res.json().data;
                  this.email = this.contactPersonInfo.email;
                  this.abbreviation = this.contactPersonInfo.abbreviation;
                  this.telephone = this.contactPersonInfo.telephone;
              }, error => {
                  this.appService.alert('网络异常！');
              }
          );
  }

  //添加常用联系人
  addcontactPersons(){
    const data = {
        'relUserId':this.id,
        'moduleName':'appcontact'
    }
    this.appService.ObserverHttpPostOption("/wisdomgroup/app/contact/addGeneralContactPersons",data)
    .subscribe((res: Response) => {
        let data = res.json();
        console.log("data:"+data);
        if(data["result"] == '2'){
          this.appService.popToastView('该联系人已在常用联系人中！','top',2000);
        }else if(data["result"] == '3'){
          this.appService.popToastView('添加常用联系人失败！','top',2000);
        }else{
          //this.appService.popToastView('添加常用联系人成功！','top',2000);
          this.navCtrl.push("ContactListPage"); 
        }
        //this.navCtrl.pop();
      }, error => {        
          this.appService.alert('网络异常！');    
      }   
    );
  }


  //移除常用联系人
  cancelGenaralPersons(){
    this.appService.ObserverHttpPost("/wisdomgroup/app/contact/deleteGeneralContactPersons",this.userRelEntity)
    .subscribe((res: Response) => {
        let data = res.json();
        console.log("data:"+data);
        this.navCtrl.push("ContactListPage"); 
      }, error => {        
          this.appService.alert('网络异常！');    
      }   
    );
  }

  call(number){
    console.log("拨打:" + number);
    window.location.href = "tel:" + number;
  }

    editFun(type){
      switch (type){
          case 'email':{
              this.edit.email = true;
              break;
          };
          case 'telephone':{
              this.edit.telephone = true;
              break;
          };
          case 'room':{
              this.edit.room = true;
              break;
          }
      }
    }

  //跳转设置分组
  usualContactSet(){
      this.navCtrl.push('MyBlockPage');
  }

    editDone(){
      const paramsEmail = {
          email:this.email
      };
        const paramsTelePhone = {
            telephone:this.telephone
        };
        const paramsAbbreviation = {
            abbreviation:this.abbreviation
        };
        this.appService.ObserverHttpPut("/wisdomgroup/modules/telpBookPerson/email/",paramsEmail,this.id)
            .subscribe((res: Response) => {
                    let data = res.json();
                }, error => {
                    this.appService.alert('网络异常！');
                }
            );

        this.appService.ObserverHttpPut("/wisdomgroup//modules/telpBookPerson/telephone/",paramsTelePhone,this.id)
            .subscribe((res: Response) => {
                    let data = res.json();
                }, error => {
                    this.appService.alert('网络异常！');
                }
            );

        this.appService.ObserverHttpPut("/wisdomgroup//modules/telpBookPerson/abbreviation/",paramsAbbreviation,this.id)
            .subscribe((res: Response) => {
                    let data = res.json();
                }, error => {
                    this.appService.alert('网络异常！');
                }
            );

        this.edit = {
            email:false,
            telephone:false,
            room:false
        };
        this.getPersonInfo();
    }

}
