<%@ page import="com.lightinit.hsdataplatform.dictionary.DicPublishSettingChargeMethod" %>
<%@ page import="com.lightinit.hsdataplatform.dictionary.DicResourceCatelogType" %>
<%@ 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 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"/>
    <div>
        <form id="formSearchResourceList" action="<%=basePath%>/admin/contract/resourceList.action" method="get"
              class="form-inline">

            <div class="form-group">
                <label for="catelogTheme">资源主题</label>
                <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>

            <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>
        <div>
            <jsp:include page="../../../common/admin/search_post.jsp"></jsp:include>
            <div id="divGridResourceList">
            </div>
            <br/>
            <div class="form-group">
                <label for="chargeMethod" class="col-sm-2 control-label"><span
                        style="color: red;">*</span>计费规则</label>
                <div class="col-sm-8">
                    <select id="chargeMethod" name="chargeMethod" class="form-control">
                        <c:forEach items="${chargeMethod}" var="item">
                            <option value="${item.key}">${item.value}</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
            <br/>
            <div class="form-group">
                <label for="price" class="col-sm-2 control-label">价格</label>
                <div class="col-sm-8">
                    <input type="text" id="price" name="price" class="form-control"/>
                    <span class="field-validation-valid help-block"></span>
                </div>
            </div>
            <div class="form-group">
                <label for="useMonthsLimit" class="col-sm-2 control-label"><span
                        style="color: red;">*</span>使用月数限制</label>
                <div class="col-sm-8">
                    <input type="text" id="useMonthsLimit" name="useMonthsLimit" class="form-control"/>
                    <span class="field-validation-valid help-block"></span>
                </div>
            </div>

            <div class="form-group">
                <label for="useTimesLimit" class="col-sm-2 control-label"><span
                        style="color: red;">*</span>使用次数限制</label>
                <div class="col-sm-8">
                    <input type="text" id="useTimesLimit" name="useTimesLimit" class="form-control"/>
                    <span class="field-validation-valid help-block"></span>
                </div>
            </div>

            <div class="form-group">
                <label for="effectiveDate" class="col-sm-2 control-label"><span
                        style="color: red;">*</span>生效日期</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input type="text" id="effectiveDate" name="effectiveDate" class="form-control"/>
                        <span class="input-group-addon">
                                <span class="glyphicon glyphicon-calendar"></span>
                                </span>
                    </div>
                </div>
            </div>
            <br/>
            <br/>
            <div class="form-group">
                <label for="expiriedDate" class="col-sm-2 control-label"><span
                        style="color: red;">*</span>失效日期</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input type="text" id="expiriedDate" name="expiriedDate" class="form-control"/>
                        <span class="input-group-addon">
                                <span class="glyphicon glyphicon-calendar"></span>
                                </span>
                    </div>
                </div>
            </div>
        </div>
        <br>


        <br/>
        <br/>
        <jsp:include page="../../../common/admin/modal_content_post.jsp"></jsp:include>
        <jsp:include page="../../../common/admin/modal_footer_pre.jsp"></jsp:include>
        <button type="button" class="btn btn-default text-center" data-dismiss="modal"><i class="fa fa-close"></i>取消
        </button>
        <button type="button" onclick="resourceSave()" class="btn btn-primary text-center"><i class="fa fa-check"></i>保存
        </button>
        <jsp:include page="../../../common/admin/modal_footer_post.jsp"></jsp:include>

        <script type="text/javascript">

            $(".input-group.date").datetimepicker({
                format: "YYYY-MM-DD",
                locale: "zh-cn"
            });

            var checkedIds = [];//保存选中的id，亦适用于分页
            function searchResourceList() {
                var formSerialize = $("input[type='hidden'][name='formSerializeResourceList']").val();
                var pageNo = $("input[type='hidden'][id='pageNoResourceList']").val();
                var formData = formSerialize + "&pageNo=" + pageNo;

                $("#divGridResourceList").renderPartial({
                    url: "<%=basePath%>/admin/contract/resourceList.action", data: formData
                });
            }

            $("#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) {
                $(this).tab('show')
            })
            $("#btnSave").click(function () {
                $("#calle").val(Array.from(new Set(checkedIds)));
                $('#callerForm').ajaxSubmitCloseRefresh();
            });

            function resourceSave() {
                var checkedRadio = $("input[name='radio']:checked ");
                if (!checkedRadio || checkedRadio.length == 0) {
                    alert("请选择资源");
                    return;
                }
                var checkedTr = checkedRadio.closest("tr");
                var resourceType = checkedTr.children().eq(1).text();
                var catelogName = checkedTr.children().eq(2).text();
                var resourceName = checkedTr.children().eq(3).text();
                var publishInfoId = $("input[name='radio']:checked ").val();
                var contractAmount = $("#contractAmount").val();

                var newResourceName = resourceType;
                if (catelogName != '' && catelogName != null && catelogName != 'undefined') {
                    newResourceName = newResourceName + '>>' + catelogName;
                }
                newResourceName = newResourceName + '>>' + resourceName;
                var resourceBadge = $("#resourceBadge").val();
                var tables = $('#contactResource');
                var addtr = $("<tr>" +
                    "<td>" + newResourceName + "</td>" +
                    "<td>" + $("#chargeMethod").val() + "</td>" +
                    "<td>" + $("#price").val() + "</td>" +
                    "<td>" + $("#useMonthsLimit").val() + "</td>" +
                    "<td>" + $("#useTimesLimit").val() + "</td>" +
                    "<input type='hidden' name='contractAddResourceModelList[" + resourceBadge + "].publishInfoId' value='" + publishInfoId + "' />" +
                    "<input type='hidden' name='contractAddResourceModelList[" + resourceBadge + "].resourceName' value='" + resourceName + "' />" +
                    "<input type='hidden' id='price_" + resourceBadge + "' name='contractAddResourceModelList[" + resourceBadge + "].price' value='" + $("#price").val() + "'/>" +
                    "<input type='hidden' id='chargeMethod_" + resourceBadge + "' name='contractAddResourceModelList[" + resourceBadge + "].chargeMethod' value='" + $("#chargeMethod").val() + "'/>" +
                    "<input type='hidden' name='contractAddResourceModelList[" + resourceBadge + "].useMonthsLimit' value='" + $("#useMonthsLimit").val() + "'/>" +
                    "<input type='hidden' name='contractAddResourceModelList[" + resourceBadge + "].useTimesLimit' value='" + $("#useTimesLimit").val() + "'/>" +
                    "<input type='hidden' name='contractAddResourceModelList[" + resourceBadge + "].effectiveDate' value='" + $("#effectiveDate").val() + "'/>" +
                    "<input type='hidden' name='contractAddResourceModelList[" + resourceBadge + "].expiriedDate' value='" + $("#expiriedDate").val() + "'/>" +
                    "<input type='hidden' name='contractAddResourceModelList[" + resourceBadge + "].resourceType' value='" + resourceType + "'/>" +
                    "<td>" +
                    "<a onclick='deleteTrRow(this," + resourceBadge + ");'>&nbsp;删除</a>" +
                    "</td>" +
                    "</tr>");
                if ($("#chargeMethod").val() != '<%=DicPublishSettingChargeMethod.FREE%>') {
                    $("#contractAmount").val(contractAmount * 1 + $("#price").val() * 1);
                }
                addtr.appendTo(tables);
                $("#resourceBadge").val(resourceBadge * 1 + 1 * 1);
                var modal = $.getJqObj(wysharpDefaultModalContainerSelector);
                modal.modal('hide');
            }

            function deleteTrRow(tr, resourceBadge) {
                var priceId = "#price_" + resourceBadge;
                var chargeMethod = "#chargeMethod_" + resourceBadge;
                if ($(chargeMethod).val() != '<%=DicPublishSettingChargeMethod.FREE%>') {
                    $("#contractAmount").val($("#contractAmount").val() * 1 - $(priceId).val() * 1);
                }
                $(tr).parent().parent().remove();
            }

            $("#divCatelogTheme").selectTree({url: "<%=basePath%>/admin/common/resource_catelog_tree_data.action?resourceCatelogType=<%=DicResourceCatelogType.THEME%>"});

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