<%@ page import="com.lightinit.hsdataplatform.dictionary.DicPublishSettingChargeMethod" %>
<%@ page import="com.lightinit.hsdataplatform.dictionary.DicBuyTimesLimit" %>
<%@ 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" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%
    String basePath = request.getContextPath();
%>
<jsp:include page="../../../common/admin/modal_content_pre.jsp">
    <jsp:param name="modal_title" value="新增定价"/>
</jsp:include>
<form id="formAdd" action="<%=basePath%>/admin/data_register_api/apiInterface_add.action" method="post"
      class="form-horizontal">
    <div class="form-group">
        <div class="form-group">
            <label for="chargeMethod" class="col-sm-3 control-label">计费方式</label>
            <div class="col-sm-8">
                <input type="hidden" id="badge" name="badge" value="${publishSetting.badge}"/>
                <input type="hidden" id="editPublishSettingId" value="${publishSetting.id}">
                <select id="chargeMethod" name="chargeMethod" class="form-control">
                    <option value="<%=DicPublishSettingChargeMethod.FREE%>"
                            <c:if test="${publishSetting.chargeMethod eq DicPublishSettingChargeMethod.FREE}">selected="selected"</c:if>><%=DicPublishSettingChargeMethod.FREE%>
                    </option>
                    <option value="<%=DicPublishSettingChargeMethod.BY_NUMBER_OF_TIMES%>"
                            <c:if test="${publishSetting.chargeMethod eq DicPublishSettingChargeMethod.BY_NUMBER_OF_TIMES}">selected="selected"</c:if>><%=DicPublishSettingChargeMethod.BY_NUMBER_OF_TIMES%>
                    </option>
                    <option value="<%=DicPublishSettingChargeMethod.BY_TIME%>"
                            <c:if test="${publishSetting.chargeMethod eq DicPublishSettingChargeMethod.BY_TIME}">selected="selected"</c:if>><%=DicPublishSettingChargeMethod.BY_TIME%>
                    </option>
                </select>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label for="packageName" class="col-sm-3 control-label">套餐名称</label>
            <div class="col-sm-8">
                <input type="text" id="packageName" name="packageName" class="form-control"
                       value="${publishSetting.packageName}"/>
                <span class="help-block"></span>
            </div>
        </div>
        <div class="form-group">
            <label for="packagePrice" class="col-sm-3 control-label">套餐价格</label>
            <div class="col-md-8 col-sm-8">
                <div class="input-group">
                    <input type="text" id="packagePrice" name="packagePrice" class="NumDecText form-control"
                           value="${publishSetting.packagePrice}"/>
                    <span class="input-group-addon">元/</span>
                    <input type="text" id="packageMonths" name="packageMonths" class="form-control"
                           value="${publishSetting.packageMonths}" onkeyup="value=value.replace(/[^\d]/g,'')"/>
                    <span class="input-group-addon">月</span>
                </div>
                <span class="field-validation-valid help-block"></span>
            </div>
        </div>
        <div class="form-group">
            <label for="selfCost" class="col-sm-3 control-label">套餐成本</label>
            <div class="col-md-8 col-sm-8">
                <div class="input-group">
                    <input type="text" id="selfCost" name="selfCost" class="NumDecText form-control"
                           value="${publishSetting.selfCost}"/>
                    <span class="input-group-addon">元/月</span>
                </div>
                <span class="field-validation-valid help-block"></span>
            </div>
        </div>
        <div class="form-group">
            <label for="buyTimesLimit" class="col-sm-3 control-label">购买次数限制</label>
            <div class="col-sm-8">
                <select id="buyTimesLimit" name="buyTimesLimit" class="form-control">
                    <option value="0" selected="selected"><%=DicBuyTimesLimit.UNLIMITED%>
                    </option>
                    <option value="1"><%=DicBuyTimesLimit.UNLIMITED_ONE%>
                    </option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label for="useTimesLimit" class="col-sm-3 control-label">使用限制</label>
            <div class="col-md-8 col-sm-8">
                <div class="input-group">
                    <input type="text" id="useTimesLimit" name="useTimesLimit" class="form-control"
                           value="${publishSetting.useTimesLimit}" onkeyup="value=value.replace(/[^\d]/g,'')"/>
                    <span class="input-group-addon">次</span>
                </div>
                <span class="help-block"></span>
            </div>
        </div>
        <div class="form-group">
            <label for="effectiveDate" class="col-md-3 col-sm-3 control-label">生效日期</label>
            <div class="col-md-8 col-sm-8">
                <div class="input-group date">
                    <input type="text" id="effectiveDate" name="effectiveDate" class="form-control"
                           value="<fmt:formatDate value="${publishSetting.effectiveDate}" pattern="yyyy-MM-dd"/>"/>
                    <span class="input-group-addon">
                    <span class="glyphicon glyphicon-calendar"></span>
                </span>
                </div>
                <span class="help-block"></span>
            </div>
        </div>
        <div class="form-group">
            <label for="expiriedDate" class="col-md-3 col-sm-3 control-label">失效日期</label>
            <div class="col-md-8 col-sm-8">
                <div class="input-group date">
                    <input type="text" id="expiriedDate" name="expiriedDate" class="form-control"
                           value="<fmt:formatDate value="${publishSetting.expiriedDate}" pattern="yyyy-MM-dd"/>"/>
                    <span class="input-group-addon">
                    <span class="glyphicon glyphicon-calendar"></span>
                </span>
                </div>
                <span class="help-block"></span>
            </div>
        </div>
        <div class="form-group">
            <label for="description" class="col-sm-3 control-label">套餐说明</label>
            <div class="col-sm-8">
                <input type="text" id="description" name="description" class="form-control"
                       value="${publishSetting.description}"/>
            </div>
        </div>
    </div>
</form>
<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" data-dismiss="modal"><i class="fa fa-close"></i>取消</button>
<button type="button" id="btnApiPublishSettingSave" class="btn btn-primary"><i class="fa fa-check"></i>保存</button>
<jsp:include page="../../../common/admin/modal_footer_post.jsp"></jsp:include>
<script type="text/javascript">
    $("#formAdd").validate({
        rules: {
            packagePrice: "required",
            packageMonths: "required",
            selfCost: "required",
        }
    });
    $("#btnApiPublishSettingSave").click(function () {
        //简易的表单验证
        if ($.trim($('#chargeMethod').val()) != '免费') {
            var isValid = $.getJqObj("#formAdd").valid();
            if (!isValid) {
                return;
            }
        } else {
            if ($.trim($('#packageName').val()) == '') {
                $.showFormFieldValidationResult({form: "#formAdd", field: "packageName", msg: "不能为空"});
                return;
            }
        }
        var apiPublish_badge = $("#apiPublish_badge").val();
        if ($.trim($("#badge").val()).length > 0) {
            $("tr[id=publishSetting_" + $("#badge").val() + "").remove();
            apiPublish_badge = $("#badge").val();
        }
        var buyTimesLimit;
        if ($("#buyTimesLimit").val() == 0) {
            buyTimesLimit = "<%=DicBuyTimesLimit.UNLIMITED%>";
        }
        if ($("#buyTimesLimit").val() == 1) {
            buyTimesLimit = "<%=DicBuyTimesLimit.UNLIMITED_ONE%>";
        }
        var tables = $('#publishSettingTable');
        var addtr = $("<tr id='publishSetting_" + apiPublish_badge + "'>" +
            "<td>" + $("#packageName").val() + "</td>" +
            "<td>" + $("#packagePrice").val() + "元/" + $("#packageMonths").val() + "月</td>" +
            "<td>" + buyTimesLimit + "</td>" +
            "<td>" + $("#useTimesLimit").val() + "次</td>" +
            "<td>" + $("#effectiveDate").val() + "</td>" +
            "<td>" + $("#expiriedDate").val() + "</td>" +
            "<input type='hidden' name='apiPublishSettingList[" + apiPublish_badge + "].id' value='" + $("#editPublishSettingId").val() + "' />" +
            "<input type='hidden' name='apiPublishSettingList[" + apiPublish_badge + "].chargeMethod' value='" + $("#chargeMethod").val() + "' />" +
            "<input type='hidden' name='apiPublishSettingList[" + apiPublish_badge + "].packageName' value='" + $("#packageName").val() + "'/>" +
            "<input type='hidden' name='apiPublishSettingList[" + apiPublish_badge + "].packagePrice' value='" + $("#packagePrice").val() + "'/>" +
            "<input type='hidden' name='apiPublishSettingList[" + apiPublish_badge + "].packageMonths' value='" + $("#packageMonths").val() + "' />" +
            "<input type='hidden' name='apiPublishSettingList[" + apiPublish_badge + "].selfCost' value='" + $("#selfCost").val() + "'/>" +
            "<input type='hidden' name='apiPublishSettingList[" + apiPublish_badge + "].buyTimesLimit' value='" + $("#buyTimesLimit option:selected").val() + "' />" +
            "<input type='hidden' name='apiPublishSettingList[" + apiPublish_badge + "].useTimesLimit' value='" + $("#useTimesLimit").val() + "'/>" +
            "<input type='hidden' name='apiPublishSettingList[" + apiPublish_badge + "].effectiveDate' value='" + $("#effectiveDate").val() + "'/>" +
            "<input type='hidden' name='apiPublishSettingList[" + apiPublish_badge + "].expiriedDate' value='" + $("#expiriedDate").val() + "'/>" +
            "<input type='hidden' name='apiPublishSettingList[" + apiPublish_badge + "].description' value='" + $("#description").val() + "'/>" +
            "<td>" +
            "<a onclick='editTrRow(" + apiPublish_badge + ");'>&nbsp;编辑</a>" +
            "<a onclick='deleteTrRow(this);'>&nbsp;删除</a>" +
            "</td>" +
            "</tr>");
        addtr.appendTo(tables);
        if (!$.trim($("#badge").val()).length > 0) {
            $(" #apiPublish_badge ").val(apiPublish_badge * 1 + 1);
        }
        var modal = $.getJqObj(wysharpDefaultModalContainerSelector);
        modal.modal('hide');
    });

    $("#btnAdd").click(function () {
        $.popBsPartial({url: "/admin/api_publish/apiPublish_setting.htm"});
    });

    function editTrRow(apiPublish_badge) {
        $.popBsPartial({
            url: "<%=basePath%>/admin/api_publish/edit_setting.htm", data: {
                id: $("input[name = 'apiPublishSettingList[" + apiPublish_badge + "].id']").val()
                , chargeMethod: $("input[name = 'apiPublishSettingList[" + apiPublish_badge + "].chargeMethod']").val()
                ,
                packageName: $("input[name = 'apiPublishSettingList[" + apiPublish_badge + "].packageName']").val()
                ,
                packagePrice: $("input[name = 'apiPublishSettingList[" + apiPublish_badge + "].packagePrice']").val()
                ,
                packageMonths: $("input[name = 'apiPublishSettingList[" + apiPublish_badge + "].packageMonths']").val()
                ,
                selfCost: $("input[name = 'apiPublishSettingList[" + apiPublish_badge + "].selfCost']").val()
                ,
                buyTimesLimit: $("input[name = 'apiPublishSettingList[" + apiPublish_badge + "].buyTimesLimit']").val()
                ,
                useTimesLimit: $("input[name = 'apiPublishSettingList[" + apiPublish_badge + "].useTimesLimit']").val()
                ,
                effectiveDate: $("input[name = 'apiPublishSettingList[" + apiPublish_badge + "].effectiveDate']").val()
                ,
                expiriedDate: $("input[name = 'apiPublishSettingList[" + apiPublish_badge + "].expiriedDate']").val()
                ,
                description: $("input[name = 'apiPublishSettingList[" + apiPublish_badge + "].description']").val()
                ,
                badge: apiPublish_badge
            }
        });

    }

    function deleteTrRow(tr) {
        $(tr).parent().parent().remove();
    }


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

    $(".NumDecText").keyup(function () {
        $(this).val($(this).val().replace(/[^0-9.]/g, ''));
    }).bind("paste", function () {
        $(this).val($(this).val().replace(/[^0-9.]/g, '')); //粘贴的不是数字，则替换为''
        //或 return false; 禁用粘贴功能
    }).css("ime-mode", "disabled"); //CSS设置输入法不可用
</script>