<?php

include_once(_DIR_TO_CLASS."/c_page.php");
class c_page_cab_client_car extends c_page
{
    public $h1='Карточка автомобиля' ;
    public $rec_car=array() ;

    function select_obj_info()
    { if (_CUR_ID)
       { $this->rec_car=PERSONAL()->get_car_by_id(_CUR_ID) ;
         if (!$this->rec_car['pkey']) $this->result=404 ;
       }
    }

    function get_cur_page_meta_tags()
    {
      if ($this->rec_car['pkey']) $this->h1='Карточка автомобиля "'.$this->rec_car['obj_name'].'"' ;
    }

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

    function panel_car_edit()
    { $list_orgs=execSQL('select pkey,obj_name from obj_site_personals where clss=207 and enabled=1 and client_id='.MEMBER()->id) ;
      ?><div id="panel_car_create"><form>
        <table class="info">
         <tr><td>Собственник:</td><td><select name="car[org_id]"><?
                  if (_sizeof($list_orgs)) foreach($list_orgs as $rec_list) {?><option value="<?echo $rec_list['pkey']?>" <?if ($rec_list['pkey']==$this->rec_car['parent']) echo 'selected'?>><?echo $rec_list['obj_name']?></option><?}
              ?></select>
         </td></tr>
         <tr><td>Марка и модель авто:</td><td><input type="text"  data-required name="car[model]" class="big" value="<?echo $this->rec_car['model']?>"></td></tr>
         <tr><td>Госномер:</td><td><input type="text"  data-required name="car[gos_nomer]" class="small" value="<?echo $this->rec_car['obj_name']?>"></td></tr>
         <tr><td>Класс авто:</td><td><input type="text" name="car[typ]" class="small" value="<?echo $this->rec_car['typ']?>"></td></tr>
         <tr><td>Дата окончания полиса ОСАГО:</td><td><input type="text" id="e_osago" data-required name="car[e_osago]" class="small" value="<?echo date('d.m.Y',$this->rec_car['e_osago'])?>"></td></tr>
         <tr><td>Дата окончания полиса СТС:</td><td><input type="text" id="e_sts" data-required name="car[e_sts]" class="small" value="<?echo date('d.m.Y',$this->rec_car['e_sts'])?>"></td></tr>
         <tr><td>Дата окончания полиса ТО:</td><td><input type="text" id="e_to" data-required name="car[e_to]" class="small" value="<?echo date('d.m.Y',$this->rec_car['e_to'])?>"></td></tr>
         <tr><td>Номер лицензии:</td><td><input type="text"  name="car[permit]" class="small" value="<?echo $this->rec_car['permit']?>"></td></tr>
         <tr><td></td><td><input type="submit" class="v1" cmd="save_car" value="Сохранить измерения" validate="form"></td></tr>
         </table></form>
       </form></div>
       <script type="text/javascript">
           $j('#e_osago').datepicker({dateFormat: 'dd.mm.yy',prevText: '<i class="fa fa-chevron-left"></i>',nextText: '<i class="fa fa-chevron-right"></i>',onSelect: function( selectedDate ) {/*$j('#finish').datepicker('option', 'minDate', selectedDate);*/}});
           $j('#e_sts').datepicker({dateFormat: 'dd.mm.yy',prevText: '<i class="fa fa-chevron-left"></i>',nextText: '<i class="fa fa-chevron-right"></i>',onSelect: function( selectedDate ) {/*$j('#finish').datepicker('option', 'minDate', selectedDate);*/}});
           $j('#e_to').datepicker({dateFormat: 'dd.mm.yy',prevText: '<i class="fa fa-chevron-left"></i>',nextText: '<i class="fa fa-chevron-right"></i>',onSelect: function( selectedDate ) {/*$j('#finish').datepicker('option', 'minDate', selectedDate);*/}});
       </script>

       <?
    }


    function CMD_save_car()
     { //damp_array($_POST) ;
       $data=array() ;
       if ($_POST['car']['org_id']!=$this->rec_car['parent'])       $data['parent']=$_POST['car']['org_id'] ;
       if ($_POST['car']['model']!=$this->rec_car['model'])         $data['model']=$_POST['car']['model'] ;
       if ($_POST['car']['gos_nomer']!=$this->rec_car['obj_name'])  $data['obj_name']=$_POST['car']['gos_nomer'] ;
       if ($_POST['car']['typ']!=$this->rec_car['typ'])             $data['typ']=$_POST['car']['typ'] ;
       if ($_POST['car']['permit']!=$this->rec_car['permit'])       $data['permit']=$_POST['car']['permit'] ;
       $e_osago=strtotime($_POST['car']['e_osago']) ;
       $e_sts=strtotime($_POST['car']['e_sts']) ;
       $e_to=strtotime($_POST['car']['e_to']) ;
       if ($e_osago!=$this->rec_car['e_osago'])                     $data['e_osago']=$e_osago ;
       if ($e_sts!=$this->rec_car['e_sts'])                         $data['e_sts']=$e_sts ;
       if ($e_to!=$this->rec_car['e_to'])                           $data['e_to']=$e_to ;
       if (_sizeof($data)) $result=update_rec_in_table('obj_site_personals_cars',$data,'pkey='.$this->rec_car['pkey']) ;
       $this->rec_car=PERSONAL()->get_car_by_id($this->rec_car['pkey']) ;
       //damp_array($data) ;
       //damp_array($result) ;
       if (_sizeof($data) and !$result['found']) {?><div class="red">Не удалось изменить данные по автомобилю</div><?}
       else if ($result['update']) {?><div class="green">Данные изменены</div><?}
       else {?><div class="green">Изменений не обнаружено</div><?}
     }

}



?>
