function check_change_rate_option(duration_index,option_change,index)
{
    if(option_change['from']!=undefined||option_change['to']!=undefined){
        if(option_change['from'].length<=0||option_change['to'].length<=0||index>=option_change['from'].length||index>=option_change['to'].length) 
        {
            return false;
        }
        else{
            if(option_change['to'][index]<duration_index||option_change['from'][index]>Number(duration_index)) 
            {
                index +=1;
                return check_change_rate_option(duration_index,option_change,index);
            }
            if(Number(option_change['from'][index])<=Number(duration_index)&&Number(option_change['to'][index])>=Number(duration_index)) 
            {
                return Number(option_change['condition'][index]/100);
            }
            else{
                index +=1;
                return check_change_rate_option(duration_index,option_change,index);
            }
            
        }
    }
    else return false;
    
}
function loan_generator(amount,amount_os,type, duration, interest, start_date, disbursement,option_change_rate=[],check_rate_condition) {
    var row = '';
    if (interest == "") {
        interest = 0;
    }
    var rate = interest / (100);
    var start_on = disbursement;
    var end_on = start_date;

    if (type == 2 || (type == 1 && interest <= 0 && option_change_rate['from'].length<=0&&option_change_rate['to'].length<=0)) {
        var principle = amount / duration;
        var remainingBalance = amount_os;
        var payment_date = start_date;
        for (i = 1; i <= duration; i++) {
            var annualinterest = check_change_rate_option(i,option_change_rate,0)==false?parseFloat(rate):(check_change_rate_option(i,option_change_rate,0));
            annualinterest = annualinterest*parseFloat(remainingBalance);
            var monthly_payment = parseFloat(principle) + parseFloat(annualinterest);
            remainingBalance -= parseFloat(principle);

            if (remainingBalance < 1) {
                remainingBalance = 0;
            }
            row += `<tr>
                            <td><label>Step</label></td>
                            <td><input type="text" class="form-control" value="${i}" readonly></td>
                            <td><label>Date</label></td>
                            <td><input type="text" class="form-control loan-date-picker loan-payment-date" value="${payment_date}" readonly></td>
                            <td><label>Principal</label></td>
                            <td><input type="text" class="form-control principal" value="${principle}" readonly></td>
                            <td><label>Interest</label></td>
                            <td><input type="text" class="form-control" value="${annualinterest}" readonly></td>
                            <td><label>Payment</label></td>
                            <td><input type="text" class="form-control monthly_payment" value="${monthly_payment}" readonly></td>
                            <td><label>Balance</label></td>
                            <td><input type="text" class="form-control" value="${remainingBalance}" readonly></td>
                        </tr>`;

            payment_date = Loan_date_generator(payment_date);
            start_on = end_on;
            end_on = payment_date;
        }
    } else {
        var check_rate_condition = false;
        for (i = 1; i <= duration; i++) 
        {
            var rate_interest_check = check_change_rate_option(i,option_change_rate,0)==false?parseFloat(rate):check_change_rate_option(i,option_change_rate,0);
            if(Number(rate_interest_check)==0) {
                check_rate_condition = true;
            }
        }
        if(check_rate_condition==true){
            alert("Please check rate (it must be more than zero) !");
        }else {
            var remainingBalance = amount;
            var amount_os_change = amount;
            var payment_date = start_date;
            var duration_rate_change =0;
            var monthly_payment = (amount * rate) / (1 - (Math.pow((1 + rate), -Math.abs(duration))));
            var rate_cp=rate;
            for (i = 1; i <= duration; i++) {
                
                var rate_interest = check_change_rate_option(i,option_change_rate,0)==false?parseFloat(rate):check_change_rate_option(i,option_change_rate,0);
                console.log(check_change_rate_option(i,option_change_rate,0));
                if(rate_cp!=rate_interest){
                    rate_cp=rate_interest;
                    amount_os_change = remainingBalance;
                    monthly_payment = (amount_os_change * rate_interest) / (1 - (Math.pow((1 + rate_interest), -Math.abs(duration - duration_rate_change))));
                }
                duration_rate_change++;
                var start = i + 1;
                var annualinterest = remainingBalance * rate_interest;
                var principle = monthly_payment - annualinterest;
                remainingBalance -= principle;
                if (remainingBalance < 1) {
                    remainingBalance = 0;
                }
                
                row += `<tr>
            <td><label>Step</label></td>
            <td><input type="text" class="form-control" value="${i}" readonly></td>
            <td><label>Date</label></td>
            <td><input type="text" class="form-control loan-date-picker loan-payment-date" value="${payment_date}" readonly></td>
            <td><label>Principal</label></td>
            <td><input type="text" class="form-control principal" value="${principle}" readonly></td>
            <td><label>Interest</label></td>
            <td><input type="text" class="form-control" value="${annualinterest}" readonly></td>
            <td><label>Payment</label></td>
            <td><input type="text" class="form-control monthly_payment" value="${monthly_payment}" readonly></td>
            <td><label>Balance</label></td>
            <td><input type="text" class="form-control" value="${remainingBalance}" readonly></td>
                            </tr>`;
                payment_date = Loan_date_generator(payment_date);
                start_on = end_on;
                end_on = payment_date;
            }
        }
        
    }
    return row;
}
function Loan_date_generator(date) {
    var next_date = date;
    var next_date = next_date.split('-');
    var str_date = next_date[2] + '-' + next_date[1] + '-' + next_date[0];
    var next_date = new Date(str_date);
    next_date.setMonth(next_date.getMonth() + 1);
    var new_next_date = new Date(next_date);
    var dd = new_next_date.getDate();
    var mm = new_next_date.getMonth() + 1;

    var yyyy = new_next_date.getFullYear();
    if (dd < 10) {
        dd = '0' + dd;
    }
    if (mm < 10) {
        mm = '0' + mm;
    }
    return new_next_date = dd + '-' + mm + '-' + yyyy;
}
function dateIntheNext30Day(givenDate){//format dd-mm-YYY 01-01-2011
    var currentDate = givenDate;
    var splitDate = currentDate.split('-');
    var newDate = splitDate[2]+'-'+splitDate[1]+'-'+splitDate[0];
    var newDate = new Date(newDate);
    newDate.setDate(newDate.getDate() + 30);
    var dateString = newDate.toISOString().split('T')[0];
    dateString = dateString.split('-');
    dateString = dateString[2]+'-'+dateString[1]+'-'+dateString[0];
    return dateString;
}