<%@ page import="org.apache.commons.lang.StringUtils" %>
<%@ page import="com.lightinit.hsdataplatform.dictionary.DicResourceCatelogType" %>
<%@ page import="com.lightinit.hsdataplatform.dictionary.DicResourceState" %>
<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<%@ page isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<%
    String basePath = request.getContextPath();
%>
<!DOCTYPE html>
<html>
<head>
    <jsp:include page="../../../common/admin/head.jsp"></jsp:include>
</head>
<body>
<jsp:include page="../../../common/admin/body_pre.jsp"></jsp:include>
<div class="row hidden-print">
    <div class="col-lg-12">
        <div class="ibox">
            <div class="ibox-title">
                <h2>API注册</h2>
                <ol class="breadcrumb">
                    <li>
                        <a href="javascript:void(0);">资源活化</a>
                    </li>
                    <li>
                        <a href="javascript:void(0);">资源</a>
                    </li>
                    <li>
                        <a href="<%=basePath%>/admin/data_register_api/index.htm">API注册</a>
                    </li>
                    <li class="active">
                        <strong>API注册编辑</strong>
                    </li>
                </ol>
            </div>
        </div>
    </div>

    <div class="col-md-12 column" >
        <form class="form-horizontal" id="api_interface_edit_add_from" method="post" class="form-horizontal" action="<%=basePath%>/admin/data_register_api/apiInterface_edit_add.action">
            <input name="id" type="hidden" value="${resourceId}">
            <input type="hidden" name="api_badge" id="api_badge" value=0>
            <div class="form-group">
                <label for="resourceName" class="col-sm-2 control-label"><span style="color: red;">*</span>服务名称</label>
                <div class="col-sm-7">
                    <input type="text" id="resourceName" name="resourceName" class="form-control" value="${resource.resourceName}"/>
                    <span class="field-validation-valid help-block"></span>
                </div>
            </div>
            <div class="form-group">
                <label for="resourceIdentity" class="col-sm-2 control-label"><span style="color: red;">*</span>服务标识</label>
                <div class="col-sm-7">
                    <input type="text" id="resourceIdentity" name="resourceIdentity" class="form-control" value="${resource.resourceIdentity}"/>
                    <span class="field-validation-valid help-block"></span>
                </div>
            </div>
            <div class="form-group">
                <label for="resourceSummary" class="col-sm-2 control-label"><span style="color: red;">*</span>服务简介</label>
                <div class="col-sm-7">
                    <input type="text" id="resourceSummary" name="resourceSummary" class="form-control" value="${resource.resourceSummary}"/>
                    <span class="field-validation-valid help-block"></span>
                </div>
            </div>
            <div class="form-group">
                <label for="registerDate" class="col-sm-2 control-label">注册日期</label>
                <div class="col-sm-7" >
                    <div class="input-group date">
                        <input type="text" id="registerDate" name="registerDate" class="form-control" value="<fmt:formatDate value="${resource.registerDate}" pattern="yyyy-MM-dd"/>" />
                        <span class="input-group-addon">
                    <span class="glyphicon glyphicon-calendar"></span>
                </span>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label for="provider" class="col-sm-2 control-label"><span style="color: red;">*</span>提供单位</label>
                <div class="col-md-7 col-sm-7">
                    <input type="hidden" id="provider" name="provider" />
                    <button type="button" class="btn btn-default dropdown-toggle" style="min-width: 120px; text-align: right;">
                        <span class="caret" style="margin-left: 10px;"></span>
                    </button>
                    <div id="divAddSectorTree"></div>
                    <span class="field-validation-valid help-block"></span>
                </div>
                <span class="field-validation-valid help-block"></span>
            </div>
            <div class="form-group">
                <label for="returnCode" class="col-sm-2 control-label"><span style="color: red;">*</span>返回码</label>
                <div class="col-sm-7">
                    <textarea type="text" id="returnCode" name="returnCode" class="form-control">${resourceApi.returnCode}</textarea>
                    <span class="field-validation-valid help-block"></span>
                </div>
            </div>
            <div class="form-group">
                <label for="resourceSummary" class="col-sm-2 control-label"><span style="color: red;">*</span>目录分类</label>
                <div class="col-md-7 col-sm-7">
                    <input type="checkbox" name="catelogType" <c:if test="${fn:contains(resource.catelogType,'基础分类')}">checked</c:if>
                           value="<%=DicResourceCatelogType.BASIC%>" /><%=DicResourceCatelogType.BASIC%>
                    <input type="checkbox" name="catelogType" <c:if test="${fn:contains(resource.catelogType,'部门分类')}">checked</c:if>
                           value="<%=DicResourceCatelogType.SECTOR%>" style="margin-left: 50px;" /><%=DicResourceCatelogType.SECTOR%>
                    <br />
                    <input type="checkbox" name="catelogType" <c:if test="${fn:contains(resource.catelogType,'主题分类')}">checked</c:if>
                           value="<%=DicResourceCatelogType.THEME%>" /><%=DicResourceCatelogType.THEME%>
                    <input type="checkbox" name="catelogType" <c:if test="${fn:contains(resource.catelogType,'自定义分类')}">checked</c:if>
                           value="<%=DicResourceCatelogType.CUSTOM%>" style="margin-left: 50px;" /><%=DicResourceCatelogType.CUSTOM%>
                    <span class="field-validation-valid help-block"></span>
                </div>
            </div>
            <div class="form-group">
                <label for="catelogBasic" class="col-sm-2 control-label text-right">基础分类</label>
                <div class="col-md-7 col-sm-7">
                    <input type="hidden" id="catelogBasic" name="catelogBasic" />
                    <button type="button" class="btn btn-default dropdown-toggle" style="min-width: 120px; text-align: right;">
                        <span class="caret" style="margin-left: 10px;"></span>
                    </button>
                    <div id="divCatelogBasic"></div>
                </div>
                <span class="field-validation-valid help-block"></span>
            </div>
            <div class="form-group">
                <label for="catelogSector" class="col-sm-2 control-label">部门分类</label>
                <div class="col-md-7 col-sm-7">
                    <input type="hidden" id="catelogSector" name="catelogSector" />
                    <button type="button" class="btn btn-default dropdown-toggle" style="min-width: 120px; text-align: right;">
                        <span class="caret" style="margin-left: 10px;"></span>
                    </button>
                    <div id="divCatelogSector"></div>
                </div>
                <span class="field-validation-valid help-block"></span>
            </div>
            <div class="form-group">
                <label for="catelogTheme" class="col-sm-2 control-label">主题分类</label>
                <div class="col-md-7 col-sm-7">
                    <input type="hidden" id="catelogTheme" name="catelogTheme" />
                    <button type="button" class="btn btn-default dropdown-toggle" style="min-width: 120px; text-align: right;">
                        <span class="caret" style="margin-left: 10px;"></span>
                    </button>
                    <div id="divCatelogTheme"></div>
                </div>
                <span class="field-validation-valid help-block"></span>
            </div>
            <div class="form-group">
                <label for="catelogCustom" class="col-sm-2 control-label">自定义分类</label>
                <div class="col-md-7 col-sm-7">
                    <input type="hidden" id="catelogCustom" name="catelogCustom" />
                    <button type="button" class="btn btn-default dropdown-toggle" style="min-width: 120px; text-align: right;">
                        <span class="caret" style="margin-left: 10px;"></span>
                    </button>
                    <div id="divCatelogCustom"></div>
                </div>
                <span class="field-validation-valid help-block"></span>
            </div>
            <div class="form-group">
                <label for="shareType" class="col-sm-2 control-label">共享类型</label>
                <div class="col-md-7 col-sm-7">
                    <select id="shareType" name="shareType" class="form-control">
                        <c:forEach items="${dicResourceShareType}" var="item" >
                            <option value="${item.key}" ${item.key == resource.shareType ? "selected=\"selected\"" : ""}>${item.value}</option>
                        </c:forEach>
                    </select>
                </div>
                <span class="field-validation-valid help-block"></span>
            </div>
            <div class="form-group">
                <label for="openType" class="col-sm-2 control-label">开放类型</label>
                <div class="col-md-7 col-sm-7">
                    <select id="openType" name="openType" class="form-control">
                        <option></option>
                        <c:forEach items="${dicResourceOpenType}" var="item" >
                            <option value="${item.key}" ${item.key == resource.openType ? "selected=\"selected\"" : ""}>${item.value}</option>
                        </c:forEach>
                    </select>
                </div>
                <span class="field-validation-valid help-block"></span>
            </div>
            <div class="form-group">
                <label for="allowDeal" class="col-sm-2 control-label">是否可交易</label>
                <div class="col-md-7 col-sm-7">
                    <select id="allowDeal" name="allowDeal" class="form-control">
                        <option value="0" ${resource.allowDeal == 0 ? "selected=\"selected\"" : ""}>否</option>
                        <option value="1" ${resource.allowDeal == 1 ? "selected=\"selected\"" : ""}>是</option>
                    </select>
                </div>
                <span class="field-validation-valid help-block"></span>
            </div>
            <div class="form-group">
                <label for="serviceType" class="col-sm-2 control-label">服务类型</label>
                <div class="col-md-7 col-sm-7">
                    <select id="serviceType" name="serviceType" class="form-control">
                        <c:if test="${dicAPIServiceType.size()>0}">
                        <c:forEach items="${dicAPIServiceType}" var="item" >
                            <option value="${item.name}" ${item.value == resourceApi.serviceType ? "selected=\"selected\"" : ""}>${item.value}</option>
                        </c:forEach>
                        </c:if>
                    </select>
                </div>
                <span class="field-validation-valid help-block"></span>
            </div>
            <div class="form-group">
                <label for="serviceTag" class="col-sm-2 control-label">服务标签</label>
                <div class="col-md-7 col-sm-7">
                    <input type="text" id="serviceTag" name="serviceTag" class="form-control" value="${resourceApi.serviceTag}"/>
                </div>
                <span class="field-validation-valid help-block"></span>
            </div>
            <div class="form-group">
                <label for="component" class="col-sm-2 control-label"><span style="color: red;">*</span>组成接口</label>
                <div class="col-md-7 col-sm-7">
                    <input type="text" id="component" name="component" class="form-control" value="${resourceApi.component}"/>
                    <span class="field-validation-valid help-block"></span>
                </div>
            </div>
            <div class="form-group">
                <table id="testaaaEdit" class="table table-hover  table-condensed table-bordered " style="width: 70%;margin-left:10%" align="center">
                    <thead>
                    <tr>
                        <th>接口名称</th>
                        <th>接口标识</th>
                        <th>接口地址</th>
                        <th>操作</th>
                    </tr>
                    <c:if test="${resourceApiInterface.size()>0}">
                    <c:forEach items="${resourceApiInterface}" var="apiInterface">
                        <tr>
                            <td>${apiInterface.interfaceName}</td>
                            <td>${apiInterface.interfaceIdentity}</td>
                            <td>${apiInterface.url}</td>
                            <td><a onclick='deleteTrRowEdit(this,${apiInterface.id});'>&nbsp;删除</a></td>
                        </tr>
                    </c:forEach>
                    </c:if>
                    </thead>
                </table>
                <button id="btnEditAdd" type="button" class="btn btn-sm btn-primary btn-position" style="margin-right:20%">
                    <i class="fa fa-plus"></i>新增接口
                </button>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-7 text-center">
                    <button type="button" id="editSubmitButton" class="btn btn-primary"><i class="fa fa-check"></i>提交</button>
                    <button type="button" id="keepButton"  class="btn btn-warning"><i class="fa fa-exclamation"></i>暂存</button>
                    <button type="button" id="cancel" class="btn btn-default" data-dismiss="modal"><i class="fa fa-close"></i>取消</button>
                </div>
            </div>
            <input type="hidden" id="resourceState" name="resourceState">
        </form>
    </div>
</div>
<jsp:include page="../../../common/admin/body_post.jsp"></jsp:include>
<script type="text/javascript">
    var from = $('#api_interface_edit_add_from');
    var z = 0 ;
    $(".input-group.date").datetimepicker({
        format: "YYYY-MM-DD",
        locale: "zh-cn"
    });

    $("#btnEditAdd").click(function () {
        $.popBsPartial({ url: "<%=basePath%>/admin/data_register_api/apiInterface_edit_add.htm"});
    });

    $("#cancel").click(function () {
        window.location.href="<%=basePath%>/admin/data_register_api/index.htm" ;
    });

    function deleteTrRowEdit(tr,id) {
        $(tr).parent().parent().remove();
        var addtr2 = $(
            "<input type='hidden' name='resourceApiInterfaceIds["+z+"]' value='"+id+"'/>"
            );
        addtr2.appendTo(from);
        z++ ;
    }

    $("#editSubmitButton").click(function () {
        $('input[name="resourceState"]').val("<%=DicResourceState.PENDING%>");
        $('#api_interface_edit_add_from').ajaxValSubmit({ajaxSubmitOptions:{
                onSuccess: function () {
                    window.location.href="<%=basePath%>/admin/data_register_api/index.htm" ;
                }}});
    });

    $("#keepButton").click(function () {
        $('input[name="resourceState"]').val("<%=DicResourceState.DRAFT%>");
        $('#api_interface_edit_add_from').ajaxValSubmit({ajaxSubmitOptions:{
                onSuccess: function () {
                    window.location.href="<%=basePath%>/admin/data_register_api/index.htm" ;
                }}});
    });

    $("#divAddSectorTree").selectTree({url: "<%=basePath%>/admin/common/sector_tree_data.action${resourceApi.provider == null ? "" : "?selectedId="}${resourceApi.provider}"});
    $("#divCatelogBasic").selectTree({url: "<%=basePath%>/admin/common/resource_catelog_tree_data.action?resourceCatelogType=<%=DicResourceCatelogType.BASIC%>${resource.catelogBasic == null ? "" : "&selectedId="}${resource.catelogBasic}"});
    $("#divCatelogSector").selectTree({url: "<%=basePath%>/admin/common/resource_catelog_tree_data.action?resourceCatelogType=<%=DicResourceCatelogType.SECTOR%>${resource.catelogSector == null ? "" : "&selectedId="}${resource.catelogSector}"});
    $("#divCatelogTheme").selectTree({url: "<%=basePath%>/admin/common/resource_catelog_tree_data.action?resourceCatelogType=<%=DicResourceCatelogType.THEME%>${resource.catelogTheme == null ? "" : "&selectedId="}${resource.catelogTheme}"});
    $("#divCatelogCustom").selectTree({url: "<%=basePath%>/admin/common/resource_catelog_tree_data.action?resourceCatelogType=<%=DicResourceCatelogType.CUSTOM%>${resource.catelogCustom == null ? "" : "&selectedId="}${resource.catelogCustom}"});
</script>
</body>
</html>