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

{% block id_main_div %}
{% if request.user|HasPerm:"iaccess.browse_accdoor" %}
{% if request.user|HasPerm:"iaccess.browse_accfirstopen" %}
    <div id="id_accfirstopen" style="margin:5px 0px 0px 0px;">
    {% block datalist_before %}
        <div id="id_current_door"></div>
        <div id="id_datalist" class="div_box" style="float:left; width:48%; margin:5px 0px 0px 10px;margin:5px 0px 0px 5px\9;"><h1>{% trans "首卡常开设置" %}</h1>    
        </div>
    {% endblock %}<!--endblock of datalist_before-add by darcy-->

    {% block extend_before %}
        <div id="id_extend" class="div_box" style="float:right; width:42%; margin:5px 10px 0px 5px; margin:5px 5px 0px 5px\9;"><h1 style="width:100%">{% trans "浏览开门人员" %}</h1>
        </div>
    {% endblock %}<!--endblock of extend_before-add by darcy-->
    </div>
{% endif %}<!--iaccess.browse_accfirstopen-->
{% endif %}<!--iaccess.browse_accdoor-->

{% endblock %}<!--endblock of content-add by darcy-->

{% block acc_door_set %}
{% if request.user|HasPerm:"iaccess.browse_accdoor" %}
{% if request.user|HasPerm:"iaccess.browse_accfirstopen" %}
    var url_search = window.location.search;//?_lock=1&door__id=1
    var door_id = url_search.split("=")[2];
    var door_name = '';
    $.ajax({
        type: "POST",
        url: "/{{ request.surl }}iaccess/GetData/?func=get_doorname&door_id="+ door_id,
        dataType: "json",
        async: false,
        success: function(data){
            door_name = data;
            $("#id_current_door").append('<label class="current_doorname"  name="'+door_name+'" value="'+door_id+'" style="margin: 2px 2px 3px 10px;">'+gettext("当前门:")+door_name+'</label>');
        
            $("#id_datalist").model_grid({
                "model_url": "{{ model_url }}",
                "base_query": ["door__id__exact="+door_id],
                "disabled_actions": ["_change",'OpDelEmpFromFCOpen'],
                "async": false,
                "do_action_masker_div": [$("#id_current_door")],
                "row_operations": ["op_edit","_delete","OpAddEmpToFCOpen"],
                "init_after_get_jdata": function(){
                    $("#id_OpAddEmpToFCOpen").remove();
                }
            });
            
            var left_data = 0;//右边记录对应的左边记录id
            //处理第一次加载时人员组为空时，右侧不显示
            var $tr = $("#id_datalist #id_tbl tbody tr:first");
            left_data = $tr.attr("data");
            
            var base_query = [];
            if($tr.length != 0)
            {
                var group_name2 = $($tr.find("td")[1]).text();                        
                base_query = ["accfirstopen__id__exact="+$tr.attr("data")];
            }
            else
            {   
                base_query = ["accfirstopen__id__exact=0"];
            }
            
            $("#id_extend").model_grid({
                "model_url": "{{ dbapp_url }}personnel/Employee/",
                "base_query": base_query,
                "need_plus_sign": false,
                "model_actions": false,
                "object_actions": false,
                "layout_types": [], 
                "fields_show": ['PIN','EName','DeptID','Card'],
                "obj_edit": false,
                "async": false,
                "record_per_page": 20,
                "max_no_page": 30,
                "row_operations": []
            });

            {% if request.user|HasPerm:"iaccess.opdelempfromfcopen_accfirstopen" %}
            $("#id_extend div.action").append('<li name="OpDelEmpOfFCOpen" id="id_OpDelEmpOfFCOpen"><a alt="OpDelEmpOfFCOpen" href="javascript:void(0)" class="action_OpDelEmpOfFCOpen">'+gettext("删除开门人员")+'</a></li>')
            $("#id_OpDelEmpOfFCOpen a").click(function(){
                var emps = new Array()
                $("#id_extend .select_row_style").each(function(){
                    emps.push($(this).attr("data"));            
                });
                
                if(emps == "")
                {
                    alert(gettext("请先选择要删除的人员！"));
                    return false;
                }
                
                if(!confirm(gettext("确认要从首卡常开设置信息中删除开门人员？")))
                {
                    return false;
                }
            
                $.ajax({
                    type: "POST",
                    dataType: "json",
                    url: "/{{ request.surl }}iaccess/FCOpenEmpOp/?data="+ left_data +"&emps="+ emps,
                    async: false,
                    success: function(data)
                    {
                        if(data.ret == 1)
                        {
                            alert(gettext("删除开门人员成功！"));
                            $("#id_extend").get(0).g.load_data();
                            $("#id_datalist").get(0).g.load_data();
                        }
                        else
                        {
                            alert(gettext("删除开门人员失败！"));
                        }
                    },
                    error:function (XMLHttpRequest, textStatus, errorThrown) 
                    {
                        alert(gettext("服务器处理数据失败，请重试！错误码：-603"));
                        flag = false;
                    }            
                });
            });
            {% endif %}<!--opdelempfromfcopen_accfirstopen-->
            
            $("#id_datalist").on_row_click(function(grid, key, row_data){ //单击
                left_data = row_data;
                $('#id_extend').get(0).g.base_query = ["accfirstopen__id__exact="+row_data];
                $('#id_extend').get(0).g.load_data();
               
                return true;
            });
            function onclick_left()
            {
                var $tr2 = $("#id_datalist #id_tbl tbody tr:first");
                //alert($tr2.length);
                if($tr2.length != 0)
                {
                    $tr2.click();//$("#id_datalist #id_tbl tbody tr:first").click();
                }
                else
                {
                    $('#id_extend').get(0).g.base_query=["accfirstopen__id__exact=0"];
                    $('#id_extend').get(0).g.load_data();
                }
                return true;
            }
            //onclick_left();
            //处理查询之后加载时人员组为空时，右侧不显示
            //默认单击左边的datalist的第一行
            after_query = function()
            {
                onclick_left();
            }    
            
            //编辑完左侧的人员组后需要同步后侧的人员列表
            after_submit = function()
            {
                var row_data2 = $($("#id_datalist").get(0).current_row).attr("data");
                $($("#id_datalist tr[data="+row_data2+"]")).click();
            }
            
            //左边新增、删除后刷新列表//，单击第一个
            after_save_return = function()
            {
                onclick_left();
            }
            
        },
        error:function (XMLHttpRequest, textStatus, errorThrown) 
        {
            alert(gettext("服务器处理数据失败，请重试！错误码：-604"));
        }        
    });
    
    {% else %}<!--iaccess.browse_accfirstopen-->
        alert(gettext("对不起，您没有访问该页面的权限，不能浏览更多信息！"));
        window.location.href = "/{{ request.surl }}accounts/login/";
    {% endif %}<!--iaccess.browse_accfirstopen-->
    {% else %}<!--iaccess.browse_accdoor-->
        alert(gettext("对不起，您没有访问该页面的权限，不能浏览更多信息！"));
        window.location.href="/{{ request.surl }}accounts/login/";
    {% endif %}<!--iaccess.browse_accdoor-->
    
{% endblock %}




