@extends('admin.layouts.master')

@section('title')
    Edit Release
@stop

@section('style')
    {{--<link href="https://gitcdn.github.io/bootstrap-toggle/2.2.2/css/bootstrap-toggle.min.css" rel="stylesheet">--}}
    {{--<link rel="stylesheet" type="text/css" href="{{asset('/css/bootstrap-toggle.min.css')}}">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">--}}

    <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>
@stop

@section('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> Edit Release
            </h1>
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-edit"></i> Admin Panel</a></li>
                <li class="active">Edit Release</li>
            </ol>
        </section>

        <!-- Main content -->
        <section class="content" style=" padding-bottom: 42%;">
            <div class="page-header">
                {{--route('releases')--}}
                <a href="{{ 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="{{number_format($ttl_rcvd[0]->ttl_receive, 2)}}" readonly>
            <form method="post" action="{{route('releases.update')}}">
                <input type="hidden" name="rcvd_id" id="rcvd_id" class="form-control" value="{{$rcvidd}}" readonly>
                <input type="hidden" name="gl_id" id="gl_id" class="form-control" value="{{$release[0]->gl_id}}" readonly>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label for="" class="control-label">Project / Developer:</label>
                            <span> {{$header[0]->dev_name}} // {{$header[0]->prj_name}}</span>
                        </div>
                        <div class="form-group">
                            <label for="" class="control-label">Blk / Lot:</label>
                            <span> {{$header[0]->blk}} | {{$header[0]->lot}}</span>
                        </div>
                        <div class="form-group">
                            <label for="" class="control-label">Client Name:</label>
                            <span> {{$header[0]->client_name}}</span>
                            <label for="" class="control-label" style="margin-left:-10px;float:right!important">Received Amount: <span> {{number_format($header[0]->amount, 2)}}</span></label>
                            <br>
                            <label for="" class="control-label" style="margin-left:-10px;float:right!important"><span> {{$header[0]->pay_mode}}</span></label>
                        </div>
                        <div class="form-group">
                            <label for="" class="control-label">Agent Name:</label>
                            <span class="label label-warning">{{$header[0]->agent_name}}</span>
                            <br>
                            <label for="" class="control-label">Total Comm Receivable :</label>
                            <span id="a_ttl_rcvd" class="">{{ number_format($sale_comm->agent_com_rate,2) ?? 0 }}</span>
                            <br>
                            <label for="" class="control-label">Total to Release :</label>
                            <span class="" id="a_ttl_rls">{{ number_format((float) $ttl_rls[0]->ttl_release, 2) ?? 0  }}</span>
                            {{--<span class="">{{ number_format($a_rem_rcvd,2) ?? 0 }}</span>--}}
                        </div>

                        <div class="form-group">
                          <label for="" class="control-label">Unit Manager:</label>
                          <span class="label label-success">{{$header[0]->um_name}}</span>
                          <br>
                          <label for="" class="control-label">Total Comm Receivable :</label>
                          <span class="" id="m_ttl_rcvd">{{ number_format($sale_comm->um_com_rate,2) ?? 0 }}</span>
                          <br>
                          <label for="" class="control-label">Total to Release :</label>
                          <span class="" id="m_ttl_rls">{{ number_format((float) $ttl_rls[0]->ttl_release_mgr, 2) ?? 0  }}</span>
                          {{--<span class="">{{ number_format($u_rem_rcvd,2) ?? 0 }}</span>--}}
                        </div>
                        {{--<div class="form-group">
                            <label for="" class="control-label">Notes:</label>
                            <span> {{$header[0]->pay_mode}}</span>
                        </div>--}}
                        {{--<div class="form-group">
                          <label for="" class="control-label">Received Amount:</label>
                          <span> {{number_format($header[0]->amount, 2)}}</span>
                        </div>--}}

                        <div class="form-group">
                            <input type="hidden" name="max_release" id="max_release" class="form-control" value="{{$max_rls, 2 ?? 0 }}" style="text-align: right" readonly>
                            <input type="hidden" name="ttl_receive" id="ttl_receive" class="form-control" value="{{$header[0]->amount ?? 0 }}" style="text-align: right" readonly> {{--$ttl_rcvd[0]->ttl_receive--}}
                        </div>
                        <div class="form-group" style="height: 50px;">
                        {{--<hr>
                            <div>
                                <label for="" class="control-label">Agent C/A Balance:</label>
                                <span> {{number_format($ca_bal[0]->ttl_cash_adv-$ca_bal[0]->ttl_cash_payment, 2)}}</span>
                                <br>
                                <label for="" class="control-label">Manager C/A Balance:</label>
                                <span> {{number_format($ca_bal2[0]->ttl_cash_adv-$ca_bal2[0]->ttl_cash_payment, 2)}}</span>
                            </div>--}}
                        </div>

                    </div>

                    <div class="col-md-3">
                        <div class="form-group has-feedback @if($errors->has('agent_pcg_rate')) has-error @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="{{$release[0]->agent_pcg_rate}}" style="text-align: right" step="1" min="1">
                            <span class="glyphicon glyphicon-zoom-out form-control-feedback"></span>
                            @if($errors->has('agent_pcg_rate')) <span class="help-block">{{$errors->first('agent_pcg_rate')}}</span> @endif
                        </div>
                    </div>

                    <div class="col-md-3">
                        <div class="form-group has-feedback @if($errors->has('agent_com_rate')) has-error @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="{{ number_format((float) $release[0]->agent_com_rate, 2)}}" style="text-align: right" readonly>
                            <span class="glyphicon glyphicon-zoom-out form-control-feedback"></span>
                            @if($errors->has('agent_com_rate')) <span class="help-block">{{$errors->first('agent_com_rate')}}</span> @endif
                        </div>
                    </div>

                    <div class="col-md-3">
                        <div class="form-group has-feedback @if($errors->has('um_pcg_rate')) has-error @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="{{$release[0]->um_pcg_rate}}" style="text-align: right" step="1" min="1">
                            <span class="glyphicon glyphicon-zoom-out form-control-feedback"></span>
                            @if($errors->has('um_pcg_rate')) <span class="help-block">{{$errors->first('um_pcg_rate')}}</span> @endif
                        </div>
                    </div>

                    <div class="col-md-3">
                        <div class="form-group has-feedback @if($errors->has('um_com_rate')) has-error @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="{{ number_format((float) $release[0]->um_com_rate, 2)}}" style="text-align: right" readonly>
                            <span class="glyphicon glyphicon-zoom-out form-control-feedback"></span>
                            @if($errors->has('um_com_rate')) <span class="help-block">{{$errors->first('um_com_rate')}}</span> @endif
                        </div>
                    </div>

                    <div class="col-md-3">
                        <div class="form-group has-feedback @if($errors->has('broker_pcg_rate')) has-error @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="{{$release[0]->broker_pcg_rate}}" style="text-align: right" step="1" min="1">
                            <span class="glyphicon glyphicon-zoom-out form-control-feedback"></span>
                            @if($errors->has('broker_pcg_rate')) <span class="help-block">{{$errors->first('broker_pcg_rate')}}</span> @endif
                        </div>
                    </div>

                    <div class="col-md-3">
                        <div class="form-group has-feedback @if($errors->has('broker_com_rate')) has-error @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="{{ number_format((float) $release[0]->broker_com_rate, 2)}}" style="text-align: right" readonly>
                            <span class="glyphicon glyphicon-zoom-out form-control-feedback"></span>
                            @if($errors->has('broker_com_rate')) <span class="help-block">{{$errors->first('broker_com_rate')}}</span> @endif
                        </div>
                    </div>

                    <div class="col-md-3">
                        <div class="form-group has-feedback @if($errors->has('ref_pcg_rate')) has-error @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="{{$release[0]->ref_pcg_rate}}" style="text-align: right" step="0.1" min="0.1">
                            <span class="glyphicon glyphicon-zoom-out form-control-feedback"></span>
                            @if($errors->has('ref_pcg_rate')) <span class="help-block">{{$errors->first('ref_pcg_rate')}}</span> @endif
                        </div>
                    </div>

                    <div class="col-md-3">
                        <div class="form-group has-feedback @if($errors->has('ref_com_rate')) has-error @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="{{ number_format((float) $release[0]->ref_com_rate, 2)}}" style="text-align: right" readonly>
                            <span class="glyphicon glyphicon-zoom-out form-control-feedback"></span>
                            @if($errors->has('ref_com_rate')) <span class="help-block">{{$errors->first('ref_com_rate')}}</span> @endif
                        </div>
                    </div>

                    <div class="col-md-3">
                        <div class="form-group has-feedback @if($errors->has('ca_amount')) has-error @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: {{number_format($ca_bal,2) ?? 0 }}</span>
                            </div>
                            <input type="text" name="ca_amount" id="ca_amount" class="form-control" value="{{ number_format((float) $release[0]->payment_adv, 2)}}" style="text-align: right">
                            <span class="glyphicon glyphicon-zoom-out form-control-feedback"></span>
                            @if($errors->has('ca_amount')) <span class="help-block">{{$errors->first('ca_amount')}}</span> @endif
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group has-feedback @if($errors->has('um_ca_amount')) has-error @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: {{number_format($um_bal,2) ?? 0 }}</span>
                            </div>
                            <input type="text" name="um_ca_amount" id="um_ca_amount" class="form-control" value="{{ number_format((float) $release[0]->um_payment_adv, 2)}}" style="text-align: right">
                            <span class="glyphicon glyphicon-zoom-out form-control-feedback"></span>
                            @if($errors->has('um_ca_amount')) <span class="help-block">{{$errors->first('um_ca_amount')}}</span> @endif
                        </div>
                    </div>

                    {{--<div class="col-md-12">
                        <div class="form-group has-feedback @if($errors->has('remarks')) has-error @endif">
                            <textarea id="remarks" name="remarks" class="form-control" rows="2" style="resize: none;" placeholder="Add notes/remarks"> {{$release[0]->remarks ?? ''}}</textarea>
                            @if($errors->has('remarks')) <span class="help-block">{{$errors->first('remarks')}}</span> @endif
                        </div>
                    </div>--}}


                    {{--<div class="col-md-12">
                        <button id="btnOk" name="btnOk" type="submit" class="btn btn-success btn-s"><i class="fa fa-check"></i> Update </button>
                        <a href="{{ url()->previous() }}" class="btn btn-primary"><i class="fa fa-arrow-left"></i> Cancel</a>
                    </div>--}}


                <div class="form-group" style="float:left;padding: 10px;padding-top:20px;">
                    <button id="btnOk" name="btnOk" type="submit" class="btn btn-success btn-s"><i class="fa fa-check"></i> Update </button>
                    <a href="{{ url()->previous() }}" class="btn btn-primary"><i class="fa fa-arrow-left"></i> Cancel</a>
                </div>
                {{csrf_field()}}
            </form>

        </section>
        @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> {{Session('info')}}</div>
                </div>
            </div>
        @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>
@stop

@section('script')

    {{--<script src="https://gitcdn.github.io/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js"></script>--}}
    <script type="text/javascript" src="{{ asset('js/numeric/jquery.masknumber.js') }}" ></script>
    <script type="text/javascript" src="{{ 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();

            //var a_rls   = $('#a_ttl_rls').maskNumber();
            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');
                console.log('check_rcvd: Ok');
            } else {
                //alert('Warning: You have an Unmatched commission.');
                //alert('Maintenance mode.');
                $('#btnOk').attr("disabled", false);
                console.log('check_rcvd: X');
            }

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

            $('#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();
            //var ff = $('#release_amount').maskNumber();
            //console.log(ff);


        });

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

        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);
            //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));
        }

        $('.comm_rate').on('keyup keydown change', function () {

            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));

            var sum = 0;
            //var nsp = $('#net_selling_price').val();
            //var rate = $(this).val();
            var y = $('#ttl_receive').val();
            var num = y; //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');
                //$('#modBody').css("background-color","#BB2E0F");
                //notifPopup('Notification', 'Total sharing % came short.');
            } else if (sum > 100) {
                console.log('sobra');
                $('#btnOk').attr("disabled", true);
                //alert('Total sharing % exceeded.');
                //$('a[href="#finish"]').css('display', 'none');
                //$('#modBody').css("background-color","#BB2E0F");
                //notifPopup('Notification', 'Total sharing % exceeded.');
            } else {
                console.log('sakto. daw coke');
                $('#btnOk').attr("disabled", false);
                //$('a[href="#finish"]').css('display', 'block');
            }

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

            check_rcvd(a_rcv, a_rcvd2, m_rcv, m_rcvd2);

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

            /*if (m_rcvd >= m_rls) {
                //alert('Okeyy keeyu');
            } else {
                alert('Warning: You have an Overpayment manager commission.');
            }*/
            /*if (a_rcv > a_rcvd2) {
                //alert('You have an Overpayment agent commission.');
                console.log('overpayment agent');
                console.log('entry: '+a_rcv+' '+a_rcvd2);
                $('#btnOk').attr("disabled", true);
            } else if (m_rcv > m_rcvd) {
                //alert('You have an Overpayment manager commission.');
                console.log('overpayment manager');
                console.log('entry: '+m_rcv+' '+m_rcvd2);
                $('#btnOk').attr("disabled", true);
            } else {
                $('#btnOk').attr("disabled", false);
            }*/

            //comm = parseFloat(nsp) * parseFloat(rate);

            //$('#ttl_basic_pay').val(sum);
            //$('#gross_pay').val(gross_pay);
            //get_totals();

        });

        function check_rcvd(a_rcv, a_rcvd2, m_rcv, m_rcvd2) {
            if (a_rcv > a_rcvd2) {
                //alert('You have an Overpayment agent commission.');
                console.log('unmatched payment agent');
                console.log('entry: '+a_rcv+' '+a_rcvd2);
                //commented out
                //$('#btnOk').attr("disabled", true);
            } else if (m_rcv > m_rcvd2) {
                //alert('You have an Overpayment manager commission.');
                console.log('unmatched payment manager');
                console.log('entry: '+m_rcv+' '+m_rcvd2);
                //commented out
                //$('#btnOk').attr("disabled", true);
            } else {
                $('#btnOk').attr("disabled", false);
            }
        }

        function realParseFloat(s)
        {
            s = s.replace(/[^\d,.-]/g, ''); // strip everything except numbers, dots, commas and negative sign
            if (navigator.language.substring(0, 2) !== "de" && /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(s)) // if not in German locale and matches #,###.######
            {
                s = s.replace(/,/g, ''); // strip out commas
                return parseFloat(s); // convert to number
            }
            else if (/^-?(?:\d+|\d{1,3}(?:\.\d{3})+)(?:,\d+)?$/.test(s)) // either in German locale or not match #,###.###### and now matches #.###,########
            {
                s = s.replace(/\./g, ''); // strip out dots
                s = s.replace(/,/g, '.'); // replace comma with dot
                return parseFloat(s);
            }
            else // try #,###.###### anyway
            {
                s = s.replace(/,/g, ''); // strip out commas
                return parseFloat(s); // convert to number
            }
        }

        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>
@stop