@extends('vip.layouts')
@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" style="width: 200px;">代理名&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" id="agent_name" name="agent_name" required lay-verify="required" placeholder="" autocomplete="off"
                       class="layui-input" value="{{isset($data['agent_user'])?$data['agent_user']:''}}" readonly="readonly">
               <input type="hidden" id="gid" name="gid" required autocomplete="off" disabled="" 
               class="layui-input" value="{{isset($data['agent_identity'])?$data['agent_identity']:''}}" readonly="readonly">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 200px;">代理等级&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="grade" id="form_grade" required lay-verify="required" placeholder="" autocomplete="off"
                class="layui-input" value="{{isset($data['grade'])?$data['grade']:''}}" disabled="disabled">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 200px;">结算时间年</label>
            <div class="layui-input-inline">
                <select name="year" id="form_year" lay-filter="testy"  autocomplete="off" eventType="event-query">
                    @foreach($data['years'] as $k=>$v):
                        @if($data['year']==$v)
                            <option value="{{$v}}" data-id="{{$data['wnum'][$v]}}" selected="selected">{{$v}}</option>
                        @else
                            <option data-id="{{$data['wnum'][$v]}}" value="{{$v}}">{{$v}}</option>
                        @endif
                    @endforeach
                </select>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">结算时间周</label>
                <div class="layui-input-inline">
                    <select name="week" id="form_week" lay-filter="testw"  autocomplete="off" eventType="event-query">
                        @foreach($data['weeks'] as $sk=>$sv):
                            @if($data['week']==$sv)
                                <option value="{{$sv}}" selected="selected">{{$sv}}</option>
                            @else
                                <option value="{{$sv}}">{{$sv}}</option>
                            @endif
                        @endforeach
                    </select>
                </div>
            </div>
            <input type="button" class="layui-btn layui-btn-warm" onclick="sumMoney()" value="计算金额">
            @foreach($data['wday'] as $k=>$v)
                @if($data['curyw'] == $k)
                    <div class="ywday" id="{{$k}}" style="color:red;padding-left: 230px;">*结算日期区间：{{$v}}</div>
                @else
                    <div class="ywday" id="{{$k}}" style="display: none;color:red;padding-left: 230px;">*结算日期区间：{{$v}}</div>
                @endif
            @endforeach
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 200px;">结算业绩比例（百分比）&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="ratio"  id="ratio"required lay-verify="required" placeholder="" autocomplete="off"
                       class="layui-input" value="{{isset($data['water']['ratio']['water_ratio'])?$data['water']['ratio']['water_ratio']:''}}" readonly="readonly">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 200px;">所选时间直推会员业绩&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="swater" required lay-verify="required" placeholder="" autocomplete="off"
                       class="layui-input" value="{{isset($data['water']['sae'])?$data['water']['sae']:0}}" readonly="readonly">
            </div>
            <div class="layui-input-inline">
                <span style="color: red"></span>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 200px;">所选时间直属代理业绩&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="gwater" required lay-verify="required" placeholder="" autocomplete="off"
                       class="layui-input" value="{{isset($data['water']['gae'])?$data['water']['gae']:0}}" readonly="readonly">
            </div>
            <div class="layui-input-inline">
                <span style="color: red">*若代理等级与直属代理等级相同则对应业绩佣金为0</span>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 200px;">所选时间总业绩&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="aewater" required lay-verify="required" placeholder="" autocomplete="off"
                       class="layui-input" value="{{isset($data['water']['ae'])?$data['water']['ae']:0}}" readonly="readonly">
            </div>
            <div class="layui-input-inline">
                <span style="color: red">*直推会员业绩 + 直属代理业绩</span>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 200px;">直推会员结算金额<font color="red"> (直推业绩*业绩分成比例)</font></label>
            <div class="layui-input-inline">
                <input type="text" name="sgetMoney" required lay-verify="required" placeholder="" autocomplete="off"
                       class="layui-input" value="{{isset($data['water']['saemoney'])?$data['water']['saemoney']:0}}">
            </div>
            <div class="layui-form-mid layui-word-aux">*系统自动计算的结算金额，也可以手动编辑结算金额</div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 200px;">直属代理结算金额<font color="red"> (直属代理业绩*（代理分成比例－直属代理分成比例)）</font></label>
            <div class="layui-input-inline">
                <input type="text" name="ggetMoney" required lay-verify="required" placeholder="" autocomplete="off"
                       class="layui-input" value="{{isset($data['water']['gaemoney'])?$data['water']['gaemoney']:0}}">
            </div>
            <div class="layui-form-mid layui-word-aux">*系统自动计算的结算金额，也可以手动编辑结算金额</div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 200px;">本次结算总金额<font color="red"> (直推会员结算金额+直属代理结算金额）</font></label>
            <div class="layui-input-inline">
                <input type="text" name="aemoney" required lay-verify="required" placeholder="" autocomplete="off"
                       class="layui-input" value="{{isset($data['water']['aemoney'])?$data['water']['aemoney']:0}}">
            </div>
            <div class="layui-form-mid layui-word-aux">*系统自动计算的结算金额，也可以手动编辑结算金额</div>
        </div>
        
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 200px;">结算方式</label>
            <div class="layui-input-block">
                <input type="radio" name="type" lay-filter="test" value="1" title="结算到代理" checked>
                <input type="radio" name="type" lay-filter="test" value="2" title="结算到用户" >
            </div>
            <div class="layui-form-mid layui-word-aux">*结算到用户将结算金额直接结算到代理得到用户账号</div>
        </div>

        <div class="layui-form-item" id="select_show" style="display: none">
            <label class="layui-form-label" style="width: 200px;">结算理由</label>
            <div class="layui-input-inline">
                <input type="text" name="reason" placeholder="" 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>

        var curyear = $('#form_year').val();
        var weekid = $("[value|='" + curyear + "']").attr('data-id');
        weekselect(1000);
        function weekselect(times){
            var os = $('#form_week option');
            os.each(function(){
                $(this).show();
                if($(this).attr('value')>parseInt(weekid)){
                    $(this).hide();
                }
            });
            setTimeout(function(){
                var ts = $("#form_week").next().children('dl').children();
                ts.each(function(){
                    $(this).show();
                    if($(this).attr('lay-value')>parseInt(weekid)){
                        $(this).hide();
                    }
                });
            },times) 
        }

        //Demo
        layui.use(['form'], function () {
            var form = layui.form;
            form.on('select(testy)', function(data){
                curyear = $('#form_year').val();
                weekid = $("[value|='" + curyear + "']").attr('data-id');
                weekselect(10);
                var curw = $('#form_week').val();
                $('.ywday').hide();
                $("#"+curyear+curw).show();
            });

            form.on('select(testw)', function(data){
                curyear = $('#form_year').val();
                var curw = $('#form_week').val();
                $('.ywday').hide();
                $("#"+curyear+curw).show();
            });

            //监听提交
            form.on('submit(submitForm)', function (data) {
                return true;
            });
            form.on('radio(test)', function (data) {
                if (data.value == 1) {
                    $('#select_show').hide();
                } else {
                    $('#select_show').show();
                }
            });
        });

        layui.use('laydate', function(){
            var laydate = layui.laydate;
            lay('.test-item').each(function(){
                laydate.render({
                    elem: this
                    ,type: 'date'
                    ,trigger: 'click'
                    ,btns:['now']
                    ,done: function(value, date){
                        $('.lay-btn-diy').click();
                    }
                });
            });
        });

        function sumMoney() {
            var year=$('#form_year').val();
            var week=$('#form_week').val();
            var gid=$('#gid').val();
            if(year==''||week==''){
                layer.msg('请选择计算时间');
            }else if(gid==''){
                layer.msg('代理ID有误');
            }else{
               $.ajax({
                    url:'/admin/nagent/SumMoney',
                    type:'post',
                    dataType:'json',
                    data:{year:year,week:week,gid:gid},
                    success:function (data) {
                        layer.msg(data.msg);
                        var val=data.data;
                        $('input[name="swater"]').val(val.sae);
                        $('input[name="gwater"]').val(val.gae);
                        $('input[name="aewater"]').val(val.ae);
                        $('input[name="sgetMoney"]').val(val.saemoney);
                        $('input[name="ggetMoney"]').val(val.gaemoney);
                        $('input[name="aemoney"]').val(val.aemoney);
                        if(val.ratio != undefined){
                            $('input[name="ratio"]').val(val.ratio.water_ratio);
                        }
                    }
                }); 
           }   
        }

        function cbFormSuccess(data){
            layer.alert(data.msg,{},function(){
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index);
                window.history.go(-1);
            });
        }
    </script>
@endsection
