<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%@ page isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
    String basePath = request.getContextPath();
%>
<jsp:include page="../../../common/admin/modal_content_pre.jsp"><jsp:param name="modal_title" value="编辑资源调用关系"/></jsp:include>

    <div >
        <ul class="nav nav-tabs" role="tablist">
            <li role="presentation" class="active"><a href="#caller" aria-controls="caller" onclick="closeDiv()"   role="tab" data-toggle="tab">已调用资源</a></li>
            <li role="presentation"><a href="#callee" aria-controls="callee" onclick="openDiv()" role="tab" data-toggle="tab">添加调用资源</a></li>
        </ul>
        <div class="tab-content">
            <div role="tabpanel" class="tab-pane active" id="caller" >
                <table class="table table-striped table-bordered table-hover" >
                    <thead>
                    <tr>
                        <th>资源类型</th>
                        <th>资源名称</th>
                        <th>服务商名称</th>
                        <th class="hidden-print">操 作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <c:choose>
                        <c:when test="${callerList!=null}">
                            <c:forEach items="${callerList}" var="item" >
                                <tr>
                                    <td>${item.resourceType}</td>
                                    <td>${item.resourceName}</td>
                                    <td></td>
                                    <td></td>
                                </tr>
                            </c:forEach>
                        </c:when>
                        <c:otherwise>
                            <tr>
                                <td colspan="4">无数据</td>
                            </tr>
                        </c:otherwise>
                    </c:choose>
                    </tbody>
                </table>
            </div>
            <div role="tabpanel" class="tab-pane"id="callee">
                <jsp:include page="../../../common/admin/search_pre.jsp"></jsp:include>
                <input type="hidden" name="formSerializeResourceList"/>
                <input type="hidden" id="pageNoResourceList" name="pageNo" value="1"/>

                <form id="formSearchResourceList" action="<%=basePath%>/admin/RelationshipManage/calleeList.action" method="get" class="form-inline">

                    <div class="form-group">
                        <label for="resourceName">资源名称</label>
                        <input type="text" id="resourceName" name="resourceName" class="form-control"/>
                    </div>

                    <div class="form-group">
                        <label for="resourceType">资源类型</label>
                        <select id="resourceType" name="resourceType" class="form-control">
                            <option></option>
                            <c:forEach items="${resourceType}" var="item">
                                <option value="${item.key}">${item.value}</option>
                            </c:forEach>
                        </select>
                    </div>
                    <div class="form-group">
                        <button id="btnSearchResourceList" type="button" class="btn btn-sm btn-primary"><i class="fa fa-search"></i>查询</button>
                    </div>
                </form>
                <jsp:include page="../../../common/admin/search_post.jsp"></jsp:include>
                <div id="divGridResourceList">

                </div>
            </div>
        </div>

    </div>
<form id="callerForm" action="<%=basePath%>/admin/RelationshipManage/doEdit.action" method="post" class="form-inline">
    <input type="hidden" id="calle" name="calle" />
    <input type="hidden" id="callrr" name="caller" value="${resourceId}" />
<button type="button" style="display: none" id="btnCancel" class="btn btn-default" data-dismiss="modal"><i class="fa fa-close"></i>取消</button>
<button type="button" style="display: none" id="btnSave" class="btn btn-primary"><i class="fa fa-check"></i>保存</button>
</form>
<jsp:include page="../../../common/admin/modal_content_post.jsp"></jsp:include>
<jsp:include page="../../../common/admin/modal_footer_pre.jsp"></jsp:include>

<script type="text/javascript">
    function openDiv() {
        $("#btnCancel").show();
        $("#btnSave").show();
    }
    function closeDiv() {
        $("#btnCancel").hide();
        $("#btnSave").hide();
    }
    var checkedIds= [];//保存选中的id，亦适用于分页
    function searchResourceList() {
        var formSerialize = $("input[type='hidden'][name='formSerializeResourceList']").val();
        var pageNo = $("input[type='hidden'][id='pageNoResourceList']").val();
        var id = $("#callrr").val();
        var formData = formSerialize + "&pageNo=" + pageNo+"&id="+id;
        // var aa = document.getElementsByName("resourceId");
        // for (var i = 0; i < aa.length; i++) {
        //     if(aa[i].checked){
        //         checkedIds.push($(aa[i]).val());
        //     }
        //
        // }

        $("#divGridResourceList").renderPartial({url: "<%=basePath%>/admin/RelationshipManage/calleeList.action", data: formData,onFinished: function(t){
            var allpro = document.getElementsByName("resourceId");
                for (var i = 0; i < allpro.length; i++) {
                    var id = $(allpro[i]).val();
                    if (checkedIds.indexOf(id, 0) != -1) {
                        allpro[i].checked = true;
                    } else {
                        allpro[i].checked = false;
                    }
                }
            }});
    }

    $("#btnSearchResourceList").click(function () {
        $("input[type='hidden'][id='pageNoResourceList']").val(1);
        $("input[type='hidden'][name='formSerializeResourceList']").val($("#formSearchResourceList").serialize());
        searchResourceList();
    });


    searchResourceList();
    $('#myTabs a').click(function (e) {
        /*e.preventDefault()*/
        $(this).tab('show')
    })
    $("#btnSave").click(function() {
        // var ids = document.getElementsByName("resourceId");
        // var str="";
        // if(!(ids.length>0)) {
        // alert("请选择调用资源");
        // }else{
        //     for(var i=0;i<ids.length;i++){
        //         if(ids[i].checked){
        //             str+=ids[i].value+",";
        //         }
        //     }
        //     //去掉字符串末尾的‘,'
        //     str=str.substring(0, str.length-1);
        // }
        // $("#calle").val(str);

        $("#calle").val(Array.from(new Set(checkedIds)));
        $('#callerForm').ajaxSubmitCloseRefresh();
    });

    $(".input-group.date").datetimepicker({
        format: "YYYY-MM-DD",
        locale: "zh-cn"
    });
    //获取checkbox按钮组
    var allpro = document.getElementsByName("resourceId");
    //全选方法
    function change() {
        //获取全选按钮
        var all = document.getElementById("all");
        //全选按钮被选中时，遍历所有按钮
        if (all.checked) {
                for (var i = 0; i < allpro.length; i++) {
                    if (allpro[i].type == "checkbox") {
                        allpro[i].checked = true;
                        if (!(checkedIds.indexOf($(allpro[i]).val(), 0) != -1)) {
                            checkedIds.push($(allpro[i]).val());
                        }
                    }
                }
            //全选按钮未被选中时
        }else{
                for (var i = 0; i < allpro.length; i++) {
                    if (allpro[i].type == "checkbox") {
                        allpro[i].checked=false;
                        checkedIds.splice(checkedIds.indexOf($(allpro[i]).val()),1);
                    }
                }
        }
    }

    function checkboxChange(sender, id) {
        var checkboxJqObj = $(sender);
        var value = checkboxJqObj.val();
        if (sender.checked){
            checkedIds.push(value);
        }else{
            checkedIds.splice(checkedIds.indexOf(value),1);
        }
    }

</script>
<jsp:include page="../../../common/admin/modal_footer_post.jsp"></jsp:include>