<?php

include_once("c_manager.php");
class c_page_cab_manager_client extends c_page_cab_manager
{
    public $h1='Карточка клиента' ;
    public $rec_member=array() ;
    public $rec_org=array() ;

    function second_menu_config()
    { if ($this->rec_member['pkey'])
      { $this->second_menu['/cab/manager/client/'.$this->rec_member['pkey'].'/']=array('title'=>'Карточка клиента','class'=>'fa fa-user-md') ;
        $this->second_menu['/cab/manager/client/'.$this->rec_member['pkey'].'/rekvizit/']=array('title'=>'Реквизиты','class'=>'fa fa-user-md') ;
        $this->second_menu['/cab/manager/client/'.$this->rec_member['pkey'].'/pass/']=array('title'=>'Смена пароля','class'=>'fa fa-user-md') ;
        //$this->second_menu[$this->rec_member['__href'].'sert/']=array('title'=>'Сертификат','class'=>'fa fa-user-md') ;
      }
    }

    function select_obj_info()
    { if (_CUR_ID)
       { $this->rec_member=ACCOUNTS()->get_member_by_id(_CUR_ID) ;
         if (!$this->rec_member['pkey']) { $this->result=404 ; /*$this->access_denited=1 ; $this->access_denited_comment='Персонал #'._CUR_ID.' не найден' ;*/ }
         else if ($this->rec_member['rol']!=6) { $this->access_denited=1 ; $this->access_denited_comment='Персонал #'._CUR_ID.' не является клиентом' ; }
         else
         { $client_org_id=execSQL_value('select pkey from obj_site_personals where clss=207 and enabled=1 and client_id='.$this->rec_member['pkey'].' order by c_data limit 1') ;
           if (!$client_org_id) $client_org_id=adding_rec_to_table('obj_site_personals',array('clss'=>207,'parent'=>1,'client_id'=>$this->rec_member['pkey'],'obj_name'=>$this->rec_member['obj_name'])) ;
           $this->rec_org=PERSONAL()->get_org_by_id($client_org_id,array('get_ext_info'=>1)) ;
           if (MEMBER()->id==_CUR_ID) $this->allow_access_to_member=MEMBER()->id  ; // разрешаем доступ к странице для текущего пользователя
         }
       }
    }

    function get_cur_page_meta_tags()
    {
      if ($this->rec_member['pkey']) $this->h1='Карточка клиента "'.$this->rec_member['obj_name'].'"' ;
    }

    function block_main()
    { $this->page_title() ;
      $this->panel_member_edit() ;
    }


    function panel_member_edit()
    {  $rec=$this->rec_member ;  //damp_array($this->rec_member) ; //damp_array($_SESSION['member']) ;
       if (!$rec['UID'])
       {   $rec['UID']=md5(uniqid(rand(),true)) ;
           update_rec_in_table('obj_site_account',array('UID'=>$rec['uid']),'pkey='.$rec['pkey'],array('debug'=>0)) ;
       }
       ?><div id="panel_member_edit"><form enctype="multipart/form-data">
        <table class="info">
         <?if (MEMBER()->id!=$rec['pkey']){?><tr><td>Состояние:</td><td><input type="hidden" name=member_edit[enabled] value="0"> <input type=checkbox name=member_edit[enabled] value="1" <?if ($this->rec_member['enabled']) echo 'checked'?>></td></tr><?}?>
         <tr><td>Название организации:</td><td><input data-required type="text" name="member_edit[obj_name]" class="big" value="<?echo htmlspecialchars($rec['obj_name'])?>"></td></tr>
         <tr><td>Контактное лицо:</td><td><input data-required type="text" name="member_edit[contact_name]" class="big" value="<?echo htmlspecialchars($rec['contact_name'])?>"></td></tr>
         <tr><td>Контактный email:</td><td><input data-required type="text" name="member_edit[email]" class="big" value="<?echo htmlspecialchars($rec['email'])?>"></td></tr>
         <tr><td>Контактный телефон:</td><td><input data-required type="text" name="member_edit[phone]" class="big" value="<?echo htmlspecialchars($rec['phone'])?>"></td></tr>
         <tr><td>Логин:</td><td><input data-required class=login type=text name=member_edit[login] value="<?echo addslashes($rec['login'])?>"></td></tr>
         <tr><td>Пароль:</td><td><?echo addslashes($rec['password'])?></td></tr>
         <tr><td>UID:</td><td><?echo addslashes($rec['UID'])?></td></tr>
         <tr><td></td><td><input type="submit" class=v1 cmd="save_member" value="Сохранить" validate="form"></td></tr>
       </table>
       </form></div><?
      //damp_array(MEMBER());
    }

    function CMD_save_member()
     { //damp_array($_POST['member_edit']) ;
       $res=ACCOUNTS()->member_save($this->rec_member['pkey'],$_POST['member_edit']) ;

       if (!$res) echo '<div class="red">Изменений не обнаружено</div>' ;
       else { echo '<div class="green">Данные сохранены</div>' ;
              update_rec_in_table('obj_site_personals',array('obj_name'=>$_POST['member_edit']['obj_name']),'pkey='.$this->rec_org['pkey']) ;
              $this->select_obj_info() ;
            }
     }

}



?>