<?php $__env->startSection('title'); ?>
    New Release
<?php $__env->stopSection(); ?>

<?php $__env->startSection('style'); ?>
    
    

    <style>
    .checkbox label:after {
      content: '';
      display: table;
      clear: both;
    }

    .checkbox .cr {
      position: relative;
      display: inline-block;
      border: 1px solid #a9a9a9;
      border-radius: .25em;
      width: 1.3em;
      height: 1.3em;
      float: left;
      margin-right: .5em;
    }

    .checkbox .cr .cr-icon {
      position: absolute;
      font-size: .8em;
      line-height: 0;
      top: 50%;
      left: 15%;
    }

    .checkbox label input[type="checkbox"] {
      display: none;
    }

    .checkbox label input[type="checkbox"]+.cr>.cr-icon {
      opacity: 0;
    }

    .checkbox label input[type="checkbox"]:checked+.cr>.cr-icon {
      opacity: 1;
    }

    .checkbox label input[type="checkbox"]:disabled+.cr {
      opacity: .5;
    }
    </style>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('content'); ?>
    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper" >
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>
                <span class="fa fa-edit"></span> New Release
            </h1>
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-edit"></i> Admin Panel</a></li>
                <li class="active">New Release</li>
            </ol>
        </section>

        <!-- Main content -->
        <section class="content" style=" padding-bottom: 20%;">
            <div class="page-header">
                
                <a href="<?php echo e(url()->previous()); ?>" class="btn btn-primary"><i class="fa fa-backward"></i> Back</a>
            </div>

            <input type="hidden" name="release_amount" id="release_amount" class="form-control" value="<?php echo e(number_format($rls_release, 2)); ?>" readonly>
            <form method="post" action="<?php echo e(route('releases.new.post')); ?>">
                <input type="hidden" name="gl_id" id="gl_id" class="form-control" value="<?php echo e($id); ?>" readonly>
                <input type="hidden" name="rcvid" id="rcvid" class="form-control" value="<?php echo e($rcvd_id); ?>" readonly>
                <input type="hidden" id="agent_id" name="agent_id" value="<?php echo e($header[0]->agent_id); ?>">
                <input type="hidden" id="umanager_id" name="umanager_id" value="<?php echo e($header[0]->unit_manager_id); ?>">
                <input type="hidden" id="referrer_id" name="referrer_id" value="<?php echo e($header[0]->referrer_id); ?>">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label for="" class="control-label">Project / Developer:</label>
                            <span> <?php echo e($header[0]->dev_name); ?> // <?php echo e($header[0]->prj_name); ?></span>
                        </div>
                        <div class="form-group">
                            <label for="" class="control-label">Blk / Lot:</label>
                            <span> <?php echo e($header[0]->blk); ?> | <?php echo e($header[0]->lot); ?></span>
                        </div>
                        <div class="form-group">
                            <label for="" class="control-label">Client Name:</label>
                            <span> <?php echo e($header[0]->client_name); ?></span>
                            <label for="" class="control-label" style="margin-left:-10px;float:right!important">Received Amount:
                            <span id="rcvd_amt"> <?php echo e(number_format($header[0]->amount, 2)); ?></span> </label>
                            <br>
                            <label for="" class="control-label" style="margin-left:-10px;float:right!important"><span> <?php echo e($header[0]->pay_mode); ?></span></label>
                        </div>
                        <div class="form-group">
                            <label for="" class="control-label">Agent Name:</label>
                            <span class="label label-warning"><?php echo e($header[0]->agent_name); ?></span>
                            <br>
                            <label for="" class="control-label">Total Receivable :</label>
                            <span id="a_ttl_rcvd" class=""><?php echo e(number_format($sale_comm->agent_com_rate,2) ?? 0); ?></span>
                            <br>
                            <label for="" class="control-label">Total Released :</label>
                            <span class="" id="a_ttl_rls"><?php echo e(number_format((float) $rls_release, 2) ?? 0); ?></span>
                            
                        </div>
                        <div class="form-group">
                          <label for="" class="control-label">Unit Manager:</label>
                          <span class="label label-success"><?php echo e($header[0]->um_name); ?></span>
                          <br>
                          <label for="" class="control-label">Total Receivable :</label>
                          <span class="" id="m_ttl_rcvd"><?php echo e(number_format($sale_comm->um_com_rate,2) ?? 0); ?></span>
                          <br>
                          <label for="" class="control-label">Total Released :</label>
                          <span class="" id="m_ttl_rls"><?php echo e(number_format((float) $ttl_release_mgr, 2) ?? 0); ?></span>
                          
                        </div>

                        

                        <div class="form-group">
                            <input type="hidden" name="max_release" id="max_release" class="form-control" value="<?php echo e($max_rls, 2 ?? 0); ?>" style="text-align: right" readonly>
                            <input type="hidden" name="ttl_receive" id="ttl_receive" class="form-control" value="<?php echo e($header[0]->amount ?? 0); ?>" style="text-align: right" readonly> 
                        </div>
                        <div class="form-group" style="height: 50px;">
                        
                        </div>

                    </div>

                    <div class="col-md-3">
                        <div class="form-group has-feedback <?php if($errors->has('agent_pcg_rate')): ?> has-error <?php endif; ?>">
                            <label for="agent_pcg_rate" class="control-label">Agent Rate (%):</label>
                            <input type="text" name="agent_pcg_rate" id="agent_pcg_rate" class="form-control comm_rate allownumericwithdecimal" value="<?php echo e($share[0]->shr_agent); ?>" style="text-align: right" step="1" min="1">
                            <span class="glyphicon glyphicon-zoom-out form-control-feedback"></span>
                            <?php if($errors->has('agent_pcg_rate')): ?> <span class="help-block"><?php echo e($errors->first('agent_pcg_rate')); ?></span> <?php endif; ?>
                        </div>
                    </div>

                    <div class="col-md-3">
                        <div class="form-group has-feedback <?php if($errors->has('agent_com_rate')): ?> has-error <?php endif; ?>">
                            <label for="agent_com_rate" class="control-label">Agent Commission:</label>
                            <input type="text" name="agent_com_rate" id="agent_com_rate" class="form-control allownumericwithdecimal" value="" style="text-align: right" readonly>
                            <span class="glyphicon glyphicon-zoom-out form-control-feedback"></span>
                            <?php if($errors->has('agent_com_rate')): ?> <span class="help-block"><?php echo e($errors->first('agent_com_rate')); ?></span> <?php endif; ?>
                        </div>
                    </div>

                    <div class="col-md-3">
                        <div class="form-group has-feedback <?php if($errors->has('um_pcg_rate')): ?> has-error <?php endif; ?>">
                            <label for="um_pcg_rate" class="control-label">Unit Manager (%):</label>
                            <input type="text" name="um_pcg_rate" id="um_pcg_rate" class="form-control comm_rate allownumericwithdecimal" value="0.00" style="text-align: right" step="1" min="1"> 
                            <span class="glyphicon glyphicon-zoom-out form-control-feedback"></span>
                            <?php if($errors->has('um_pcg_rate')): ?> <span class="help-block"><?php echo e($errors->first('um_pcg_rate')); ?></span> <?php endif; ?>
                        </div>
                    </div>

                    <div class="col-md-3">
                        <div class="form-group has-feedback <?php if($errors->has('um_com_rate')): ?> has-error <?php endif; ?>">
                            <label for="um_com_rate" class="control-label">Unit Manager Commission:</label>
                            <input type="text" name="um_com_rate" id="um_com_rate" class="form-control" value="" style="text-align: right" readonly>
                            <span class="glyphicon glyphicon-zoom-out form-control-feedback"></span>
                            <?php if($errors->has('um_com_rate')): ?> <span class="help-block"><?php echo e($errors->first('um_com_rate')); ?></span> <?php endif; ?>
                        </div>
                    </div>

                    <div class="col-md-3">
                        <div class="form-group has-feedback <?php if($errors->has('broker_pcg_rate')): ?> has-error <?php endif; ?>">
                            <label for="broker_pcg_rate" class="control-label">Broker Commission (%):</label>
                            <input type="text" name="broker_pcg_rate" id="broker_pcg_rate" class="form-control comm_rate" value="<?php echo e($share[0]->shr_broker); ?>" style="text-align: right" step="1" min="1">
                            <span class="glyphicon glyphicon-zoom-out form-control-feedback"></span>
                            <?php if($errors->has('broker_pcg_rate')): ?> <span class="help-block"><?php echo e($errors->first('broker_pcg_rate')); ?></span> <?php endif; ?>
                        </div>
                    </div>

                    <div class="col-md-3">
                        <div class="form-group has-feedback <?php if($errors->has('broker_com_rate')): ?> has-error <?php endif; ?>">
                            <label for="broker_com_rate" class="control-label">Broker Commission:</label>
                            <input type="text" name="broker_com_rate" id="broker_com_rate" class="form-control" value="" style="text-align: right" readonly>
                            <span class="glyphicon glyphicon-zoom-out form-control-feedback"></span>
                            <?php if($errors->has('broker_com_rate')): ?> <span class="help-block"><?php echo e($errors->first('broker_com_rate')); ?></span> <?php endif; ?>
                        </div>
                    </div>

                    <div class="col-md-3">
                        <div class="form-group has-feedback <?php if($errors->has('ref_pcg_rate')): ?> has-error <?php endif; ?>">
                            <label for="ref_pcg_rate" class="control-label">Referrer Commission (%):</label>
                            <input type="text" name="ref_pcg_rate" id="ref_pcg_rate" class="form-control comm_rate" value="<?php echo e($share[0]->shr_refer); ?>" style="text-align: right" step="0.1" min="0.1">
                            <span class="glyphicon glyphicon-zoom-out form-control-feedback"></span>
                            <?php if($errors->has('ref_pcg_rate')): ?> <span class="help-block"><?php echo e($errors->first('ref_pcg_rate')); ?></span> <?php endif; ?>
                        </div>
                    </div>

                    <div class="col-md-3">
                        <div class="form-group has-feedback <?php if($errors->has('ref_com_rate')): ?> has-error <?php endif; ?>">
                            <label for="ref_com_rate" class="control-label">Referrer Commission:</label>
                            <input type="text" name="ref_com_rate" id="ref_com_rate" class="form-control" value="" style="text-align: right" readonly>
                            <span class="glyphicon glyphicon-zoom-out form-control-feedback"></span>
                            <?php if($errors->has('ref_com_rate')): ?> <span class="help-block"><?php echo e($errors->first('ref_com_rate')); ?></span> <?php endif; ?>
                        </div>
                    </div>

                    <div class="col-md-3">
                        <div class="form-group has-feedback <?php if($errors->has('ca_amount')): ?> has-error <?php endif; ?>">
                            <label for="ca_amount" class="control-label">Agent C/A Payment:</label>
                            <div style="float: right;font-size: 12px;font-style: italic">
                                <span>Bal: <?php echo e(number_format($ca_bal,2) ?? 0); ?></span>
                            </div>
                            <input type="text" name="ca_amount" id="ca_amount" class="form-control" value="0.00" style="text-align: right">
                            <span class="glyphicon glyphicon-zoom-out form-control-feedback"></span>
                            <?php if($errors->has('ca_amount')): ?> <span class="help-block"><?php echo e($errors->first('ca_amount')); ?></span> <?php endif; ?>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group has-feedback <?php if($errors->has('um_ca_amount')): ?> has-error <?php endif; ?>">
                            <label for="um_ca_amount" class="control-label">Unit Manager C/A Payment:</label>
                            <div style="float: right;font-size: 12px;font-style: italic">
                                <span>Bal: <?php echo e(number_format($um_bal,2) ?? 0); ?></span>
                            </div>
                            <input type="text" name="um_ca_amount" id="um_ca_amount" class="form-control" value="0.00" style="text-align: right">
                            <span class="glyphicon glyphicon-zoom-out form-control-feedback"></span>
                            <?php if($errors->has('um_ca_amount')): ?> <span class="help-block"><?php echo e($errors->first('um_ca_amount')); ?></span> <?php endif; ?>
                        </div>
                    </div>

                    <div class="col-md-12">
                        <div class="form-group">
                            <label for="remarks" class="control-label">Remarks:</label>
                            <textarea class="form-control txt" rows="4" name="remarks" id="remarks" placeholder=""></textarea>
                        </div>
                    </div>
                    

                <div class="form-group" style="float:left;padding: 10px;padding-top:20px;padding-bottom:50px;">
                    <button id="btnOk" name="btnOk" type="submit" class="btn btn-success btn-s"><i class="fa fa-save"></i> Save </button>
                    <a href="<?php echo e(url()->previous()); ?>" class="btn btn-primary"><i class="fa fa-arrow-left"></i> Cancel</a>
                </div>
                <?php echo e(csrf_field()); ?>

            </form>

        </section>
        <?php if(Session('info')): ?>
            <div class="row">
                <div class="col-md-6 col-md-offset-3 text-center">
                    <div class="tem alert alert-success navbar-fixed-bottom"><span class="glyphicon glyphicon-ok-circle"></span> <?php echo e(Session('info')); ?></div>
                </div>
            </div>
        <?php endif; ?>

        <!--Modal Notif-->
        <div class="modal fade bs-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" style=" padding-right: 0px;background-color: rgba(4, 4, 4, 0.8);">
          <div class="modal-dialog modal-lg" style="top: 20%;width: 70%;text-align: center;">
            <div class="modal-content" style="border-radius: 0px;border: none;top: 40%;">
              <div class="modal-body" style="background-color: #0f8845;color: white;">
                <h2 id="h22"></h2>
                <h4 id="h44"></h4>
              </div>
            </div>
          </div>
        </div>

    </div>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('script'); ?>

    
    <script type="text/javascript" src="<?php echo e(asset('js/numeric/jquery.masknumber.js')); ?>" ></script>
    <script type="text/javascript" src="<?php echo e(asset('js/bootstrap-toggle.min.js')); ?>" ></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

    <script>
        $(document).ready(function () {
            //$('#total_selling_price').maskNumber();
            //$('#equity').maskNumber();
            //$('#loanable_amount').maskNumber();

            $('#release_amount').maskNumber();
            $('#agent_com_rate').maskNumber();
            $('#um_com_rate').maskNumber();
            $('#broker_com_rate').maskNumber();
            $('#ref_com_rate').maskNumber();
            $('#ca_amount').maskNumber();
            $('#um_ca_amount').maskNumber();

            sum_comm();
            init_data();

        });

        $('.comm_rate').on('change blur', function () { //keyup keydown
            sum_comm();
            init_data();
        });

        $('#remarks').on('click', function () { //keyup keydown
            //alert('readyyyyyyyy');
            //init_data();
        });

        function removeCommas(str) {
            while (str.search(",") >= 0) {
                str = (str + "").replace(',', '');
            }
            return str;
        }

        function sum_comm() {
            var sum = 0;
            //var nsp = $('#net_selling_price').val();
            //var rate = $(this).val();
            //var y = $('#ttl_receive').val();
            var num = $('#ttl_receive').val(); //realParseFloat(y);

            $('.comm_rate').each(function() {
                sum += Number($(this).val());
            });

            if (sum < 100) {
                console.log('kulang');
                $('#btnOk').attr("disabled", false);
                //$('a[href="#finish"]').css('display', 'none');
            } else if (sum > 100) {
                console.log('sobra');
                alert('Total sharing % exceeded.');
                $('#btnOk').attr("disabled", true);
            } else if (sum == 100) {
                console.log('sakto. daw coke');
                $('#btnOk').attr("disabled", false);
                //$('a[href="#finish"]').css('display', 'block');
            }

            compute_comm(num);
            console.log(sum);

        }

        function compute_comm(nsp) {

            //var y = $('#net_selling_price').val();
            //var num = realParseFloat(y);
            //console.log(nsp);

            //var nsp             = $('#net_selling_price').val();
            var agt_pcg_rate    = $('#agent_pcg_rate').val();
            var um_pcg_rate     = $('#um_pcg_rate').val();
            var broker_pcg_rate = $('#broker_pcg_rate').val();
            var ref_pcg_rate    = $('#ref_pcg_rate').val();

            var agt_com_amt     = nsp * (agt_pcg_rate / 100);
            var um_com_amt      = nsp * (um_pcg_rate / 100);
            var broker_com_amt  = nsp * (broker_pcg_rate / 100);
            var ref_com_amt     = nsp * (ref_pcg_rate / 100);

            console.log(agt_com_amt);
            console.log(um_com_amt);

            $('#agent_com_rate').val(formatnum(agt_com_amt));
            $('#um_com_rate').val(formatnum(um_com_amt));
            $('#broker_com_rate').val(formatnum(broker_com_amt));
            $('#ref_com_rate').val(formatnum(ref_com_amt));
        }

        function init_data() {
            var a_rls   = $('#a_ttl_rls').text();
            var a_rcvd  = $('#a_ttl_rcvd').text();
            var m_rls   = $('#m_ttl_rls').text();
            var m_rcvd  = $('#m_ttl_rcvd').text();

            var a_comm  = $('#agent_com_rate').val();
            var m_comm  = $('#um_com_rate').val();

            var a_rcvd2 = parseFloat(removeCommas(a_rcvd));
            var m_rcvd2 = parseFloat(removeCommas(m_rcvd));

            var a_rcv   = parseFloat(removeCommas(a_rls))+parseFloat(removeCommas(a_comm));
            var m_rcv   = parseFloat(removeCommas(m_rls))+parseFloat(removeCommas(m_comm));

            check_rcvd(a_rcv, a_rcvd2, m_rcv, m_rcvd2);

            /*if (a_rcvd >= a_rls || m_rcvd >= m_rls) {
                alert('Okeyy keeyu');
                $('#btnOk').attr("disabled", false);
            } else {
                alert('Warning: You have an Overpayment commission.');
                $('#btnOk').attr("disabled", true);
            }*/
        }

        function check_rcvd(a_rcv, a_rcvd2, m_rcv, m_rcvd2) {
            if (a_rcv > a_rcvd2 || m_rcv > m_rcvd2) {
                //alert('You have an Overpayment agent commission.');
                //console.log('overpayment agent');
                console.log('entry: '+a_rcv+' '+a_rcvd2);
                $('#btnOk').attr("disabled", false);
                //alert('Warning: You have an Overpayment commission.');
                //alert('Maintenance mode.');
                /*commented out*/
                /*if (confirm("Perform auto-match commission")) {
                    //txt = "You pressed OK!";
                    auto_amount();
                } else {
                    //txt = "You pressed Cancel!";
                }*/
            } /*else if (m_rcv > m_rcvd2) {
                //alert('You have an Overpayment manager commission.');
                console.log('overpayment manager');
                console.log('entry: '+m_rcv+' '+m_rcvd2);
                $('#btnOk').attr("disabled", true);
                alert('Warning: You have an Overpayment commission.');
            }*/
            else {
                //$('#btnOk').attr("disabled", true);
                console.log('entry: '+a_rcv+' '+a_rcvd2);
                console.log('entry: '+m_rcv+' '+m_rcvd2);
            }

            /*if (a_rcvd >= a_rls || m_rcvd >= m_rls) {
                //alert('Okeyy keeyu');
            } else {
                alert('Warning: You have an Overpayment commission.');
            }*/
        }

        function auto_amount() {

            var a_rls       = $('#a_ttl_rls').text();
            var a_rcvd      = $('#a_ttl_rcvd').text();
            var m_rls       = $('#m_ttl_rls').text();
            var m_rcvd      = $('#m_ttl_rcvd').text();

            var a_bal       = parseFloat(removeCommas(a_rcvd))-parseFloat(removeCommas(a_rls));
            var m_bal       = parseFloat(removeCommas(m_rcvd))-parseFloat(removeCommas(m_rls));

            var rcvd_amt    = $('#rcvd_amt').text();
            var a_pct       = $('#agent_pcg_rate').val();
            var a_com       = $('#agent_com_rate').val();
            var m_pct       = $('#um_pcg_rate').val();
            var m_com       = $('#um_com_rate').val();
            //alert(rcvd_amt);

            var a_rate      = (a_bal / parseFloat(removeCommas(rcvd_amt)))*100;
            var m_rate      = (m_bal / parseFloat(removeCommas(rcvd_amt)))*100;
            //alert(a_rate);
            $('#agent_pcg_rate').val(a_rate);
            $('#agent_com_rate').val(formatnum(a_bal));
            $('#um_pcg_rate').val(m_rate);
            $('#um_com_rate').val(formatnum(m_bal));

            $('#btnOk').attr("disabled", false);

        }

        function formatnum(n) {
            return n.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,");
        }

        function notifPopup(title,body) {
            $('#h22').text(title);
            $('#h44').text(body);
            $('.bs-modal-lg').modal('toggle');
            setTimeout(function() {$('.bs-modal-lg').modal('hide');
            }, 10000);
        }

    </script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('admin.layouts.master', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>