function addSalesQuotationMaintenanceData() {    
    sales_ms_idx++;

    var newRow = "<tr data-row='sales-quotation-maintenance-rows-"+sales_ms_idx+"' id='sales-quotation-maintenance-rows-"+sales_ms_idx+"' class='dnd-moved kt-datatable__row' style='left: 0px;'> " +
                    "<td data-field='id' class='kt-datatable__cell' style='display: none;'>" +
                        "<input type='text' class='form-control txtMaintenanceId' name='input_row' id='txtMaintenanceId"+sales_ms_idx+"' value='-1' />"+
                    "</td> " +
                    
                    "<td data-field='text' id='col_number"+sales_ms_idx+"' class='kt-datatable__cell' style='width: 50px;vertical-align:top;'>"+                   
                    "<span>"+
                    "<input type='text' class='form-control' style='width:40px;text-align:right' name='input_row' id='txtMaintenanceNo"+sales_ms_idx+"' placeholder='No.' value='"+sales_ms_idx+"' disabled />"+
                    "</span></td> " +

                    "<td data-field='description' class='kt-datatable__cell' style='width: 650px;'>"+
                        "<span>"+
                            "<div style='padding-right: 25px;'>"+
                                "<input type='text' class='form-control txtMaintenanceDesc' name='input_row' id='txtMaintenanceDesc"+sales_ms_idx+"' placeholder='Description'/>"+
                            "</div>"+
                            "<div style='margin-top:10px;padding-left: 25px;width:600px;'>"+
                                "<textarea class='form-control txtMaintenanceComment' name='input_row' id='txtMaintenanceComment"+sales_ms_idx+"' placeholder='Comment' rows='1'></textarea>"+
                            "</div>"+
                        "<span>"+
                    "</td>"+

                    "<td data-field='amount' class='kt-datatable__cell' style='width: 100px;vertical-align:top;'>"+
                        "<div>"+
                            "<input type='text' class='form-control txtMaintenanceAmount' name='input_row' id='txtMaintenanceAmount"+sales_ms_idx+"' value='0.00' style='text-align:right' onchange='javascript:getTotalAmountMaintenance("+sales_ms_idx+");' />"+
                        "</div>"+
                    "</td>"+

                    '<td style="width: 50px;vertical-align:top;" >'+
                        '<button title="Copy" onclick="javascript: copySalesQuotationMaintenanceData('+sales_ms_idx+', this);" id="btnCopy" class="btn btn btn-icon btn-info btn-sm btn-icon-md"><i class="fa fa-copy"></i></button>'+
                    '</td>' +

                    "<td style='width: 50px;vertical-align:top;'>"+
                        "<button type='button' class='btn btn btn-icon btn-danger btn-sm btn-icon-md' id='btnRemoveSalesMaintenanceData' onclick='removeSalesMaintenanceData("+sales_ms_idx+");'><i class='fa fa-trash'></i></button>"+
                    "</td> " +
                    "<td></td>"
                "</tr>";
                
    $('#sales-quotation-maintenance-list').append(newRow);

    getTotalAmountMaintenance();
}

function copySalesQuotationMaintenanceData(idx) {
    $('#sales-quotation-maintenance-rows-'+idx).each(function() { 
        desc       = $('.txtMaintenanceDesc',this).val()  ;
        comment    = $('.txtMaintenanceComment',this).val()  ;
        amount     = $('.txtMaintenanceAmount',this).val()  ;        
    }); 

    sales_ms_idx++;  

    $('#sales-quotation-maintenance-list').append(
               "<tr data-row='sales-quotation-maintenance-rows-"+sales_ms_idx+"' id='sales-quotation-maintenance-rows-"+sales_ms_idx+"' class='dnd-moved kt-datatable__row' style='left: 0px;'> " +
                    "<td data-field='id' class='kt-datatable__cell' style='display: none;'>" +
                        "<input type='text' class='form-control txtMaintenanceId' name='input_row' id='txtMaintenanceId"+sales_ms_idx+"' value='-1' />"+
                    "</td> " +
                    
                    "<td data-field='text' id='col_number"+sales_ms_idx+"' class='kt-datatable__cell' style='width: 50px;vertical-align:top;'>"+                   
                    "<span>"+                    
                    "<input type='text' class='form-control' style='width:40px;text-align:right' name='input_row' id='txtMaintenanceNo"+sales_ms_idx+"' placeholder='No.' value='"+sales_ms_idx+"' disabled />"+
                    "</span></td> " +

                    "<td data-field='description' class='kt-datatable__cell' style='width: 650px;'>"+
                        "<span>"+
                            "<div style='padding-right: 25px;'>"+
                                "<input type='text' class='form-control txtMaintenanceDesc' name='input_row' id='txtMaintenanceDesc"+sales_ms_idx+"' placeholder='Description' value='"+desc+"'/>"+
                            "</div>"+
                            "<div style='margin-top:10px;padding-left: 25px;width:600px;'>"+
                                "<textarea class='form-control txtMaintenanceComment' name='input_row' id='txtMaintenanceComment"+sales_ms_idx+"' placeholder='Comment' rows='1'>"+comment+"</textarea>"+
                            "</div>"+
                        "<span>"+
                    "</td>"+

                    "<td data-field='amount' class='kt-datatable__cell' style='width: 100px;vertical-align:top;'>"+
                        "<div>"+
                            "<input type='text' class='form-control txtMaintenanceAmount' name='input_row' id='txtMaintenanceAmount"+sales_ms_idx+"' style='text-align:right' onchange='javascript:getTotalAmountMaintenance("+sales_ms_idx+");' value='"+amount+"'/>"+
                        "</div>"+
                    "</td>"+

                    '<td style="width: 50px;vertical-align:top;" >'+
                        '<button title="Copy" onclick="javascript: copySalesQuotationMaintenanceData('+sales_ms_idx+', this);" id="btnCopy" class="btn btn btn-icon btn-info btn-sm btn-icon-md"><i class="fa fa-copy"></i></button>'+
                    '</td>' +

                    "<td style='width: 50px;vertical-align:top;'>"+
                        "<button type='button' class='btn btn btn-icon btn-danger btn-sm btn-icon-md' id='btnRemoveSalesMaintenanceData' onclick='removeSalesMaintenanceData("+sales_ms_idx+");'><i class='fa fa-trash'></i></button>"+
                    "</td> " +
                    "<td></td>"+
                "</tr>"
    );

    getTotalAmountMaintenance();
}

function showSalesQuotationMaintenanceData(myObj){
    sales_ms_idx = 1;
    var id_max = [1];
    for (i in myObj) {
        id          = myObj[i].id;
        desc        = myObj[i].desc;
        comment     = myObj[i].comment;
        amount      = myObj[i].amount;

        id_max.push(id);

        $('#sales-quotation-maintenance-list').append(
            "<tr data-row='sales-quotation-maintenance-rows-"+sales_ms_idx+"' id='sales-quotation-maintenance-rows-"+sales_ms_idx+"' class='dnd-moved kt-datatable__row' style='left: 0px;'> " +
                    "<td data-field='id' class='kt-datatable__cell' style='display: none;'>" +
                        "<input type='text' class='form-control txtMaintenanceId' name='input_row' id='txtMaintenanceId"+sales_ms_idx+"' value='-1' />"+
                    "</td> " +
                    
                    "<td data-field='text' id='col_number"+sales_ms_idx+"' class='kt-datatable__cell' style='width: 50px;vertical-align:top;'>"+                   
                    "<span>"+
                    "<input type='text' class='form-control' style='width:40px;text-align:right' name='input_row' id='txtMaintenanceNo"+sales_ms_idx+"' placeholder='No.' value='"+sales_ms_idx+"' disabled />"+
                    "</span></td> " +

                    "<td data-field='description' class='kt-datatable__cell' style='width: 650px;'>"+
                        "<span>"+
                            "<div style='padding-right: 25px;'>"+
                                "<input type='text' class='form-control txtMaintenanceDesc' name='input_row' id='txtMaintenanceDesc"+sales_ms_idx+"' placeholder='Description' value='"+desc+"'/>"+
                            "</div>"+
                            "<div style='margin-top:10px;padding-left: 25px;width:600px;'>"+
                                "<textarea class='form-control txtMaintenanceComment' name='input_row' id='txtMaintenanceComment"+sales_ms_idx+"' placeholder='Comment' rows='1'>"+comment+"</textarea>"+
                            "</div>"+
                        "<span>"+
                    "</td>"+

                    "<td data-field='amount' class='kt-datatable__cell' style='width: 100px;vertical-align:top;'>"+
                        "<div>"+
                            "<input type='text' class='form-control txtMaintenanceAmount' name='input_row' id='txtMaintenanceAmount"+sales_ms_idx+"' value='0.00' style='text-align:right' onchange='javascript:getTotalAmountMaintenance("+sales_ms_idx+");' />"+
                        "</div>"+
                    "</td>"+

                    '<td style="width: 50px;vertical-align:top;" >'+
                        '<button title="Copy" onclick="javascript: copySalesQuotationMaintenanceData('+sales_ms_idx+', this);" id="btnCopy" class="btn btn btn-icon btn-info btn-sm btn-icon-md"><i class="fa fa-copy"></i></button>'+
                    '</td>' +

                    "<td style='width: 50px;vertical-align:top;'>"+
                        "<button type='button' class='btn btn btn-icon btn-danger btn-sm btn-icon-md' id='btnRemoveSalesMaintenanceData' onclick='removeSalesMaintenanceData("+sales_ms_idx+");'><i class='fa fa-trash'></i></button>"+
                    "</td> " +
                    "<td></td>"+
                "</tr>"

                // "<tr data-row='sales-quotation-maintenance-rows-"+id+"' id='sales-quotation-maintenance-rows-"+id+"' class='dnd-moved kt-datatable__row' style='left: 0px;'> " +
                //     "<td data-field='id' class='kt-datatable__cell' style='display: none;'>" +
                //         "<input type='text' class='form-control txtMaintenanceId' name='input_row' id='txtMaintenanceId"+id+"' value='"+id+"' />"+
                //     "</td> " +
                    
                //     "<td data-field='text' id='col_number"+sales_ms_idx+"' class='kt-datatable__cell' style='width: 50px;vertical-align:top;'>"+                   
                //     "<span>"+                    
                //     "<input type='text' class='form-control' style='width:40px;text-align:right' name='input_row' id='txtMaintenanceNo"+sales_ms_idx+"' placeholder='No.' value='"+sales_ms_idx+"' disabled />"+
                //     "</span></td> " +

                //     "<td data-field='description' class='kt-datatable__cell' style='width: 650px;'>"+
                //         "<span>"+
                //             "<div style='padding-right: 25px;'>"+
                //                 "<input type='text' class='form-control txtMaintenanceDesc' name='input_row' id='txtMaintenanceDesc"+id+"' placeholder='Description' value='"+desc+"'/>"+
                //             "</div>"+
                //             "<div style='margin-top:10px;padding-left: 25px;width:600px;'>"+
                //                 "<textarea class='form-control txtMaintenanceComment' name='input_row' id='txtMaintenanceComment"+id+"' placeholder='Comment' rows='1'>"+comment+"</textarea>"+
                //             "</div>"+
                //         "<span>"+
                //     "</td>"+

                //     "<td data-field='amount' class='kt-datatable__cell' style='width: 100px;vertical-align:top;'>"+
                //         "<div>"+
                //             "<input type='text' class='form-control txtMaintenanceAmount' name='input_row' id='txtMaintenanceAmount"+id+"' style='text-align:right' onchange='javascript:getTotalAmountMaintenance("+id+");' value='"+amount+"'/>"+
                //         "</div>"+
                //     "</td>"+

                //     '<td style="width: 50px;vertical-align:top;" >'+
                //         '<button title="Copy" onclick="javascript: copySalesQuotationMaintenanceData('+id+', this);" id="btnCopy" class="btn btn btn-icon btn-info btn-sm btn-icon-md"><i class="fa fa-copy"></i></button>'+
                //     '</td>' +

                //     "<td style='width: 50px;vertical-align:top;'>"+
                //         "<button type='button' class='btn btn btn-icon btn-danger btn-sm btn-icon-md' id='btnRemoveSalesMaintenanceData' onclick='removeSalesMaintenanceData("+id+");'><i class='fa fa-trash'></i></button>"+
                //     "</td> " +
                // "</tr>"
         );

        sales_ms_idx = parseInt(id) +1;
    }
    sales_ms_idx = Math.max.apply(null, id_max);

    getTotalAmountMaintenance();
}

function showMaintenanceTemplate(myObj){
    sales_ms_idx = 1;
    var id_max = [1];

    $('#sales-quotation-maintenance-list').html("");
    for (i in myObj) {

        id          = myObj[i].id;
        desc        = myObj[i].desc;
        comment     = myObj[i].comment;
        amount      = myObj[i].amount;

        id_max.push(id);

        $('#sales-quotation-maintenance-list').append(
                "<tr data-row='sales-quotation-maintenance-rows-"+id+"' id='sales-quotation-maintenance-rows-"+id+"' class='dnd-moved kt-datatable__row' style='left: 0px;'> " +
                    "<td data-field='id' class='kt-datatable__cell' style='display: none;'>" +
                        "<input type='text' class='form-control txtMaintenanceId' name='input_row' id='txtMaintenanceId"+id+"' value='-1' />"+
                    "</td> " +
                    
                    "<td data-field='text' id='col_number"+sales_ms_idx+"' class='kt-datatable__cell' style='width: 50px;vertical-align:top;'>"+                   
                    "<span>"+
                    "<input type='text' class='form-control' style='width:40px;text-align:right' name='input_row' id='txtMaintenanceNo"+sales_ms_idx+"' placeholder='No.' value='"+sales_ms_idx+"' disabled />"+
                    "</span></td> " +

                    "<td data-field='description' class='kt-datatable__cell' style='width: 650px;'>"+
                        "<span>"+
                            "<div style='padding-right: 25px;'>"+
                                "<input type='text' class='form-control txtMaintenanceDesc' name='input_row' id='txtMaintenanceDesc"+id+"' placeholder='Description' value='"+desc+"'/>"+
                            "</div>"+
                            "<div style='margin-top:10px;padding-left: 25px;width:600px;'>"+
                                "<textarea class='form-control txtMaintenanceComment' name='input_row' id='txtMaintenanceComment"+id+"' placeholder='Comment' rows='1'>"+comment+"</textarea>"+
                            "</div>"+
                        "<span>"+
                    "</td>"+

                    "<td data-field='amount' class='kt-datatable__cell' style='width: 100px;vertical-align:top;'>"+
                        "<div>"+
                            "<input type='text' class='form-control txtMaintenanceAmount' name='input_row' id='txtMaintenanceAmount"+id+"' style='text-align:right' onchange='javascript:getTotalAmountMaintenance("+id+");' value='"+amount+"'/>"+
                        "</div>"+
                    "</td>"+

                    '<td style="width: 50px;vertical-align:top;" >'+
                        '<button title="Copy" onclick="javascript: copySalesQuotationMaintenanceData('+id+', this);" id="btnCopy" class="btn btn btn-icon btn-info btn-sm btn-icon-md"><i class="fa fa-copy"></i></button>'+
                    '</td>' +

                    "<td style='width: 50px;vertical-align:top;'>"+
                        "<button type='button' class='btn btn btn-icon btn-danger btn-sm btn-icon-md' id='btnRemoveSalesMaintenanceData' onclick='removeSalesMaintenanceData("+id+");'><i class='fa fa-trash'></i></button>"+
                    "</td> " +
                "</tr>"
         ); 

        sales_ms_idx = parseInt(id) +1;
    }

    sales_ms_idx = Math.max.apply(null, id_max);

    getTotalAmountMaintenance();
}

function removeSalesMaintenanceData(idx) {
    $("#sales-quotation-maintenance-rows-"+idx).remove();
    sales_ms_idx--;
    updateSalesMaintenanceCounter();
    getTotalAmountMaintenance();
}

function updateSalesMaintenanceCounter(){
    var count = 0;
    $('#sales-quotation-maintenance-list tr').each(function() {
        var idx = $(this).attr('id');
        idx = idx.substring(idx.lastIndexOf('-')+1);
        count++;
        $('#col_number'+idx+' span').text(count);
    });
}

function getMaintenance(){
    var arrays = [];
    $('#sales-quotation-maintenance-list tr').each(function() { 

        var data = {     
                    "id"            :$('.txtMaintenanceId',this).val()  ,
                    "desc"          :$('.txtMaintenanceDesc',this).val()  ,
                    "comment"       :$('.txtMaintenanceComment',this).val()  ,
                    "amount"        :$('.txtMaintenanceAmount',this).val()
                   };   

        arrays.push(data);
    }); 

    return JSON.stringify(arrays) ;
}

function clearMaintenance() {
    sales_ms_idx = 0;
    $('#sales-quotation-maintenance-list').html('');

    $('#txtSalesMaintenanceSubtotal').val('0');
    $('#txtSalesMaintenanceSubtotal').prop("disabled", true);
    $('#cobSalesMaintenanceDiscType').val('');
    $('#txtSalesMaintenanceDiscPercentage').val('0');
    $('#txtSalesMaintenanceDiscPercentage').prop("disabled", true);
    $('#txtSalesMaintenanceDiscTotal').val('0');
    $('#txtSalesMaintenanceDiscTotal').prop("disabled", true);
    $('#txtSalesMaintenanceGstPercentage').val('0');
    $('#txtSalesMaintenanceGstTotal').val('0');
    $('#txtSalesMaintenanceGstTotal').prop("disabled", true);
    $('#txtSalesMaintenanceGrandTotal').val('0');
    $('#txtSalesMaintenanceGrandTotal').prop("disabled", true);

    changeMaintenanceDiscountType($('#cobSalesMaintenanceDiscType'));
}

function getTotalAmountMaintenance() {
    var total_amount = 0;

    $('#sales-quotation-maintenance-list tr').each(function() {
        var idx = $(this).attr('id');
        idx = idx.substring(idx.lastIndexOf('-')+1);

        var amount = $('.txtMaintenanceAmount',this).val()  ; 
        if(amount == "") {
            amount = 0;
        }
        total_amount += parseFloat(amount);
        $('#txtMaintenanceAmount'+idx).val(parseFloat(amount).toFixed(2));
    }); 

    $('#txtSalesMaintenanceSubtotal').val(parseFloat(total_amount).toFixed(2));

    calculateMaintenanceTotal();
}

function calculateMaintenanceTotal() {
    var sub_total       = $('#txtSalesMaintenanceSubtotal').val();
    var disc_type       = $('#cobSalesMaintenanceDiscType').val();
    var disc_percent    = $('#txtSalesMaintenanceDiscPercentage').val();
    var disc_total      = $('#txtSalesMaintenanceDiscTotal').val();
    var gst_percent     = $('#txtSalesMaintenanceGstPercentage').val();
    var gst_total       = $('#txtSalesMaintenanceGstTotal').val();
    var grand_total     = $('#txtSalesMaintenanceGrandTotal').val();

    if(disc_type == 'P' && disc_percent != 0) {
        disc_total = (parseFloat(disc_percent) / 100) * parseFloat(sub_total);
        $('#txtSalesMaintenanceDiscTotal').val(parseFloat(disc_total).toFixed(2));
    } else {
        $('#txtSalesMaintenanceDiscPercentage').val('0');
        $('#txtSalesMaintenanceDiscTotal').val(parseFloat(disc_total).toFixed(2));
    }
    
    gst_total = (parseFloat(gst_percent) / 100) * (parseFloat(sub_total) - parseFloat(disc_total));
    grand_total = (parseFloat(sub_total) - parseFloat(disc_total)) + parseFloat(gst_total);

    $('#txtSalesMaintenanceSubtotal').val(parseFloat(sub_total).toFixed(2));
    $('#txtSalesMaintenanceGstTotal').val(parseFloat(gst_total).toFixed(2));
    $('#txtSalesMaintenanceGrandTotal').val(parseFloat(grand_total).toFixed(2));
}

function popupSaveTemplateMaintenance() {
    var strBtn = "<button type=\"button\" class=\"btn btn-secondary\" data-dismiss=\"modal\">Cancel</button>" +
                     "<button onclick=\"saveTemplateMaintenance();\" class=\"btn btn-primary ml-2\" > Save </button>";
    $('#modalSaveMaintenanceTemplateConfirmationButton').html(strBtn);
}

function saveTemplateMaintenance() {
    var data = { 
            "name"                  : $('#MaintenanceTemplateName').val(),
            "data"                  : getMaintenance(),
            "maintenance_id"        : $('#txtSalesMaintenanceId').val(),
            "maintenance_currency"  : $('#lblSalesMaintenanceCurr').text(),

            "subtotal_maintenance"          : $('#txtSalesMaintenanceSubtotal').val(),
            "disc_type_maintenance"         : $('#cobSalesMaintenanceDiscType').val(),
            "disc_percentage_maintenance"   : $('#txtSalesMaintenanceDiscPercentage').val(),
            "disc_total_maintenance"        : $('#txtSalesMaintenanceDiscTotal').val(),
            "gst_percentage_maintenance"    : $('#txtSalesMaintenanceGstPercentage').val(),
            "gst_total_maintenance"         : $('#txtSalesMaintenanceGstTotal').val(),
            "grand_total_maintenance"       : $('#txtSalesMaintenanceGrandTotal').val(),

            "option_template"       : $('#txtChkMaintenanceTempOption').val(),
            "template_id"           : $('#MaintenanceTemplateList').val(),
            // "new_name"              : $('#maintenanceTemplateNewName').val(),
            "type"                  : "C_SAVE_MAINTENANCE_TEMPLATE"
            };

    var test = ($('#chkMaintenanceTempOption').is(':checked') ? '1' : '0');

    var url = "index.pzx?c=" + sales_quotation_url + "&d=" + new Date().getTime() ;
  
    callServer(url,"json",data,onMaintenanceTemplateResponse,null) ;
}

function changeMaintenanceTempOption() {
    if($('#chkMaintenanceTempOption').is(':checked')){
        $('#divMaintenanceTemplateList').hide();
        $('#divMaintenanceTemplateName').show();

        $('#txtChkMaintenanceTempOption').val("1");
    }else{
        $('#divMaintenanceTemplateList').show();
        $('#divMaintenanceTemplateName').hide();

        $('#txtChkMaintenanceTempOption').val("0");
    }

    populateMaintenanceTemplateList();
}

function populateMaintenanceTemplateList() {
    var data = { 
                "type"                 : "C_VIEW_LOAD_MAINTENANCE_TEMPLATE_LIST"
            };
    var url = "index.pzx?c=" + sales_quotation_url + "&d=" + new Date().getTime() ;
  
    callServer(url,"json",data,onMaintenanceTemplateResponse,null) ;
}

function popupLoadTemplateMaintenance() {
    var data = { 
                "type"                 : "C_VIEW_LOAD_MAINTENANCE_TEMPLATE"
                };
    var url = "index.pzx?c=" + sales_quotation_url + "&d=" + new Date().getTime() ;
  
    callServer(url,"json",data,onMaintenanceTemplateResponse,null) ;
}

function DeleteTemplateMaintenance() {
    $("#LoadMaintenanceTemplateConfirmationView").modal("hide");
    var data = { 
            "template_id"          : $('#MaintenanceTemplateLoad').val(),
            "type"                 : "C_DELETE_MAINTENANCE_TEMPLATE"
            };
    var url = "index.pzx?c=" + sales_quotation_url + "&d=" + new Date().getTime() ;
  
    callServer(url,"json",data,onMaintenanceTemplateResponse,null) ;
}

function LoadTemplateMaintenance() {
    var data = { 
                "template_id"          : $('#MaintenanceTemplateLoad').val(),
                "type"                 : "C_LOAD_MAINTENANCE_TEMPLATE"
                };
    var url = "index.pzx?c=" + sales_quotation_url + "&d=" + new Date().getTime() ;
  
    callServer(url,"json",data,onMaintenanceTemplateResponse,null) ;
}

function onMaintenanceTemplateResponse(obj,resp){
    if (resp.status != C_OK ) {
        Swal.fire("Error", resp.mesg, "error");
        return false;
    }

    if(resp.type == "C_SAVE_MAINTENANCE_TEMPLATE"){
        $('#MaintenanceTemplateName').val("");
        $('#chkMaintenanceTempOption').prop('checked', true);
        $('#MaintenanceTemplateList').val(0);
        // $('#MaintenanceTemplateNewName').val("");

        $('#SaveMaintenanceTemplateConfirmationView').modal('hide'); 
        Swal.fire("Success", resp.mesg, "success");
      
    }else if(resp.type == "C_VIEW_LOAD_MAINTENANCE_TEMPLATE"){
        var strBtn = "<button type=\"button\" class=\"btn btn-secondary\" data-dismiss=\"modal\">Cancel</button>" +
                     "<button onclick=\"DeleteTemplateMaintenance();\" type=\"button\" class=\"btn btn-danger ml-2\" >Delete</button>" +
                     "<button onclick=\"LoadTemplateMaintenance();\" class=\"btn btn-primary ml-2\" > Load </button>";

        $('#modalLoadMaintenanceTemplateConfirmationButton').html(strBtn);
        $("#MaintenanceTemplateLoad").html(resp.data.load_option);
      
    } else if(resp.type == "C_VIEW_LOAD_MAINTENANCE_TEMPLATE_LIST"){
        $("#MaintenanceTemplateList").html(resp.data.load_option);
    }
    else if(resp.type == "C_LOAD_MAINTENANCE_TEMPLATE"){
        $('#LoadMaintenanceTemplateConfirmationView').modal('hide'); 

         var myObj = JSON.parse(resp.data.salesquotation_maintenance);
         showMaintenanceTemplate(myObj);

         $('#txtSalesMaintenanceId').val(resp.data.maintenance_id);
         $('#lblSalesMaintenanceCurr').text(resp.data.maintenance_currency);

         $('#txtSalesMaintenanceSubtotal').val(resp.data.subtotal_maintenance);
         $('#cobSalesMaintenanceDiscType').val(resp.data.disc_type_maintenance);
         $('#txtSalesMaintenanceDiscPercentage').val(resp.data.disc_percentage_maintenance);
         $('#txtSalesMaintenanceDiscTotal').val(resp.data.disc_total_maintenance);
         $('#txtSalesMaintenanceGstPercentage').val(resp.data.gst_percentage_maintenance);
         $('#txtSalesMaintenanceGstTotal').val(resp.data.gst_total_maintenance);
         $('#txtSalesMaintenanceGrandTotal').val(resp.data.grand_total_maintenance);
    } else if(resp.type == "C_DELETE_MAINTENANCE_TEMPLATE"){
         Swal.fire("Success", resp.mesg, "success");
    }
}
function changeMaintenanceDiscountType(obj) {
    $('#txtSalesMaintenanceDiscPercentage').prop("disabled", true);
    $('#txtSalesMaintenanceDiscTotal').prop("disabled", true);
    if($(obj).val()=='P'){
        $('#txtSalesMaintenanceDiscPercentage').prop("disabled", false);
        $('#txtSalesMaintenanceDiscTotal').prop("disabled", true);
    } else if($(obj).val()=='A'){
        $('#txtSalesMaintenanceDiscPercentage').prop("disabled", true);
        $('#txtSalesMaintenanceDiscTotal').prop("disabled", false);
    }
    $('#txtSalesMaintenanceDiscPercentage').val('0');
    $('#txtSalesMaintenanceDiscTotal').val('0');
    calculateMaintenanceTotal();
}