<? global $vis_language; ?>
<section id="breadcrumb" class="row m-b m-l-none m-r-none">
   <div class="col-sm-6">
      <h3 class="m-b-xs text-black"><span><? echo $vis_language['TXTDEPARTMENT']; ?> </span></h3>
      <ul class="breadcrumb bg-empty no-border m-b-xxs no-padder">
         <li><a class="link" href="#/home/quick_view/<? echo $vis_language['TXTHOME']; ?>"><?echo $vis_language['TXTHOME'];?></a></li>
         <li><a class="link" href="#/setting/setting_menu/<? echo $vis_language['TXTSETTINGS'];?>" onclick="$('#vis_workpanel_div').visExecution('load_module','setting',{'vis_module':'setting','vis_operation':'setting_menu'});"><? echo $vis_language['TXTSETTINGS'];?></a></li>
         <li> <a href="#/setting/view_departments/<? echo $vis_language['TXTDEPARTMENT'];?>" onclick="$('#vis_workpanel_div').visExecution('load_module','<? echo $vis_language['TXTDEPARTMENT'];?>',{'vis_module':'setting','vis_operation':'view_departments'});" class="link"><? echo $vis_language['TXTDEPARTMENT']; ?></a></li>
         <li class="active"><? echo $vis_language['TXTUPDATEDEPARTMENT'];?></li>
      </ul>
   </div>
</section>
<section class="padder">
   <section class="panel panel-default">
      <header class="panel-heading sub_header"><? echo $vis_language['TXTUPDATEDEPARTMENT'];?></header>
      <div class="panel-body">
      <form class="form-horizontal" id="vis_department_form" name="vis_department_form" method="post" >
         <div class="form-group">
            <label class="col-sm-2 control-label"><? echo $vis_language['TXTDOMAIN']; ?></label>
            <div class="col-sm-10">
               <label class="control-label"><? echo $department_details['domain_name']; ?></label>
                  </div>
                  </div>
               <div class="form-group">
               <label class="col-sm-2 control-label"><? echo $vis_language['TXTTITLE']; ?></label>
               <div class="col-sm-10">
                  <input class="form-control input-sm" type="text" name="vis_departmentname" id="vis_departmentname" value="<? echo htmlentities($department_details['department_name'], ENT_COMPAT, 'UTF-8'); ?>">
               </div>
            </div>
            <div class="form-group">
               <label class="col-sm-2 control-label"><? echo $vis_language['TXTAUTORESPONDEREMAIL']; ?></label>
               <div class="col-sm-10">
                  <select class="form-control input-sm selectpicker" <? if(count($templates)>10) echo 'data-live-search="true"';?>  name="vis_autorespond_id">
                    <option><? echo $vis_language['TXTNONE']; ?></option>
                     <?
                        for($i=0;$i<count($templates);$i++) echo '<option value="'.$templates[$i]['etemplate_id'].'"'.(($templates[$i]['etemplate_id']==$department_details['autoresponder_id'])?' selected="selected"':'').'>'.$templates[$i]['etemplate_name'].'</option>'; ?>
                  </select>
               </div>
            </div>
            <div class="form-group">
               <label class="col-sm-2 control-label" ><? echo $vis_language['TXTDISPLAYORDER']; ?></label>
               <div class="col-sm-10">
                  <input class="form-control input-sm" type="text" name="vis_displayorder" id="vis_displayorder" value="<? echo $department_details['display_order']; ?>">
               </div>
            </div>
            <div class="form-group">
               <label class="col-sm-2 control-label"><? echo $vis_language['TXTACCESS']; ?></label>
               <div class="col-sm-10">
                  <div class="radio i-checks pull-left m-r-lg">
				  <label>
                  <input type="radio" name="vis_access" id="vis_access_yes" checked="checked" value="0" <? if($department_details['access_type']!=1) echo ' checked="checked"'; ?> /><i></i><? echo $vis_language['TXTPUBLIC']; ?>
                  </label></div>
                  <div class="radio i-checks pull-left">
				  <label>
                  <input type="radio" name="vis_access" id="vis_access_no" value="1" <? if($department_details['access_type']==1) echo ' checked="checked"'; ?> /><i></i><? echo $vis_language['TXTPRIVATE']; ?>
                  </label></div>
               </div>
            </div>
            <div class="form-group">
               <label class="col-sm-2 control-label"><? echo $vis_language['TXTISPAID']; ?></label>
               <div class="col-sm-10">
                  <div class="radio i-checks pull-left m-r-lg">
				  <label>
                  <input type="radio" name="vis_paid" id="vis_paid_yes" value="1"<? if($department_details['is_paid']==1) echo ' checked="checked"'; ?> /><i></i><? echo $vis_language['TXTYES']; ?>
                  </label></div>
                  <div class="radio i-checks pull-left">
				  <label>
                  <input type="radio" name="vis_paid" id="vis_paid_no"  value="0"<? if($department_details['is_paid']!=1) echo ' checked="checked"'; ?> /><i></i><? echo $vis_language['TXTNO']; ?>
                  </label></div>
               </div>
            </div>
            <div class="form-group">
               <label class="col-sm-2 control-label"><? echo $vis_language['TXTAUTORESPONDER']; ?></label>
               <div class="col-sm-10">
                  <div class="radio i-checks pull-left m-r-lg">
				  <label>
                  <input type="radio" name="vis_autorespond" id="vis_autorespond_yes" value="1"<? if($department_details['autoresponder']==1) echo ' checked="checked"'; ?> /><i></i><? echo $vis_language['TXTYES']; ?>
                  </label></div>
                  <div class="radio i-checks pull-left">
				  <label>
                  <input type="radio" name="vis_autorespond" id="vis_autorespond_no" value="0"<? if($department_details['autoresponder']!=1) echo ' checked="checked"'; ?> /><i></i><? echo $vis_language['TXTNO']; ?>
                  </label></div>
               </div>
            </div>
            <div class="form-group">
               <label class="col-sm-2 control-label"><? echo $vis_language['TXTREGISTRATIONREQUIRED']; ?></label>
               <div class="col-sm-10">
                  <div class="radio i-checks pull-left m-r-lg">
				  <label>
                  <input type="radio" name="vis_registered" id="vis_registered_yes" value="1"<? if($department_details['registration']!=0) echo ' checked="checked"'; ?> /><i></i><? echo $vis_language['TXTYES']; ?>
                  </label></div>
                  <div class="radio i-checks pull-left">
				  <label>
                  <input type="radio" name="vis_registered" id="vis_registered_no" value="0"<? if($department_details['registration']==0) echo ' checked="checked"'; ?>/><i></i><? echo $vis_language['TXTNO']; ?>
                  </label></div>
               </div>
            </div>
            <div class="form-group">
               <label class="col-sm-2 control-label"><? echo $vis_language['TXTSTAFFGROUPACCESS']; ?></label>
               <div class="col-sm-10">
                  <label class="checkbox-inline i-checks">
                  <input type="checkbox" id="vis_team_access_all" onchange="toggle_team_list();" name="vis_team_access_all"><i></i><? echo $vis_language['TXTALLTEAM']; ?></label>
               </div>
            </div>
            <div class="form-group" id="vis_team_section">
               <label class="col-sm-2 control-label"></label>
               <div class="col-sm-10">
				<div class="dept_access_list">
                  <? $totTeamAccess = 0;
                     for($i=0; $i<count($staff_group); $i++) {
                         $staff_group_display_list.=$staff_group[$i]['group_id'].",";
                         if($staff_group[$i]['department_access']==0) $access_arr = 0;
                         else $access_arr = explode(',',$staff_group[$i]['department_access']);
                      ?>
                  <label class="checkbox-inline i-checks">
				  <? if($access_arr==0 || in_array($department_details['department_id'],$access_arr)){$totTeamAccess++;} ?>
                  <input type="checkbox" value="<? echo $staff_group[$i]['group_id']; ?>" <?php echo ($access_arr==0 || in_array($department_details['department_id'],$access_arr))?'checked="checked"':""; ?> name="vis_staff_department_access[]" /> <i></i><? echo $staff_group[$i]['group_name']; ?></label><br/>
                  <? } ?>
                  <input type="hidden" value="<? echo trim($staff_group_display_list,','); ?>" name="staff_group_display_list"/>
                  <? if($totTeamAccess == count($staff_group)){echo "<script>$('#vis_team_section').hide();$('#vis_team_access_all').attr('checked','checked')</script>";}?>
               </div>
            </div>
            </div>
            <div class="form-group">
               <label class="col-sm-2 control-label"><? echo $vis_language['TXTSTAFFLIST']; ?></label>
               <div class="col-sm-10">
                  <label class="checkbox-inline i-checks">
                  <?
                        $is_allstaffselected=0;
                        for($i=0; $i<count($staff_list); $i++) {
                            if($staff_list[$i]['department_access']==0) $access_arr = 0;
                            elseif($staff_list[$i]['department_access']==-2) $access_arr = explode(',',$staff_list[$i]['group_department_access']);
                            else $access_arr = explode(',',$staff_list[$i]['department_access']);
                            if($access_arr==0 || in_array($department_details['department_id'],$access_arr)) $is_allstaffselected=1;
                            else { $is_allstaffselected=0; break; }
                        }
                  ?>
                  <input type="checkbox" id="vis_staff_access_all" <?php echo ($is_allstaffselected==1)?'checked="checked"':""; ?> onchange="toggle_dept_list(); $(this).visInterface('toogle_list_check', {'vis_form':'#vis_department_form', 'vis_list_status_class':'.vis_list_checkbox'})" name="vis_staff_access_all"><i></i> <? echo $vis_language['TXTALLSTAFF']; ?></label>
               </div>
            </div>
            <div id="vis_dept_list_section" class="form-group" style="<?= ($is_allstaffselected==1)?'display:none':'';?>">
               <label class="col-sm-2 control-label"></label>
               <div class="col-sm-10">
				<div class="dept_access_list">
                  <?
                     for($i=0; $i<count($staff_list); $i++) {
					 $staff_display_list.=$staff_list[$i]['staff_id'].",";
                     if($staff_list[$i]['department_access']==0) $access_arr = 0;
                     elseif($staff_list[$i]['department_access']==-2) $access_arr = explode(',',$staff_list[$i]['group_department_access']);
                     else  $access_arr = explode(',',$staff_list[$i]['department_access']);
                      ?>
                  <label class="checkbox-inline i-checks">
                  <input type="checkbox" value="<? echo $staff_list[$i]['staff_id']; ?>" <?php echo ($access_arr==0 || in_array($department_details['department_id'],$access_arr))?'checked="checked"':""; ?> name="vis_staff_access[]" /> <i></i><? echo $staff_list[$i]['first_name'].' '. $staff_list[$i]['last_name']; ?></label><br/>
                  <? } ?>
				  <input type="hidden" value="<? echo trim($staff_display_list,','); ?>" name="staff_display_list" style="display:none;" />
               </div>
               </div>
            </div>
            <div class="form-group">
               <label class="col-sm-2 control-label" ><? echo $vis_language['TXTSTATUS']; ?></label>
               <div class="col-sm-1">
                  <label class="switch  <? if($department_details['is_enabled']==0) echo 'radio-no-uncheck'; ?>  ">
                  <input type="checkbox"  name="vis_status"  id="vis_status" <? if($department_details['is_enabled']==1) echo ' checked="checked"'; ?> >
                  <span></span>
                  </label>
               </div>
            </div>
            <? if($language_details!=-1) { ?>
            <div class="form-group">
               <div class="col-sm-12"><h5><? echo $vis_language['MSGLANGTRANSFORDEPARTMENT']; ?></h5></div>
            </div>
            <div class="line b-b line-lg pull-in"></div>
            <br/>
            <?
               for($i=0;$i<count($language_details);$i++) {
				?>
            <div class="form-group">
               <label class="col-sm-2 control-label"><? echo $language_details[$i]['language_name']; ?></label>
               <div class="col-sm-10">
                  <input class="form-control input-sm" type="text" name="vis_language_<? echo $language_details[$i]['language_id']; ?>" type="text" value="<? echo $department_lang[$language_details[$i]['language_id']]; ?>" />
               </div>
            </div>
            <?
               }
               }  ?>
            <div class="line b-b line-lg pull-in"></div>
            <br/>
            <div class="form-group">
               <div class="col-sm-4 col-sm-offset-2">
                  <input type="hidden" name="vis_department_id" value="<? echo $department_details['department_id']; ?>" />
                  <button type="button" class="btn btn-primary" onclick="$('#vis_workpanel_div').visExecution('submit_form',{'vis_module':'setting', 'vis_operation':'update_department', 'vis_form':'vis_department_form'})"><? echo $vis_language['TXTUPDATE']; ?></button>
               </div>
			   </div>
      </form>
      </div>
   </section>
</section>
<script>
   $(".selectpicker").selectpicker();
   function toggle_dept_list(){
       if($('#vis_staff_access_all').is(":checked")) $('#vis_dept_list_section').hide();
       else $('#vis_dept_list_section').show();
	   $('.dept_access_list').each(function(index) {
        $(this).find('input[type="checkbox"]').removeAttr('checked');
       });
   }
    function toggle_team_list(){
       if($('#vis_team_access_all').is(":checked")) $('#vis_team_section').hide();
       else $('#vis_team_section').show();
	   $('.dept_access_list').each(function(index) {
        $(this).find('input[type="checkbox"]').removeAttr('checked');
       });
   }
</script>
