{% extends "Acc_Reportform.html" %}
{% load i18n %}

{%  block search_div  %}
                <div id="id_search" class="div_box" style="margin-bottom:0px !important; padding-bottom:2px;">
                    <h1 style="margin-bottom:5px;">{% trans "查询" %}</h1>
                    <table style="width:99%;">
                    <tr>
                        <td>
                            <div id="id_div_query_acclevelset">
                               {{ searchform_AccLevelSet }}
                            </div> 
                            <div id="id_div_query_accdoor" style="display:None;">
                               {{ searchform_AccDoor }}
                            </div>
                            <div id="id_div_query_employee" style="display:None;">
                               {{ searchform_Employee }}
                            </div>
                        </td>
                        <td width="160" valign="bottom">  
                                <div style="text-align:right;width:160px;" class="topSearchBtn"><!--height:20px;-->
                                   
                                    <span class="Link_blue1">
                                        <span class="action_topSearch floatL"></span><a href="javascript:void(0)" id="id_header_search" class="floatL">{% trans "查询" %}</a>
                                    </span>
                                   <!-- {% block advance_search%}
                                    <span class="Link_blue1" id="id_advanced_search" >
                                        <span class="action_advancedSearch floatL"></span><a href="javascript:void(0)" class="floatL">{% trans "高级" %}</a>
                                    </span> 
                                    {% endblock %}--> 
                                    <span class="Link_blue1">
                                        <span class="action_topClear floatL"></span><a href="javascript:void(0)" id="id_header_clear" class="floatL">{% trans "清除" %}</a>
                                    </span>
                                    <div class="clear"></div>
                                </div>
                       </td>
                    </tr>
                    </table>
                </div>
 {%  endblock  %}

{% block id_main_div %}
    {% if request.user|HasPerm:"contenttypes.can_EmpLevelByLevelPage" %}
    <div><li id="id_model_export" style="display:none"><a class="action_Model_Export" href="javascript:void(0)" alt="export"></a></li></div>
	<li id="id_radio" style="margin:10px 10px 10px 10px;">
        	<input type="radio" Checked="true" name="name_radio1" id="id_radio_level" value="0"/> <label id="lab_radio_level"></label>
        	<input type="radio" name="name_radio1" id="id_radio_door" value="1"/> <label id="lab_radio_door"></label>
        	<input type="radio" name="name_radio1" id="id_radio_emp" value="2"/> <label id="lab_radio_emp"></label>
   </li>
	<div id="emplevel_bylevel" style="margin:5px 0px 10px 0px;">
        <!-- 权限组列表 -->
		<div id="id_datalist" class="div_box" style="float:left; width:43%; margin:0px 0px 0px 10px; _margin:0px 0px 0px 5px;"><h1></h1>
		</div>
		<!-- 对应人员列表 -->
        
		<div id="id_extend" class="div_box" style="float:right; width:48%; margin:0px 10px 0px 0px;_margin:0px 5px 0px 0px;"><h1 style="width:80%">{% trans "浏览指定权限组的人员" %}</h1>
		</div>
    </div>
    {% endif %}<!--contenttypes.can_EmpLevelByLevelPage-->
{% endblock %}

{% block getdatalist %}
    {% if request.user|HasPerm:"contenttypes.can_EmpLevelByLevelPage" %}
    $("#id_datalist h1").text(gettext("权限组列表"));
    
    $("#lab_radio_level").text(gettext("以权限组查询"))
    $("#lab_radio_door").text(gettext("以门查询"))
    $("#lab_radio_emp").text(gettext("以人员查询"))
    
    $("#id_sys_cur_model").val("AccLevelSet");
    
    
    //处理三个checkbutton选择框
    function process_check_button(row_data)
    {
        if ($("input[name='name_radio1']")[0].checked)	 //以权限组查看
        {
            $("#id_sys_cur_app").val("personnel"); 
            $("#id_sys_cur_model").val("Employee");
            var level_name = $($("#id_datalist tr[data="+row_data+"]").find("td")[0]).text();
            $("#id_sys_cur_exporttitle").val(gettext("浏览权限组：")+level_name+gettext(' 的开门人员')); 
            $('#id_extend').get(0).g.init_query=["acclevelset__id__exact="+row_data];
            $('#id_extend').get(0).g.load_data();
            $("#id_extend h1").text(gettext("浏览权限组：")+level_name+gettext(" 的开门人员")); 
        }
        else if($("input[name='name_radio1']")[1].checked)//以门查看
        { 
            $("#id_sys_cur_app").val("personnel"); 
            $("#id_sys_cur_model").val("Employee"); 
            var door_name = $("#id_datalist tr[data="+row_data+"]").find("td")[1].title;  
            if (door_name==null)
            {
                door_name="";
            }	
            $('#id_extend').get(0).g.init_query = ["acclevelset__door_group__id="+row_data];
            $('#id_extend').get(0).g.load_data();
            $("#id_extend h1").text(gettext("浏览 ")+door_name+gettext(" 的开门人员"));
            $("#id_sys_cur_exporttitle").val(gettext("浏览 ")+door_name+gettext(" 的开门人员")); 
        }
        else//以人查看
        {
            $("#id_sys_cur_app").val("iaccess");  
            $("#id_sys_cur_model").val("AccDoor"); 
            if(row_data != "")
            {
                var emp = $($("#id_datalist tr[data="+row_data+"]").find("td")[0]).text();
                $("#id_sys_cur_exporttitle").val(gettext("浏览 ")+emp+gettext("可以进出的门")); 
                $("#id_extend h1").text(gettext("浏览 ")+emp+gettext("可以进出的门"));
                $('#id_extend').get(0).g.init_query = ["acclevelset__emp__id="+row_data];
                $('#id_extend').get(0).g.load_data();
            }
            else
            {
                $("#id_extend h1").text(gettext("当前不存在人员"));
            }
        }
    }
    
    function onclick_left()
    {
        var $tr2 = $("#id_datalist #id_tbl tbody tr:first");
        if($tr2.length != 0)
        {
            $("#id_datalist #id_tbl tbody tr:first").click();
           // $('#id_extend').get(0).g.init_query = ["acclevelset__id__exact=0"]; ///tomorrow done!
            //$('#id_extend').get(0).g.load_data();            
        }
        else//左侧没有数据，右侧也不许显示数据
        {
            $('#id_extend').get(0).g.init_query = ["acclevelset__id__exact=0"];
            $('#id_extend').get(0).g.load_data();
        }
        return true;
    }
    
    
    $("#id_datalist").model_grid({
        "model_url": "{{ dbapp_url }}iaccess/AccLevelSet/",
        "model_actions": false,
        "object_actions": true,
        "fields_show": ['level_name','level_timeseg','emp_count'],
        "sort_fields":[],
        "init_query": [],
        "obj_edit": false,
        "record_per_page": 20,
        "max_no_page": 30,
        "async": true,
        "multiple_select": null,
        "need_plus_sign": false,
        "row_operations":[],
        "disabled_actions": ["_change","_delete",'OpDelEmpFromLevel','OpAddEmpToLevel'],
        "init_after_get_jdata": function(){	            
            var left_data = 0;//右边记录对应的左边记录id
            //处理第一次加载时权限组为空时，右侧不显示
            var $tr = $("#id_datalist #id_tbl tbody tr:first");
            left_data = $tr.attr("data");
            
            var init_query = [];
            if($tr.length != 0)
            {
                var level_name2 = $($tr.find("td")[1]).text();
                $("#id_extend h1").text(gettext("浏览权限组")+level_name2+gettext("的开门人员"));
                init_query = ["acclevelset__id__exact="+$tr.attr("data")];
            }
            else
            {
                $("#id_extend h1").text(gettext("当前不存在权限组"));
                init_query = ["acclevelset__id__exact=0"];
            }
            
            $("#id_extend").model_grid({
                "model_url": "{{ dbapp_url }}personnel/Employee/",
                "init_query": init_query,
                "need_plus_sign": false,
                "model_actions": false,
                "async": true,
                "multiple_select": null,
                "object_actions": false,
                "layout_types": [], 
                "disabled_actions":['OpAddLevelToEmp','OpAdjustArea','OpAdjustDept','OpEmpType','OpIssueCard','OpLeave','OpRegisterFinger','OpTitileChange','OpUploadPhoto','_change','_delete'],
                "fields_show": ['PIN','EName', 'DeptID.name','Card'],
                "obj_edit": false,
                "record_per_page": 20,
                "max_no_page": 30,
                "row_operations":[],
                "init_after_get_jdata": function(){	
        			$("#id_extend div.action").append('<li id="id_extend_export"><a alt="export" href="javascript:void(0)" class="action_Model_Export">'+gettext("导出报表")+'</a></li>')
            		$("#id_extend_export").click(function(){
                		$("#id_model_export").click();
            		});
            	}
            });
       		$("#id_sys_cur_grid").val('#id_extend');
            $("#id_datalist").on_row_click(function(grid, key, row_data){ //单击
                left_data = row_data;
                process_check_button(row_data);
                return true;
            });
          
            
            //默认单击左边的datalist的第一行
            after_query = function()
            {
                onclick_left();
            }   
            
            onclick_left();
            
            //以下三个单选按钮的事件.主要重新初始化两个表格的设置 
            $("#id_radio_level").click(function(){
            	$("#id_sys_cur_app").val("personnel");  //这里设置这两个变量，主要是因为 $("#id_model_export").click()过程中用到了这两个值
            	$("#id_sys_cur_model").val("Employee");
            	
            	$("#id_datalist h1").text(gettext("权限组列表"));
            	//以下设置查询条件的可见
            	$("#id_div_query_acclevelset").css("display","");
            	$("#id_div_query_accdoor").css("display","none");
            	$("#id_div_query_employee").css("display","none");
            	var datalist_grid = $('#id_datalist').get(0).g;
            	datalist_grid.model_url = "{{ dbapp_url }}iaccess/AccLevelSet/";
            	datalist_grid.init_query = [];
            	datalist_grid.base_query = [];
            	datalist_grid.layout_types = [];
            	datalist_grid.row_operations = [];
            	datalist_grid.fields_show = ['level_name','level_timeseg','emp_count'];
            	
            	var extend_grid = $('#id_extend').get(0).g;
            	extend_grid.model_url = "{{ dbapp_url }}personnel/Employee/";
               	extend_grid.fields_show = ['PIN','EName', 'DeptID.name','Card'];
               	extend_grid.sort_fields = ["PIN"];
               	extend_grid.init_query = [];
            	extend_grid.base_query = [];
            	extend_grid.row_operations = [];
            	
        		datalist_grid.load_data();
        	});
        	
        	$("#id_radio_door").click(function(){
        	
        		$("#id_sys_cur_app").val("personnel"); 
            	$("#id_sys_cur_model").val("Employee");  
            			
        		$("#id_datalist h1").text(gettext("门列表"));
        		
        		$("#id_div_query_acclevelset").css("display","none");
        		$("#id_div_query_employee").css("display","none");
            	$("#id_div_query_accdoor").css("display","");
            	var datalist_grid = $('#id_datalist').get(0).g;
            	datalist_grid.model_url = "{{ dbapp_url }}iaccess/AccDoor/";
            	datalist_grid.init_query = [];
            	datalist_grid.base_query = [];
            	datalist_grid.fields_show = ['device','door_name','door_no'];
            	datalist_grid.sort_fields = ["id"];
            	datalist_grid.layout_types = []; 
            	datalist_grid.row_operations = [];
            	
            	var extend_grid = $('#id_extend').get(0).g;
            	extend_grid.model_url = "{{ dbapp_url }}personnel/Employee/";
               	extend_grid.fields_show = ['PIN','EName', 'DeptID.name','Card'];
               	extend_grid.sort_fields = ["PIN"];
               	extend_grid.init_query = [];
            	extend_grid.base_query = [];
            	extend_grid.row_operations = [];
            	datalist_grid.load_data();
        	});
        	$("#id_radio_emp").click(function(){
        		$("#id_sys_cur_app").val("iaccess");  
            	$("#id_sys_cur_model").val("AccDoor"); 
        		
        		$("#id_datalist h1").text(gettext("人员列表"));
        		$("#id_div_query_acclevelset").css("display","none");
        		$("#id_div_query_employee").css("display","");
            	$("#id_div_query_accdoor").css("display","none");
            	
            	var datalist_grid = $('#id_datalist').get(0).g;
            	datalist_grid.model_url = "{{ dbapp_url }}personnel/Employee/";
            	datalist_grid.sort_fields = ["PIN"],
                datalist_grid.init_query = [];
            	datalist_grid.fields_show = ['PIN','EName', 'DeptID.name','Card'];
            	datalist_grid.layout_types = [];
            	datalist_grid.row_operations = [];
                
            	var extend_grid = $('#id_extend').get(0).g;
            	extend_grid.model_url = "{{ dbapp_url }}iaccess/AccDoor/";
                extend_grid.fields_show = ['device','door_name','door_no'];
                extend_grid.sort_fields = ["door_no"];
                extend_grid.init_query = [];
            	extend_grid.row_operations = [];
            	datalist_grid.load_data();
                var $tr3 = $("#id_datalist .dt_hdiv #id_tbl tbody tr:first");
                if($tr3.length == 0)
                {
                    process_check_button("")
                }
        	});
        	
         },
         "reload_after_get_jdata":function(){
            onclick_left();
        }
    });
    {% else %}
        $("#id_search").remove();
        alert(gettext("对不起，您没有访问该页面的权限，不能浏览更多信息！"));
        window.location.href="/{{ request.surl }}accounts/login/";
    {% endif %}<!--contenttypes.can_EmpLevelByLevelPage-->
{% endblock %}