@extends('vip.layouts')
@section('seo_title')
    {{ trans('menu.deductions') }}
@endsection
@section('content')

    <form class="layui-form" action="{{ url()->full() }}" id="submitForm" name="submitForm" method="post"
          eventType=eventAjaxForm lay-filter="submitForm">
        {!! csrf_field() !!}
        <div class="layui-form-item">
            <label class="layui-form-label">用户名&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="account_name" required lay-verify="required" placeholder="" autocomplete="off"
                       class="layui-input" value="{{isset(
      $data['account_name'])?$data['account_name']:''}}" disabled="disabled">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">当前余额&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="balance" required lay-verify="required" placeholder="" autocomplete="off"
                       class="layui-input" value="{{isset(
      $data['cash'])?$data['cash']:''}}" disabled="disabled">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">操&nbsp;作&nbsp;</label>
            <div class="layui-input-inline">
                <input type="radio" lay-filter="test" class="radio" name="type" value="1" title="加钱"
                       @if($type!=2) checked @endif >
                <input type="radio" lay-filter="test" class="radio" name="type" value="2" title="扣款"
                       @if($type==2) checked @endif >
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">金&nbsp;额&nbsp;</label>
            <div class="layui-input-inline" style="width: 150px">
                <input type="number" id="money" name="money" lay-verify="" autocomplete="off" class="layui-input"
                       value="{{isset($data['money'])?$data['money']:''}}">
            </div>
            <div class="layui-form-mid layui-word-aux">*必须为数字</div>

            <div id="rates">
                <label class="layui-form-label">流水倍数&nbsp;</label>
                <div class="layui-input-inline" style="width: 150px">
                    <input type="number" id="rate" name="rate" lay-verify="" autocomplete="off" class="layui-input"
                           value="{{isset($data['rate'])?$data['rate']:1}}">
                </div>
                <div class="layui-form-mid layui-word-aux">*必须为数字</div>
            </div>
        </div>

        <div class="layui-form-item" id="prize">
            <label class="layui-form-label">充值奖励</label>
            <div class="layui-input-inline" style="width: 150px">
                <input type="text" name="peize_bate" lay-verify="" autocomplete="off" class="layui-input" value="0">
            </div>
            <div class="layui-form-mid layui-word-aux">% <font color="red">*(充值奖励为充值金额百分比，0为不奖励)</font></div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">理&nbsp;&nbsp;由&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <textarea name="remark" rows="2" cols="60" style="resize: none"></textarea>
                <!--<input type="text" name="remark" lay-verify="" autocomplete="off" class="layui-input" value="">-->
            </div>
        </div>
        <div class="layui-input-inline">
            <div class="layui-form-mid layui-word-aux">注释：理由如果包含 "用于活动"字眼，则此次加/扣钱的金额不会在'代理存取报表'中体现。</div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">订单号&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="order_id" lay-verify="" autocomplete="off" class="layui-input" value="">
            </div>
            <div class="layui-form-mid layui-word-aux">注释：第三方充值未到账，加款时输入第三方订单号，防止重复到账(第三方订单号正常有包含用户名)。</div>
        </div>


        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn sub" lay-submit lay-filter="submitForm">确认</button>
            </div>
        </div>
    </form>

    <script>
        //Demo
        var flag = false;
        layui.use(['form'], function () {
            var form = layui.form;

//            form.verify({
//                money: function(value){
//                    return '测试成功';
//                }
//            });

            //监听提交
//            form.on('submit(submitForm)', function (data) {
//                return true;
//            });
            document.getElementById("money").addEventListener("input", function (event) {
//                event.target.value = event.target.value.replace(/\-/g,"");
                event.target.value = event.target.value.replace(/^(\-|\+|\=|\_|[0a-zA-Z])/g, "");
            });


            $('.sub').on('click', function () {
                flag = true;
                var name = $('input[name=account_name]').val();
                var type = $('input[name=type]:checked').val();
                var money = $('input[name=money]').val();
                var rate = $('input[name=rate]').val();
                var balance = $('input[name=balance]').val();
                var peize_bate = $('input[name=peize_bate]').val();

                var c_money = parseFloat(rmoney(money));
                balance = parseFloat(balance);
                peize_bate = parseFloat(peize_bate);
                var reward = peize_bate * c_money / 100;
                var str = '';
                str += ' <div class="layui-form-item" id="Tip" style="margin-top: 15px">\n' +
                    '            <label class="layui-form-label">用户名</label>\n' +
                    '            <div class="layui-input-inline">\n' +
                    '                <input type="text" class="layui-input"disabled="disabled" value="' + name + '">\n' +
                    '            </div>\n' +
                    '        </div>';

                str += ' <div class="layui-form-item">\n' +
                    '            <label class="layui-form-label">金 额 </label>\n' +
                    '            <div class="layui-input-inline">\n' +
                    '                <input type="text" class="layui-input" disabled="disabled" value="' + money + '">\n' +
                    '            </div>\n' +
                    '        </div>';
                str += ' <div class="layui-form-item">\n' +
                    '            <label class="layui-form-label">流水倍数 </label>\n' +
                    '            <div class="layui-input-inline">\n' +
                    '                <input type="number" class="layui-input" disabled="disabled" value="' + rate + '">\n' +
                    '            </div>\n' +
                    '        </div>';
                if (type === '1') {
                    str += ' <div class="layui-form-item">\n' +
                        '            <label class="layui-form-label">充值奖励</label>\n' +
                        '            <div class="layui-input-inline">\n' +
                        '                <input type="text" class="layui-input" disabled="disabled" value="' + (peize_bate * c_money / 100) + '">\n' +
                        '            </div>\n' +
                        '        </div>';
                }
                str += ' <div class="layui-form-item" style="margin-top: 15px">\n' +
                    '            <label class="layui-form-label">交易后余额</label>\n' +
                    '            <div class="layui-input-inline">\n';
                if (type === '1') {
                    str += '<input type="text" class="layui-input" disabled="disabled" value="' + Math.floor((c_money + balance + reward) * 100) / 100 + '">\n';
                } else {
                    str += '<input type="text" class="layui-input" disabled="disabled" value="' + (balance - c_money) + '">\n';
                }
                str += '            </div>\n' +
                    '        </div>';

//                $(document).keyup(function (e) {
//                    if (e.keyCode == 32){
//                        event.keyCode=0;
//                        //取出金额的值
//                        var money=$("#money").val();
//                        console.log(money);
//                        var a=rmoney(money);
//                        console.log(a);
//                        $('#money').val(a);
//                        $('#submitForm').submit();
//                        $("#money").val('');
////                        layer.close(index);
//                        event.returnvalue=false;
//                    }
//                });
                layer.open({
                    type: 1,
                    btn: ['确定', '取消'],
                    btnAlign: 'c',
                    id: 'box',
                    content: str,  //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    yes: function (index, layero) {
                        flag = false;
                        layer.close(index);
                        //取出金额的值
                        var money = $("#money").val();
                        var a = rmoney(money);
                        $('#money').val(a);
                        $('#submitForm').submit();
                    }
                });
                return false;
            });


            form.on('radio(test)', function (data) {
                if (data.value == 1) {
                    $('#prize').show();
                    $('#rates').show();
                } else {
                    $('#prize').hide();
                    $('#rates').hide();
                }

            });


        });
        var type = $('input[name="type"]:checked').val();
        if (type == 2) {
            $('#prize').hide();
            $('#rates').hide();
        }

        function cbFormSuccess(data) {
            layer.alert(data.msg, {}, function (index) {
                closeWindows();
                layer.close(index);
//                window.parent.location.reload();
            });

//            $(document).keypress(function (e) {
//                if (e.keyCode == 13){
//                    e.keyCode=0;
//                    window.parent.HideThisDiv();//打开这个窗口的父窗口的某个DIV影藏
//                    $("#money").val('');
//                        layer.close(index);
//                    e.returnvalue=false;
//                }
//            });
//            window.parent.location.reload();


//            var obj=window.parent.document.getElementsByClassName('container');
//            obj.style.display='none';
//            window.parent.location.reload();
//            $('.container').hide();
//            console.log( parent.getFrameIndex(window.name));
//            $('#content').html('');
//            layer.alert(data.msg, {}, function () {
//                var table = window.parent.tab;
//                //执行重载
//                table.reload({
//                    page: {
//                        curr: 1 //重新从第 1 页开始
//                    }
//                    , where: {
//                        account: $("#account_name", parent.document).val(),
//                        sureblur: $('.layui-form-switch', parent.document).text(),
//                    }
//                });
//                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
//                parent.layer.close(index); //再执行关闭
//            });
        };

        function closeWindows() {
            $("#account_name", parent.document).val('');
            var table = window.parent.tab;
            //执行重载
            table.reload({
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                , where: {
                    account_name: $("#form_account_name", parent.document).val(),
//                    sureblur: $('.layui-form-switch', parent.document).text(),
                }
            });
            //关闭下方加款
            $('#content_iframe', window.parent.document).hide();
//            window.parent.location.reload();
//            window.parent.HideThisDiv();//打开这个窗口的父窗口的某个DIV影藏
        }

        //格式化金额还原
        function rmoney(s) {
            return parseFloat(s.replace(/[^\d\.-]/g, ""));
        }

        //格式化金额
        function fmoney(s, n) {
            n = n > 0 && n <= 20 ? n : 2;
            s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
            var l = s.split(".")[0].split("").reverse(), r = s.split(".")[1];
            t = "";
            for (i = 0; i < l.length; i++) {
                t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
            }
            return t.split("").reverse().join("") + "." + r;
        }

        //回车绑定
        $('document').keyup(function (event) {
            if (event.keyCode == 13) {
                try {
                    $('.sub').trigger('click');
                } catch (ex) {
                }

            }
        });
        //空格绑定
        //        $(function () {
        $(document).keypress(function (e) {
            if (e.keyCode == 32) {
                console.log('空格');
                if (flag == true) {
                    console.log('进来了');
                    flag = false;
                    //取出金额的值
                    var money = $("#money").val();
                    console.log('我是空格');
                    console.log(money);
                    var a = rmoney(money);
                    console.log(a);
                    $('#money').val(a);
                    $('#submitForm').submit();
                    $("#money").val('');
                    e.returnvalue = false;
                }
            }
        });

        //        })

    </script>
@endsection
