function addSalesQuotationAppendixData() {
    sales_apdx_idx++;
    var cmbSection = '<select id="cmbSalesAppendixSection'+sales_apdx_idx+'" class="form-control cmbSalesAppendixSection" style="width: 300px;">'+$('#cmbSectionComb').html()+'</select>';

    var newRow = "<tr data-row='sales-quotation-appendix-rows-"+sales_apdx_idx+"' id='sales-quotation-appendix-rows-"+sales_apdx_idx+"' class='dnd-moved kt-datatable__row' style='left: 0px;'> " +
                    "<td data-field='id' class='kt-datatable__cell' id='col_id"+sales_apdx_idx+"' style='display: none;'>"+
                        "<input type='text' class='form-control txtSalesAppendixId' name='input_row' id='txtSalesAppendixId"+sales_apdx_idx+"' value='-1' />"+
                    "</td> " +
                    
                    "<td data-field='text' class='kt-datatable__cell' id='col_number"+sales_apdx_idx+"' style='width: 50px;vertical-align:top;'>"+
                        "<span>"+sales_apdx_idx+"</span>"+
                    "</td> " +

                    "<td style='vertical-align:top;'>"+
                        cmbSection +
                    "</td>"+
                    
                    "<td style='vertical-align:top;'>"+
                        "<input type='text' class='form-control txtSalesAppendixSubject' name='input_row' id='txtSalesAppendixSubject"+sales_apdx_idx+"'/>"+
                    "</td>"+


                    "<td style='vertical-align:top;'>"+
                        "<textarea class='form-control txtSalesAppendixDescription' name='input_row' id='txtSalesAppendixDescription"+sales_apdx_idx+"' rows='1'></textarea>"+
                    "</td>"+

                    '<td style="width: 50px;vertical-align:top;" >'+
                        '<button title="Copy" onclick="javascript: copySalesQuotationAppendixData('+sales_apdx_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='btnRemoveSalesAppendix' onclick='removeSalesAppendix("+sales_apdx_idx+");'><i class='fa fa-trash'></i></button>"+
                    "</td> " +
                "</tr>";
                
    $('#sales-quotation-appendix-list').append(newRow);
}

function showSalesQuotationAppendixData(myObj){
    sales_apdx_idx = 1;
    var id_max = [1];
    for (i in myObj) {

        id = myObj[i].id;

        section = myObj[i].section;
        subject = myObj[i].subject;
        desc = myObj[i].desc;

        id_max.push(id);
        var cmbSection = '<select id="cmbSalesAppendixSection'+id+'" class="form-control cmbSalesAppendixSection" style="width: 300px;">'+$('#cmbSectionComb').html()+'</select>';

        $('#sales-quotation-appendix-list').append(
            "<tr data-row='sales-quotation-appendix-rows-"+id+"' id='sales-quotation-appendix-rows-"+id+"' class='dnd-moved kt-datatable__row' style='left: 0px;'> " +
                "<td data-field='id' class='kt-datatable__cell' id='col_id"+id+"' style='display: none;'>"+
                    "<input type='text' class='form-control txtSalesAppendixId' name='input_row' id='txtSalesAppendixId"+id+"' value='"+id+"' />"+
                "</td> " +
                
                "<td data-field='text' class='kt-datatable__cell' id='col_number"+sales_apdx_idx+"' style='width: 50px;vertical-align:top;'>"+
                    "<span>"+sales_apdx_idx+"</span>"+
                "</td> " +

                "<td style='vertical-align:top;'>"+
                    cmbSection +
                "</td>"+
                
                "<td style='vertical-align:top;'>"+
                    "<input type='text' class='form-control txtSalesAppendixSubject' name='input_row' id='txtSalesAppendixSubject"+id+"' value='"+subject+"'/>"+
                "</td>"+


                "<td style='vertical-align:top;'>"+
                    "<textarea class='form-control txtSalesAppendixDescription' name='input_row' id='txtSalesAppendixDescription"+id+"' rows='1'>"+desc+"</textarea>"+
                "</td>"+

                '<td style="width: 50px;vertical-align:top;" >'+
                    '<button title="Copy" onclick="javascript: copySalesQuotationAppendixData('+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='btnRemoveSalesAppendix' onclick='removeSalesAppendix("+id+");'><i class='fa fa-trash'></i></button>"+
                "</td> " +
            "</tr>"
         ); 

        $("#cmbSalesAppendixSection"+id).val(section);

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

function showAppendixTemplate(myObj){
    sales_apdx_idx = 1;
    var id_max = [1];

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

        id = myObj[i].id;

        section = myObj[i].section;
        subject = myObj[i].subject;
        desc = myObj[i].desc;

        id_max.push(id);
        var cmbSection = '<select id="cmbSalesAppendixSection'+id+'" class="form-control cmbSalesAppendixSection" style="width: 300px;">'+$('#cmbSectionComb').html()+'</select>';

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

                "<td style='vertical-align:top;'>"+
                    cmbSection +
                "</td>"+
                
                "<td style='vertical-align:top;'>"+
                    "<input type='text' class='form-control txtSalesAppendixSubject' name='input_row' id='txtSalesAppendixSubject"+id+"' value='"+subject+"'/>"+
                "</td>"+


                "<td style='vertical-align:top;'>"+
                    "<textarea class='form-control txtSalesAppendixDescription' name='input_row' id='txtSalesAppendixDescription"+id+"' rows='1'>"+desc+"</textarea>"+
                "</td>"+

                '<td style="width: 50px;vertical-align:top;" >'+
                    '<button title="Copy" onclick="javascript: copySalesQuotationAppendixData('+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='btnRemoveSalesAppendix' onclick='removeSalesAppendix("+id+");'><i class='fa fa-trash'></i></button>"+
                "</td> " +
            "</tr>"
         ); 
         
        $("#cmbSalesAppendixSection"+id).val(section);

        sales_apdx_idx = parseInt(id) +1;
    }

    // sales_apdx_idx = Math.max.apply(null, id_max);
}

function copySalesQuotationAppendixData (idx) {
    $('#sales-quotation-appendix-rows-'+idx).each(function() { 
        section       = $('.cmbSalesAppendixSection',this).val()  ;
        subject       = $('.txtSalesAppendixSubject',this).val()  ;
        desc          = $('.txtSalesAppendixDescription',this).val()  ;        
    }); 

    sales_apdx_idx++;  

    var cmbSection = '<select id="cmbSalesAppendixSection'+sales_apdx_idx+'" class="form-control cmbSalesAppendixSection" style="width: 300px;">'+$('#cmbSectionComb').html()+'</select>';

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

                "<td style='vertical-align:top;'>"+
                    cmbSection +
                "</td>"+
                
                "<td style='vertical-align:top;'>"+
                    "<input type='text' class='form-control txtSalesAppendixSubject' name='input_row' id='txtSalesAppendixSubject"+sales_apdx_idx+"' value='"+subject+"'/>"+
                "</td>"+


                "<td style='vertical-align:top;'>"+
                    "<textarea class='form-control txtSalesAppendixDescription' name='input_row' id='txtSalesAppendixDescription"+sales_apdx_idx+"' rows='1' >"+desc+"</textarea>"+
                "</td>"+

                '<td style="width: 50px;vertical-align:top;" >'+
                    '<button title="Copy" onclick="javascript: copySalesQuotationAppendixData('+sales_apdx_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='btnRemoveSalesAppendix' onclick='removeSalesAppendix("+sales_apdx_idx+");'><i class='fa fa-trash'></i></button>"+
                "</td> " +
            "</tr>"
    );

    $("#cmbSalesAppendixSection"+sales_apdx_idx).val(section);
}

function removeSalesAppendix(idx) {
    $("#sales-quotation-appendix-rows-"+idx).remove();
    sales_apdx_idx--;
    updateSalesAppendixCounter();
}

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

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

        var data = {     
                    "id"            :$('.txtSalesAppendixId',this).val()  ,
                    "section"       :$('.cmbSalesAppendixSection',this).val()  ,
                    "subject"       :$('.txtSalesAppendixSubject',this).val()  ,
                    "desc"          :$('.txtSalesAppendixDescription',this).val()
                   };   

        arrays.push(data);
    }); 

    return JSON.stringify(arrays) ;
}

function clearAppendix() {
    sales_apdx_idx = 0;
    $('#sales-quotation-appendix-list').html('');
}

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

function saveTemplateAppendix() {
    var data = { 
            "name"              : $('#AppendixTemplateName').val(),
            "data"              : getAppendix(),
            "option_template"   : $('#txtChkAppendixTempOption').val(),
            "template_id"       : $('#AppendixTemplateList').val(),
            // "new_name"          : $('#appendixTemplateNewName').val(),
            "type"              : "C_SAVE_APPENDIX_TEMPLATE"
            };
    var url = "index.pzx?c=" + sales_quotation_url + "&d=" + new Date().getTime() ;
  
    callServer(url,"json",data,onAppendixTemplateResponse,null) ;
}

function changeAppendixTempOption() {
    if($('#chkAppendixTempOption').is(':checked')){
        $('#divAppendixTemplateList').hide();
        $('#divAppendixTemplateName').show();

        $('#txtChkAppendixTempOption').val("1");
    }else{
        $('#divAppendixTemplateList').show();
        $('#divAppendixTemplateName').hide();

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

    populateAppendixTemplateList();
}

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

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

function DeleteTemplateAppendix() {
    $("#LoadAppendixTemplateConfirmationView").modal("hide");
    var data = { 
            "template_id"          : $('#AppendixTemplateLoad').val(),
            "type"                 : "C_DELETE_APPENDIX_TEMPLATE"
            };
    var url = "index.pzx?c=" + sales_quotation_url + "&d=" + new Date().getTime() ;
  
    callServer(url,"json",data,onAppendixTemplateResponse,null) ;
}

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

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

    if(resp.type == "C_SAVE_APPENDIX_TEMPLATE"){
        $('#AppendixTemplateName').val("");
        $('#chkAppendixTempOption').prop('checked', true);
        $('#AppendixTemplateList').val(0);
        // $('#AppendixTemplateNewName').val("");

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

        $('#modalLoadAppendixTemplateConfirmationButton').html(strBtn);
        $("#AppendixTemplateLoad").html(resp.data.load_option);
      
    } else if(resp.type == "C_VIEW_LOAD_APPENDIX_TEMPLATE_LIST"){
        $("#AppendixTemplateList").html(resp.data.load_option);
    }
    else if(resp.type == "C_LOAD_APPENDIX_TEMPLATE"){
        $('#LoadAppendixTemplateConfirmationView').modal('hide'); 
         var myObj = JSON.parse(resp.data.salesquotation_appendix);
         showAppendixTemplate(myObj);
    } else if(resp.type == "C_DELETE_APPENDIX_TEMPLATE"){
         Swal.fire("Success", resp.mesg, "success");
    }
}