<?php if ($is_edit && $check_lock != '') { ?>
    <div class="alert alert-danger" role="alert">
        <button class="close" data-dismiss="alert"></button>
        <strong>Danger: </strong><?php echo $check_lock ?>
    </div>
    <?php
    return;
}
?>
<style type="text/css">
    .DropDownForForms > .dropdown-menu{
        width:100%;
        height:200px;
        max-height:200px;
        overflow-y:scroll;
    }
    .DropDownForForms > .dropdown-menu{
        height:auto;
        cursor: pointer;
        overflow-y:auto;
    }
    .DropDownForForms > button{
        position:relative;
        z-index:999;
    }
    .DropDownForForms > button > input{
        position: absolute;
        left:5;
        top: 0;
        width:90%;
        cursor: pointer;
        -webkit-user-select:none;
        -moz-user-select:none;
        user-select:none;
        padding: 0;
        border: 0;
        background: transparent;
    }
    .Disabled{
        cursor:default;
        opacity:0.5;
    }
    #main-div-rows{
        background: #4c0870;
        padding: 10px;
        border-radius: 6px;
        margin-bottom: 10px;
    }
    .row [class*='col-']:first-child {
        padding-left: 10px !important;}
    </style>
    <form method="POST" action="" onsubmit="return false" id="bill_form">
        <div class="container-fluid container-fixed-lg">
            <div class="panel panel-default">
                <div class="panel-body form-group">
                    <?php echo Forms::ModalLoader('bill_loader') ?>
                    <div id="bill_alert"></div>
                    <div class="invoice">
                        <div class="container-sm-height">
                            <input type="hidden" id="bill_id" name="bill_id" value="<?php echo $is_edit ? $edit_arr['main']['bill_id'] : 0; ?>" />
                            <input type="hidden" id="is_used" name="is_used" value="<?php echo $is_edit ? $edit_arr['is_used'] : 0; ?>" />
                            <div class="row-sm-height">
                                <div class="col-md-7 col-sm-height sm-no-padding">
                                    <div>
                                        <div class="input-group  col-md-6 center-margin pull-left" >
                                            <div class="sm-m-t-20">
                                                <h2 class="font-montserrat all-caps hint-text"><?php print CommonLanguage::getMessage('schedule_bills'); ?></h2>
                                            </div>
                                        </div>
                                        <div class="clearfix"></div>
                                    </div>
                                    <div class=" input-group  col-md-4 center-margin pull-left " >
                                        <label><?php print CommonLanguage::getMessage('choose_customer'); ?><span class="red-star">*</span><span class="fa fa-plus-circle m-l-5 openFrame" data-src="<?php echo "Inventory/CustomerList?is_pop=1" ?>" style="color:#4c0870;cursor:pointer"></span>
                                            </label>
                                            <input class="customer_id full-width" id="customer_id" name="current_customer_id" type="hidden" value="<?php echo $is_edit ? $edit_arr['main']['customer_id'] : (isset($get_parameters['customer_id']) ? $get_parameters['customer_id'] : ''); ?>" data-select-name='<?php echo $is_edit ? $edit_arr['main']['Customer_Name'] : (isset($get_parameters['customer_name']) ? $get_parameters['customer_name'] : ''); ?>' <?php echo ($is_edit && $edit_arr['is_used'] > 0) ? 'disabled' : ''; ?> required />
                                        </div> 
                                        <div class="col-md-4 ">
                                            <label><?php echo CommonLanguage::getMessage('account') ?><i class="fa fa-info-circle" title="<?php echo CommonLanguage::getMessage('bill_acc_hint') ?>"></i>
                                                <span class="fa fa-plus-circle m-l-5 openFrame" data-src="<?php echo "ACC/viewlibrary/1?is_pop=1" ?>" style="color:#4c0870;cursor:pointer"></span>
                                            </label>

                                            <input class="account_id full-width" id="account_id" name="current_account_id" type="hidden" value="<?php echo $is_edit ? $edit_arr['main']['account_id'] : ""; ?>" data-select-name="<?php echo $is_edit ? $edit_arr['main']['Account_Name'] : ""; ?>" <?php echo ($is_edit && $edit_arr['is_used'] > 0) ? 'disabled' : ''; ?>  />
                                        </div>
                                        <div class="col-md-4 ">
                                            <label><?php echo CommonLanguage::getMessage('pay_for') . ': ' ?><span class="red-star">*</span></label>
                                            <input class="pay_for full-width form-control" id="pay_for" name="pay_for" type="text" value="<?php echo $is_edit ? $edit_arr['main']['pay_for'] : ''; ?>" <?php echo ($is_edit && $edit_arr['is_used'] > 0) ? 'readonly' : ''; ?> required/>
                                        </div>
                                        <div class="clearfix"></div>
                                        <br>
                                        <div class="row">
                                            <div class="col-md-3">
                                                <label><?php echo CommonLanguage::getMessage('first_bill') ?></label>
                                                <input type="number" step="any" min="0" id="first_bill" value="<?php echo $is_edit ? $edit_arr['main']['first_bill'] : ''; ?>" name="first_bill" <?php echo ($is_edit && $edit_arr['is_used'] > 0) ? 'readonly' : ''; ?> class="form-control"/>
                                            </div>
                                            <div class="col-md-3">
                                                <label><?php echo CommonLanguage::getMessage('bill_amount') ?><span class="red-star">*</span></label>
                                                <input type="number" step="any" min="0" value="<?php echo $is_edit ? $edit_arr['main']['bill_amount'] : ''; ?>" id="bill_amount" <?php echo ($is_edit && $edit_arr['is_used'] > 0) ? 'readonly' : ''; ?> name="bill_amount" class="form-control" required/>
                                            </div>

                                            <div class="col-md-3">
                                                <label><?php echo CommonLanguage::getMessage('nb_bills') ?><span class="red-star">*</span></label>
                                                <input type="number" step="any" min="1" id="nb_of_bills" name="nb_of_bills"  class="form-control" <?php echo ($is_edit && $edit_arr['is_used'] > 0) ? 'readonly' : ''; ?> value="<?php echo $is_edit ? $edit_arr['main']['nb_of_bills'] : 1; ?>" required/>
                                            </div>
                                            <div class="col-md-3">
                                                <label><?php echo CommonLanguage::getMessage('last_bill') ?></label>
                                                <input type="number" step="any" min="0" id="last_bill" value="<?php echo $is_edit ? $edit_arr['main']['last_bill'] : ''; ?>" <?php echo ($is_edit && $edit_arr['is_used'] > 0) ? 'readonly' : ''; ?> name="last_bill" readonly  class="form-control"/>
                                            </div>
                                        </div>
                                        <br>
                                        <div class="row">
                                            <div class="col-md-12">
                                                <label><?php echo CommonLanguage::getMessage('bill_desc') . ': ' ?><span class="red-star">*</span></label>
                                                <textarea id="bill_description" name="bill_description" class="form-control NoteTextArea"   aria-invalid="false" required><?php echo $is_edit ? $edit_arr['main']['bill_description'] : ''; ?></textarea>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-3 col-sm-height col-bottom sm-no-padding sm-p-b-20" style="vertical-align: top;">
                                    <div>
                                        <label><?php echo CommonLanguage::getMessage('total') ?><span class="red-star">*</span></label>
                                        <input class="form-control" id="main_total" value="<?php echo $is_edit ? $edit_arr['main']['main_total'] : (isset($get_parameters['amount']) ? $get_parameters['amount'] : ''); ?>" <?php echo ($is_edit && $edit_arr['is_used'] > 0) ? 'readonly' : ''; ?> name="main_total" type="number" step="any" min="0" required/>
                                    </div>
                                    <div id="CurrencyIDContainer" class=" m-t-10" >
                                        <label><?php echo CommonLanguage::getMessage('currency') ?><span class="red-star">*</span></label>
                                        <div class="form-group form-material floating ">
                                            <div class="input-group-btn DropDownForForms Closable  " id="CurrencyDropDown">
                                                <button type="button" class="btn btn-complete btn-block dropdown-toggle" data-toggle="dropdown" <?php echo ($is_edit && $edit_arr['is_used'] > 0) ? 'disabled' : ''; ?> aria-expanded="false">
                                                    <span><?php echo $is_edit ? ($edit_arr['main']['Currency_Name'] . ' - ' . $edit_arr['main']['Currency_Symbol']) : (isset($get_parameters['currency_name']) ? $get_parameters['currency_name'] : $defCurrencyName . ' - ' . $defCurrencySymbol); ?></span>
                                                    <input type="hidden"  id="currency_id" value="<?php echo $is_edit ? $edit_arr['main']['currency_id'] : (isset($get_parameters['currency_id']) ? $get_parameters['currency_id'] : $default_currency_id) ?>" class="form-control ignore" <?php echo ($is_edit && $edit_arr['is_used'] > 0) ? 'disabled' : ''; ?> onkeyup="return false" onkeydown="return false" required/>
                                                </button>
                                                <ul class="dropdown-menu">
                                                    <?php echo $CurList ?>
                                                    <li class="divider"></li>
                                                    <li><a class="LoadMore">More..</a></li>
                                                </ul>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="m-b-10" >
                                        <label><?php print CommonLanguage::getMessage('payment_period'); ?><span class="red-star">*</span></label>
                                        <div style="display: flex;">
                                            <input id="payment_period" name="payment_period" value="<?php echo $is_edit ? $edit_arr['main']['payment_period'] : 1; ?>" <?php echo ($is_edit && $edit_arr['is_used'] > 0) ? 'readonly' : ''; ?> class="form-control"  type="number" step="any" min="0" required>
                                            <input id="period" name="period"  class="form-control" type="text" readonly value="<?php echo CommonLanguage::getMessage('month') ?>">
                                        </div>
                                    </div>
                                    <div class="form-group form-group-default input-group full-width">
                                        <label class="fade"><?php print CommonLanguage::getMessage('Date'); ?><span class="red-star">*</span></label>
                                        <input id="bill_date" name="bill_date"  class="form-control" value="<?php echo $is_edit ? $edit_arr['main']['bill_date'] : date('Y-m-d'); ?>" <?php echo ($is_edit && $edit_arr['is_used'] > 0) ? 'readonly' : ''; ?> required>
                                    </div>
                                    <div class="form-group form-group-default input-group full-width">
                                        <label class="fade"><?php print CommonLanguage::getMessage('start_date'); ?><span class="red-star">*</span></label>
                                        <input id="start_date"  <?php echo ($is_edit && $edit_arr['is_used'] > 0) ? 'readonly' : ''; ?> name="start_date" value="<?php echo $is_edit ? $edit_arr['main']['start_date'] : ''; ?>" class="form-control" <?php echo ($is_edit && $edit_arr['is_used'] > 0) ? 'readonly' : ''; ?>  required >
                                    </div>

                                    <div class=" full-width <?php echo ($is_edit && $edit_arr['is_used'] > 0) ? 'hidden' : ''; ?>">
                                        <button type="button" class="btn btn-complete full-width" id="generate_bills"><?php echo CommonLanguage::getMessage('generate') ?></button>                                            </div>
                                </div>
                            </div>
                        </div>
                        <div class="row m-t-10 hidden" id="main-div-rows">
                            <div class="col-md-12 title-row m-b-10 form-group"  >
                                <div class="row">
                                    <div class="col-xs-3"><label>#</label></div>
                                    <div class="col-xs-4 "><label><?php echo CommonLanguage::getMessage('due_date') ?></label></div>
                                    <div class="col-xs-4"><label><?php echo CommonLanguage::getMessage('amount') ?></label></div>
                                </div>
                            </div>
                        </div>
                        <div class="row hidden" id="row-totals">
                            <div class="col-md-12 total-rows form-group">
                                <div class="col-xs-3"></div>
                                <div class="col-xs-4 "><label><?php echo CommonLanguage::getMessage('schedule_total') . ': ' ?><span id="main_schedule_total"></span></label></div>
                                <div class="col-xs-4"><label><?php echo CommonLanguage::getMessage('bill_total') . ': ' ?><span id="bill_total"></span></label></div>
                            </div>
                        </div>
                        <br>
                        <div class="container-sm-height" >
                            <button class=" btn btn-complete btn-cons m-b-10 pull-right " name="submit_bill" id="submit_bill" style="padding-top: 10px;top: 20px;min-width:253px;" type="button"><i class="pg-form"></i> <span class="bold"><?php print CommonLanguage::getMessage('Save'); ?></span></button>
                            <button class=" btn btn-danger btn-cons m-b-10 pull-right disabled" id="PrintBtn" style="padding-top: 10px;top: 20px;min-width:253px;" type="button"><i class="fa fa-print"></i> <span class="bold"><?php print CommonLanguage::getMessage('print'); ?></span></button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <div class="col-md-12 row-temp m-b-10 form-group hidden"  >
        <div class="row">
            <div class="col-xs-3">
                <strong><span class="bill-span" style="color:white;"></span></strong>
            </div>
            <div class="col-xs-4 ">
                <input type="text" class="form-control bill-date  "  >
            </div>
            <div class="col-xs-4">
                <input type="text" class="form-control bill-amount digits "  >
            </div>
            <div class="col-md-1">
                <span class="fa fa-plus-circle" style="color:#4c0870;margin-top:15px;margin-right:25px"></span>
                    <span class="fa fa-minus-circle" style="color:#ed9421;margin-top:15px;"></span>
            </div>
        </div>
    </div>
    <script>
        var Required_Fields = <?php echo json_encode(CommonLanguage::getMessage('Required_Fields')); ?>,
                is_actual = <?php echo json_encode($is_actual); ?>,
                is_edit = <?php echo json_encode($is_edit); ?>,
                current_bill_id;
        Failed_Submit = <?php echo json_encode(CommonLanguage::getMessage('Failed_Submit')); ?>,
                check_totals = <?php echo json_encode(CommonLanguage::getMessage('check_totals')); ?>,
                Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>,
                CurrencySymbol = '', local_currency = <?php echo json_encode(smc_session()['local_currency']); ?>,
                schedule_details = <?php echo json_encode($edit_arr['details']); ?>;
        $(function () {
            build_select2($("#customer_id"), ("xhr/customerSearch"))
            build_select2($("#account_id"), ("xhr/accIDSearch?type=Payment"));
            apply_autocomplete($("#pay_for"), 'xhr/payForScheduleBillSearch', function (event, ui, who) {
                who.val(ui.item.text);
            });
            $('#main_total').on('input', function () {
                $("#nb_of_bills").trigger('input');
            });
            $("#nb_of_bills").on("input", function () {
                var first_bill = parseFloat($("#first_bill").val() || 0),
                        main_total = parseFloat($("#main_total").val() || 0) - first_bill,
                        nb_of_bills = parseFloat($(this).val() || 0);
                var bill_amount = Math.ceil(main_total / nb_of_bills),
                        last_bill = main_total - (bill_amount * (nb_of_bills - 1));
                $("#bill_amount").val(bill_amount)
                if (nb_of_bills > 0 && last_bill != bill_amount)
                    $("#last_bill").val(last_bill)
                else
                    $("#last_bill").val('0')
            });
            $("#first_bill").on("keyup", function () {
                var main_total = parseFloat($("#main_total").val() || 0),
                        bill_amount = parseFloat($("#bill_amount").val() || 0),
                        first_bill = parseFloat($(this).val() || 0);
                var bills_total = main_total - first_bill;
                if (bill_amount > bills_total) {
                    bill_amount = bills_total;
                    $("#bill_amount").val(bill_amount);
                }
                var nb_of_bills = Math.floor((main_total - first_bill) / bill_amount) + 1;
                $("#nb_of_bills").val(nb_of_bills);
                var last_bill = bills_total - (bill_amount * (nb_of_bills - 1));
                if (nb_of_bills <= 0 || last_bill == bill_amount)
                    last_bill = 0;
                $("#last_bill").val(last_bill);
                if (last_bill == 0)
                    $("#nb_of_bills").val(nb_of_bills - 1);
            });
            $("#bill_amount").on("keyup", function () {
                var main_total = parseFloat($("#main_total").val() || 0),
                        bill_amount = parseFloat($(this).val() || 0),
                        first_bill = parseFloat($("#first_bill").val() || 0);
                var bills_total = main_total - first_bill;
                if (bill_amount > bills_total) {
                    bill_amount = bills_total;
                    $("#bill_amount").val(bill_amount);
                }
                var nb_of_bills = Math.floor(bills_total / bill_amount) + 1;
                $("#nb_of_bills").val(nb_of_bills)
                var last_bill = bills_total - (bill_amount * (nb_of_bills - 1));
                if (nb_of_bills <= 0 || last_bill == bill_amount)
                    last_bill = 0;
                $("#last_bill").val(last_bill);
                if (last_bill == 0)
                    $("#nb_of_bills").val(nb_of_bills - 1);
                updateBillTotal();
            });
            //

            $("#generate_bills").on("click", function () {
                if (!$("#start_date").val() || !$('#bill_amount').val() || !$('#main_total').val()) {
                    AddAlert("#bill_alert", '<?php echo CommonLanguage::getMessage('choose_generate_bill_fields'); ?>', "danger");
                    $(document.body).animate({scrollTop: 0}, 800)
                    return;
                }
                if (parseFloat($('#bill_amount').val()) < 0 || parseFloat($('#nb_of_bills').val()) < 0 || parseFloat($('#main_total').val()) < 0 || parseFloat($('#first_bill').val()) < 0 || parseFloat($('#payment_period').val()) < 0) {
                    AddAlert("#bill_alert", '<?php echo CommonLanguage::getMessage('negative_value'); ?>', "danger");
                    $(document.body).animate({scrollTop: 0}, 800)
                    return;
                }

                var bill_amount = parseFloat($("#bill_amount").val() || 0),
                        first_bill = parseFloat($("#first_bill").val() || 0),
                        last_bill = parseFloat($("#last_bill").val() || 0),
                        bill_nb = parseFloat($("#nb_of_bills").val() || 0) - (last_bill > 0 ? 1 : 0),
                        start_date = $("#start_date").val(),
                        pay_period = parseFloat($("#payment_period").val() || 0),
                        temp = $(".row-temp").clone();
                $('#main-div-rows .AddRow').remove();
                var crnt_bill = 0;
                var bill_count = bill_nb + (last_bill > 0 ? 1 : 0);
                //first bill not consider from the bills it only indicator for history, since it is already paid using normal receipt
                //            if (first_bill > 0) {
                //                temp.removeClass("row-temp hidden").addClass("AddRow");
                //                temp.appendTo($("#main-div-rows"));
                //                crnt_bill++;
                //                temp.attr('data-number', crnt_bill);
                //                $("span.bill-span", temp).text(crnt_bill + '/' + bill_count);
                //                $("input.bill-date", temp).val(start_date).datepicker({autoclose: true});
                //                $("input.bill-amount", temp).val($("#first_bill").val());
                //                start_date = moment(start_date).add(pay_period, 'month').format('YYYY-MM-DD');
                //            }
                for (var i = 0; i < bill_nb; i++) {
                    var temp = $(".row-temp").clone();
                    temp.removeClass("row-temp hidden").addClass("AddRow")
                    temp.appendTo($("#main-div-rows"))
                    crnt_bill++;
                    temp.attr('data-number', crnt_bill);
                    $("span.bill-span", temp).text(crnt_bill + '/' + bill_count)
                    $("input.bill-date", temp).val(start_date).datepicker({autoclose: true})
                    $("input.bill-amount", temp).val(bill_amount)
                    start_date = moment(start_date).add(pay_period, 'month').format('YYYY-MM-DD');
                }
                if (last_bill > 0) {
                    var temp = $(".row-temp").clone();
                    temp.removeClass("row-temp hidden").addClass("AddRow")
                    temp.appendTo($("#main-div-rows"));
                    crnt_bill++;
                    $("span.bill-span", temp).text(crnt_bill + '/' + bill_count);
                    temp.attr('data-number', crnt_bill);
                    $("input.bill-date", temp).val(start_date).datepicker({autoclose: true})
                    $("input.bill-amount", temp).val(last_bill)

                }
                $("#main-div-rows,#row-totals").removeClass("hidden");
                updateBillTotal();

            });
            $('#main-div-rows').on('input', '.bill-amount', function () {
                updateBillTotal();
            });
            $("#main-div-rows").on("click", " .fa-minus-circle", function (e) {
                if ($(".AddRow").length <= 1)
                    return;
                $(this).closest(".AddRow").empty().remove();
                $(".AddRow").each(function (e) {
                    $(this).attr('data-number', (e + 1));
                    $("span.bill-span", $(this)).text((e + 1) + '/' + $(".AddRow").length);
                });
                updateBillTotal();
            });
            $("#main-div-rows").on("click", " .fa-plus-circle", function (e) {
                var Parent = $(this).parents(".AddRow"), NewItem = Parent.clone().appendTo($("#main-div-rows"));
                NewItem.find("input").val('');
                NewItem.attr("data-bill-detail-id", 0);
                NewItem.attr("data-bill-detail-is-paid", 0);
                $(".fa-minus-circle", NewItem).css("display", "inline")
                NewItem.find("input.bill-date").val('').datepicker({autoclose: true});
                $("input.bill-date,input.bill-amount", NewItem).prop('readonly', false)
                $(".AddRow").each(function (e) {
                    $("span.bill-span", $(this)).text((e + 1) + '/' + $(".AddRow").length)
                    $(this).attr('data-number', (e + 1))
                });
            });
            $("#submit_bill").on("click", function () {
                if (!$("#bill_form").valid()) {
                    $(document.body).animate({scrollTop: 0}, 800)
                    return;
                }
                var total = 0, bill_details = [], bill_detail_is_paid_arr = [], bill_detail_ids = [];
                $(".AddRow").each(function (e) {
                    var row_amount = parseFloat($("input.bill-amount", $(this)).val()) || 0;
                    var row_due_date = $("input.bill-date", $(this)).val();
                    if (row_amount > 0 && row_due_date != '') {
                        total += row_amount
                        bill_details.push({bill_detail_nb: $(this).attr('data-number'),
                            bill_detail_id: $(this).attr('data-bill-detail-id'),
                            bill_detail_amount: row_amount,
                            bill_detail_due_date: row_due_date,
                            bill_detail_is_paid: $(this).attr('data-bill-detail-is-paid')
                        });
                        if ($(this).attr('data-bill-detail-is-paid') == 1)
                            bill_detail_is_paid_arr.push($(this).attr('data-bill-detail-id'));
                        if ($(this).attr('data-bill-detail-id') != 0 && $(this).attr('data-bill-detail-is-paid') == 0)
                            bill_detail_ids.push($(this).attr('data-bill-detail-id'))
                    }
                });
                var main_total = parseFloat($("#main_total").val()) - parseFloat($("#first_bill").val() || 0);
                if (total != main_total) {
                    AddAlert("#bill_alert", check_totals, "danger");
                    $(document.body).animate({scrollTop: 0}, 800);
                    return;
                }
                $("#bill_loader").removeClass('hidden');
                var Data = new FormData(document.getElementById("bill_form"));
                Data.append('bill_details', JSON.stringify(bill_details));
                Data.append('bill_detail_ids', JSON.stringify(bill_detail_ids));
                Data.append('bill_detail_is_paid_arr', JSON.stringify(bill_detail_is_paid_arr));
                Data.append('is_actual', is_actual)
                Data.append('currency_id', $('#currency_id').val())
                Data.append('customer_id', $('#customer_id').val())
                Data.append('account_id', $('#account_id').val())
                Data.append('is_used', $('#is_used').val())
                $.ajax({
                    url: "ACCAjax/submitScheduleBills",
                    method: "POST",
                    data: Data,
                    processData: false,
                    contentType: false,
                    dataType: "JSON",
                    complete: function (jqXHR, status) {
                        if (status == 'success') {
                            if (jqXHR.responseJSON[1] == "success") {
                                $(document.body).animate({scrollTop: 0}, 800);
                                $('#submit_bill').attr('disabled', true);
                                current_bill_id = jqXHR.responseJSON[2];
                                $('#PrintBtn').removeClass('disabled');
                            }
                            AddAlert("#bill_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);

                        }
                        $("#bill_loader").addClass('hidden');
                    }
                });
            });
            if ($('#is_used').val() == 0)
                $("#start_date").datepicker({autoclose: true});
            $("#bill_date").datepicker({autoclose: true});
            $("#CurrencyDropDown li").on("click", function () {
                CurrencySymbol = $(this).attr("mysymbol")
                $(".CurrencyIndicator").html($(this).attr("mysymbol"))
                if ($(this).attr("value") == local_currency) {
                    $("#current_rate").val(1).attr("disabled", "disabled")
                } else
                    $("#current_rate").val("").removeAttr("disabled")
            });
            $("#PrintBtn").on("click", function () {
                var myurl = "ACC/view_schedule_bills/" + current_bill_id;
                printExternal(myurl);
            });
            if (is_edit && schedule_details.length > 0) {
                current_bill_id = $('#bill_id').val()
                $('#main-div-rows,#row-totals').removeClass('hidden');
                var bill_nb = schedule_details.length;  //parseFloat($("#nb_of_bills").val() || 0);
                $.each(schedule_details, function (index, value) {
                    var temp = $(".row-temp").clone();
                    temp.removeClass("row-temp hidden").addClass("AddRow")
                    temp.appendTo($("#main-div-rows"))
                    temp.attr('data-number', schedule_details[index]['bill_detail_nb']);
                    temp.attr('data-bill-detail-id', schedule_details[index]['bill_detail_id']);
                    $("span.bill-span", temp).text(schedule_details[index]['bill_detail_nb'] + '/' + bill_nb);
                    $("input.bill-date", temp).val(schedule_details[index]['bill_detail_due_date'])
                    $("input.bill-amount", temp).val(schedule_details[index]['bill_detail_amount']);
                    if (schedule_details[index]['Transaction_ID']) {
                        temp.attr('data-bill-detail-is-paid', 1)
                        $("input.bill-date,input.bill-amount", temp).attr('readonly', true)
                        $(".fa-minus-circle", temp).css("display", "none")
                    } else {
                        temp.attr('data-bill-detail-is-paid', 0)
                        $("input.bill-date", temp).datepicker({autoclose: true});
                        $("input.bill-date,input.bill-amount", temp).attr('readonly', false);
                        $(".fa-minus-circle", temp).css("display", "inline")
                    }
                });
                updateBillTotal();
            }
        });
        function updateBillTotal() {
            var bills_total = 0;
            $('#main_schedule_total').html(AddCommas(parseFloat($('#main_total').val()) - parseFloat($('#first_bill').val())));
            $('.AddRow').each(function () {
                bills_total += parseFloat($('.bill-amount', $(this)).val() || 0);
            });
            $('#bill_total').html(AddCommas(bills_total));
        }
    </script>
