<%--
  Created by IntelliJ IDEA.
  User: wuxuan
  Date: 2023/5/6
  Time: 14:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page pageEncoding="UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<% String basePath = request.getContextPath(); %>
<html>
<head>
    <title>Title</title>
    <style>
        .star {
            color: red;
        }
    </style>
</head>
<body>
<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
    <h4 class="modal-title">${type == 0 ? '空号检测条数充值' : '空号检测条数扣除'}</h4>
</div>
<div class="modal-body">
    <form id="FailAgainForm" style="width: 100%">
        <input type="hidden" name="type" value="${type}"/>
        <div class="container2">
            <div class="main_20">
                <table width="100%" border="0" cellspacing="0" cellpadding="0" class="infotable">
                    <tr>
                        <th scope="row">
                            <div class="floatLeft"><i class="star">*</i>企业ID：</div>
                        </th>
                        <td width="62%">
                            <input class="form_control form_white" type="text" name="spId" id="spId"
                                   style="width: 100%">
                        </td>
                    </tr>
                    <c:if test="${type == 0}">
                        <tr>
                            <th scope="row">
                                <div class="floatLeft"><i class="star">*</i>充值金额:</div>
                            </th>
                            <td width="62%">
                                <input class="form-control form_white" type="number" name="chargeMoney" id="chargeMoney"
                                       style="width: 100%">
                                <span>单位:元,可以输入小数</span>
                            </td>
                        </tr>
                        <tr>
                            <th scope="row">
                                <div class="floatLeft"><i class="star">*</i>单价:</div>
                            </th>
                            <td width="62%">
                                <input class="form-control form_white" type="number" name="price" id="price"
                                       style="width: 100%">
                                <span>单位分,注:此单价只做方便计算金额转条数,不做成本计算,也可以在充值条数输入框直接输入条数</span>
                            </td>
                        </tr>
                        <tr>
                            <th scope="row"></th>
                            <td width="62%">
                                <button type="button" class="btn btn_blue" onclick="doCalculate()">计算条数</button>
                            </td>
                        </tr>
                        <tr>
                            <th scope="row">
                                <div class="floatLeft"><i class="star">*</i>充值条数:</div>
                            </th>
                            <td width="62%">
                                <input class="form-control form_white" type="number" name="chargeMoney"
                                       id="chargeNumber" style="width: 100%">
                            </td>
                        </tr>
                    </c:if>
                    <c:if test="${type == 1}">
                        <tr>
                            <th scope="row">
                                <div class="floatLeft"><i class="star">*</i>扣除条数：</div>
                            </th>
                            <td width="62%">
                                <input class="form_control form_white" type="number" name="deductionValue"
                                       id="deductionValue"
                                       style="width: 100%">
                            </td>
                        </tr>
                    </c:if>
                     <tr>
                            <th scope="row">
                                <div class="floatLeft">备注信息：</div>
                            </th>
                            <td width="62%">
                                <textarea name="remark" id="remark" style="width: 100%" rows="5"></textarea>
                            </td>
                        </tr>
                </table>
            </div>
        </div>
    </form>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn_blue" onclick="doCharge()">确定</button>
    <button type="button" class="btn default" data-dismiss="modal">关闭</button>
</div>
</body>
<script>

    let $chargeMoneyEl = $("#chargeMoney");
    let $priceEl = $("#price");
    let $chargeNumberEl = $("#chargeNumber");

    function doCalculate() {
        let chargeMoney = $chargeMoneyEl.val();
        let price = $priceEl.val();
        if (!chargeMoney) {
            alert("充值金额不能为空");
            return;
        }
        if (isNaN(chargeMoney) || Number(chargeMoney) < 0) {
            alert("充值金额必须为数字");
            return;
        }
        if (!price) {
            alert("单价不能为空");
            return;
        }
        if (isNaN(price) || Number(price) < 0) {
            alert("单价必须为数字");
            return;
        }
        let chargeNumber = Number(chargeMoney) * 100 / Number(price);
        $chargeNumberEl.val(chargeNumber);
    }

    function doCharge() {
        let spId = $("#spId").val();
        let type = $("input[name=type]").val();
        let chargeNumber = $chargeNumberEl.val();
        let deductionValue = $("#deductionValue").val();
        let price = $priceEl.val();
        let remark = $("#remark").val();
        if (!spId) {
            alert("企业ID不能为空");
            return;
        }

        if (type === '0') {
            if (!chargeNumber) {
                alert("充值条数不能为空");
                return;
            }
        } else {
            if (!deductionValue) {
                alert("扣除条数不能为空");
                return;
            }
        }

        let operation = type === "0" ? "充值" + chargeNumber : "扣减" + deductionValue;
        Dialog.confirm("是否确定为" + spId + "执行" + operation + "条的操作?", () => {
            $.ajax({
                url: '<%=basePath%>/nullMobileCheckChargeAdd.html',
                type: 'post',
                data: {
                    spId: spId,
                    chargeValue: type === "0" ? chargeNumber : deductionValue,
                    chargeType: type,
                    price: price,
                    remark: remark
                },
                success: function (res) {
                    if (res.code === 0) {
                        Dialog.alertSucc(res.msg);
                        setTimeout(function () {
                            window.location.reload();
                        }, 1500);
                    } else {
                        Dialog.alertError(res.msg);
                    }
                },
                error: function (res) {
                    Dialog.alertError("请求后台充值失败");
                }
            })
        })
    }
</script>
</html>
