<?php $__env->startSection('css'); ?>
<style>
    .text-right{
        text-align: right;
        color: green;
        font-size: 20px;
    }
</style>
<?php $__env->stopSection(); ?>
<?php $__env->startSection('content'); ?>
<?php Fun::lang() ?>
<div class="title-page">
    <div class="page-bar">
        <ul class="page-breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> Dashboard</a></li>
            <li><a href="#">Agency</a></li>
            <li><a href="#">Commission Group</a></li>
        </ul>
    </div>
</div>

<div class="page">

    <div class="top-content">
        <div class="row">
            <div class="col-sm-12 col-xs-12">
                <div class="title-add">
                    <div class="pull-left">
                        <h3>Agency Commission Group</h3>
                    </div>
                </div>
                <div class="pull-right">
                    <!-- <a class="button_sm button_primary" href="<?php echo e(route('accouting-capital.create')); ?>"><span class="fa fa-plus"></span>  Create New</a> -->
                </div>
            </div>
        </div>
    </div>
    <div class="box-body table-responsive">
        <table id="datatable_commission_list" class="table table-striped table-bordered table-hover nowrap">
            <thead>
                <tr>
                    <th>No</th>
                    <th>Date</th>
                    <th>Project Name </th>
                    <th>Item </th>
                    <th>Client Name</th>
                    <th>Agency Name</th>
                    <th>Commission</th>
                    <th>Commission Receive</th>
                </tr>
            </thead>
        </table>
    </div>
</div>
<?php $__env->stopSection(); ?> <?php $__env->startSection('js'); ?>

<script type="text/javascript">
    $(document).ready(function() {
        $('.main-spinner').show();
        $('#datatable_commission_list').DataTable({
            "lengthMenu": [
                [20, 25, 50, 100, 500],
                [20, 25, 50, 100, 500]
            ],
            ordering: false,
            serverSide: false,
            ajax: "<?php echo e(route('get-report_commission_index')); ?>",
            columns: [{
                    data: 'DT_RowIndex'
                },
                {
                    data: 'created_at'
                },
                {
                    data: 'project_name'
                },
                {
                    data: 'item'
                },
                {
                    data: 'client_name'
                },
                {
                    data: 'agency_name'
                },
                {
                    className:"total_commission",
                    data: 'amount'
                },
                {
                    data: 'receive'
                }
            ],
            "initComplete": function(settings, json) {
                $('.main-spinner').hide();
            }
        });

        $.fn.dataTable.ext.errMode = 'throw';
    });

    function shareCommission(id, type, commission) {
        $('#commission_id').val(id);
        $('#commission').val(commission);
        $('#agent_type').val(type);
        $('#share-commission').modal('show');
    }
    $(document).on('click', '.btn_add_share', function() {

        var commission = parseFloat($('#commission').val());
        var shared_amount = 0;
        $('.commission_shared').each(function() {
            shared_amount += parseFloat($(this).val());
        });
        if(shared_amount>commission){
            $.alert({
                title: 'Error',
                icon: 'fa fa-warning',
                type: 'red',
                content: 'Over Amount.' +
                    '<hr>' +
                    'Your Total Shared Commission must be <= $'+(commission.toFixed(2))+'',
            });
            $('button[type="submit"]').prop('disabled',true);
        }else{
            $('.dynamic-div').append(add_share());
            $('.select2').select2();
        }
        
    });

    function add_share() {
        return `<div class="row col-sm-12 row-shared">
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-md-3 required control-label">Agency</label>
                            <div class="col-md-9">
                            <?php echo Form::select('agency_id[]', $agency, null, ['class' => 'form-control select2 agency_id','id'=>'agency_id','required']); ?>

                            </div>
                        </div>
                    </div>
                    <div class="col-sm-5">
                        <div class="form-group">
                            <label class="col-md-3 required control-label">Commission </label>
                            <div class="col-md-9">
                                <input type="text" name="commission_shared[]" class="form-control commission_shared" value="" required>
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-1">
                        <div class="form-group">
                            <button type="button" class="button_danger btn-circle btn-remove-share"><i class="fa fa-minus-circle"></i></button>
                        </div>
                    </div>
                </div>`;
    }
    $(document).on('click', '.btn-remove-share', function() {
        $(this).closest('.row-shared').remove();
    });
    $(document).on('keyup', '.commission_shared', function() {
        var e = $(this);
        var commission = parseFloat($('#commission').val());
        var shared_amount = 0;
        $('.commission_shared').each(function() {
            shared_amount += parseFloat($(this).val()==null?0:$(this).val());
        });
        if (shared_amount > commission) {
            $.alert({
                title: 'Error',
                icon: 'fa fa-warning',
                type: 'red',
                content: 'Over Amount.' +
                    '<hr>' +
                    'Your Total Shared Commission must be <= $'+(commission.toFixed(2)),
            });
            $('button[type="submit"]').prop('disabled',true);
        }else{
            $('button[type="submit"]').prop('disabled',false);
        }
    });
    $('#form-share-commission').submit(function(e){
        var commission = $('#commission').val();
        var shared_commission = 0;
        $('.commission_shared').each(function(){
            shared_commission += $(this).val()==""?0:parseFloat($(this).val());
        });
        var remain = commission - shared_commission;
        if(shared_commission < parseFloat(commission)){
            
            $.alert({
                title: 'Error',
                icon: 'fa fa-warning',
                type: 'red',
                content: 'Over Amount.' +
                    '<hr>' +
                    'Please Share All commission ( $' +(parseFloat(commission).toFixed(2))+ ' ), you have $'+(parseFloat(remain).toFixed(2)) + 'lelf!',
            });
            return false;
        }
        else if(shared_commission > parseFloat(commission)) {
            $.alert({
                title: 'Error',
                icon: 'fa fa-warning',
                type: 'red',
                content: 'Over Amount.' +
                    '<hr>' +
                    'Your Total Shared Commission must be '+(commission.toFixed(2)),
            });
            return false;
        }
    });
    $(document).on('click','.btn-view-commission',function(){
        var id = $(this).data('id');
        var e = $(this);
        route = "<?php echo e(route('get-commission-info',':id')); ?>";
        route = route.replace(':id',id);
        $.ajax({
            type: 'GET',
            url: route,
            dataType: 'json',
            data: {
                _token: '<?php echo e(csrf_token()); ?>',
                id: id
            },
            success: function(data) {
                var total_commission = e.parent().siblings('.total_commission').text();
                var avaialable_commission = data['balance'];
                var status = data['status'];
                $('#total_commission').val("$ "+parseFloat(total_commission).toFixed(2));
                $('#commission_status').val(status);
                $('#avaialable_commission').val("$ "+avaialable_commission);
                $('#view-commission').modal('show');
            }
        });
        
    });
</script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('adminlte::page', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>