<?php
include_once("c_ot_working_vpf.php");
class c_page_cab_ot_working_vpf_edit extends c_page_cab_ot_working_vpf
{
 public $h1='Должность: редактор списка ВПФ' ;

 function block_main()
 {  $this->page_title() ;
    $this->panel_filter_info() ;
    ?><br><?
    $this->panel_setting_vpf() ;
 }

  function panel_setting_vpf()
      {   //damp_array($this->filter) ;
          ?><script src="/AddOns/jstree/dist/jstree.min.js"></script>
              <link rel="stylesheet" href="/AddOns/jstree/dist/themes/default/style.css" />
            <?
          ?><div id="panel_select_item_vpf"><?
          $arr=array() ;
          $recs=execSQL('select pkey,parent,concat(number," ",obj_name) as obj_name from list_vpf',0) ;

          $org_id=($this->filter['org_id'])? $this->filter['org_id']:0 ;
          $otdel_id=($this->filter['otdel_id'])? $this->filter['otdel_id']:0 ;
          $working_id=($this->filter['working_id'])? $this->filter['working_id']:0 ;
          $_usl=array() ;
          $_usl[]='org_id='.$org_id ;
          if ($otdel_id) $_usl[]='otdel_id='.$otdel_id ;
          if ($working_id) $_usl[]='work_id='.$working_id ;
          $usl=implode(' and ',$_usl) ;
          if ($working_id) $checked_ids=execSQL_row('select vpf_id,pkey from list_workings_fvp where '.$usl) ;
          convert_list_to_tree2($recs,$arr) ;
          $arr[1]['name']='ВПФ' ;

          $org_name=($org_id)? PERSONAL()->get_org_names_by_ids($org_id):'' ;
          $otdel_name=($otdel_id)? PERSONAL()->get_org_names_by_ids($otdel_id):'' ;
          $work_name=($working_id)? execSQL_value('select obj_name from list_workings where pkey='.$this->filter['working_id']):'' ;

          ?><div id="panel_search">Поиск: <input type="text" id="plugins4_q" value="" class="input"></div><?
          if (!$working_id) {?><div id="panel_mode">&nbsp;<input type="radio" id="check_mode" name=check_mode value="set" checked> Добавить выбранные ВПФ <input type="radio" id="check_mode" name=check_mode value="unset"> Удалить выбранные ВПФ</div><?}
          ?><div class="clear"></div>
            <div id="panel_vpf_tree"><? $this->generate_tree($arr,$checked_ids) ; ?></div>
         </div>
          <div class="center"><button id="save_checked_items" org_id="<?echo $org_id?>" otdel_id="<?echo $otdel_id?>" working_id="<?echo $working_id?>">Сохранить</button></div>
         <script type="text/javascript">jQuery(document).ready(function()
          {

            var to = false;
            $j('#plugins4_q').keyup(function ()
            { if(to) { clearTimeout(to); }
              to = setTimeout(function ()
              { var v = $j('#plugins4_q').val();
                $j('#panel_vpf_tree').jstree(true).search(v);
              }, 250);
            });

              $j('#panel_vpf_tree').jstree({
                "plugins" : [ "wholerow", "checkbox",'search' ],
                "checkbox" : {"keep_selected_style" : true,'whole_node':false,'tie_selection':false,
                             }
              });

            $j('#save_checked_items').on('click',function()
            { var selectedElmsIds = [];
              var selectedElms = $j('#panel_vpf_tree').jstree("get_checked", true);
              $j.each(selectedElms, function() {
                  selectedElmsIds.push(this.id);
              });
              var mode='as_is' ;
              if ($j('#check_mode').length) mode=$j('#check_mode:checked').val() ;

              send_ajax_request({cmd:'ot/save_checked_vpf',items:selectedElmsIds,org_id:$j(this).attr('org_id'),otdel_id:$j(this).attr('otdel_id'),working_id:$j(this).attr('working_id'),mode:mode}) ;
              console.log(selectedElmsIds) ;
            }) ;


          });</script>
         <style type="text/css">
              div#panel_select_section_vpf select{width:290px;font-size:14px;}
              div#panel_select_item_vpf{font-size:14px;}
              #panel_search{margin-bottom:10px;float:left}
              #panel_mode{margin-bottom:10px;float:left}
              #panel_info{margin-bottom:10px;}
              #panel_vpf_tree{margin-bottom:10px;}
              #plugins4_q{padding:4px; border-radius:4px; border:1px solid silver;width:250px;}
         </style>
         <?


      }

    function generate_tree($recs,$checked_ids)
       {  if (_sizeof($recs)) foreach($recs as $id=>$item)
           { ?><ul>
                  <li data-jstree='{ "opened" : <?echo ($id==1)? 'true':'true'?>,"checked":<?echo (isset($checked_ids[$id]))? 'true':'false'?>}' id="item_<?echo $id?>"><?echo $item['name']?>
                  <? unset($item['name']) ;
                     if (_sizeof($item)) $this->generate_tree($item,$checked_ids) ;
                  ?>
                  </li>
               </ul><?
           }
       }







}



?>
