function addSalesQuotationProjectPlanData() {
    sales_projectplan_idx++;

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

                    "<td style='vertical-align:top;'>"+
                        "<input type='text' class='form-control txtSalesProjectPlanDesc' name='input_row' id='txtSalesProjectPlanDesc"+sales_projectplan_idx+"' style='width: 410px;'/>"+
                    "</td>"+

                    "<td style='vertical-align:top;'>"+
                        "<input type='number' class='form-control text-right txtSalesProjectPlanDays' name='input_row' id='txtSalesProjectPlanDays"+sales_projectplan_idx+"' onchange='getTotalDaysProjectPlan();' style='width: 90px;'/>"+
                    "</td>"+

                    '<td style="width: 50px;vertical-align:top;" >'+
                        '<button title="Copy" onclick="javascript: copySalesQuotationProjectPlanData('+sales_projectplan_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='btnRemoveSalesProjectPlanData' onclick='removeSalesProjectPlanData("+sales_projectplan_idx+");'><i class='fa fa-trash'></i></button>"+
                    "</td> " +
                "</tr>";
                
    $('#sales-quotation-projectplan-list').append(newRow);

    getTotalDaysProjectPlan();
}

function copySalesQuotationProjectPlanData(idx) {
    $('#sales-quotation-projectplan-rows-'+idx).each(function() { 
        phase       = $('.txtSalesProjectPlanPhase',this).val()  ;
        desc        = $('.txtSalesProjectPlanDesc',this).val()  ;
        days     = $('.txtSalesProjectPlanDays',this).val()  ;        
    }); 

    sales_projectplan_idx++;  

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

                    "<td style='vertical-align:top;'>"+
                        "<input type='text' class='form-control txtSalesProjectPlanDesc' name='input_row' id='txtSalesProjectPlanDesc"+sales_projectplan_idx+"' style='width: 410px;' value='"+desc+"'/>"+
                    "</td>"+

                    "<td style='vertical-align:top;'>"+
                        "<input type='number' class='form-control text-right txtSalesProjectPlanDays' name='input_row' id='txtSalesProjectPlanDays"+sales_projectplan_idx+"' onchange='getTotalDaysProjectPlan();' style='width: 90px;' value='"+days+"'/>"+
                    "</td>"+

                    '<td style="width: 50px;vertical-align:top;" >'+
                        '<button title="Copy" onclick="javascript: copySalesQuotationProjectPlanData('+sales_projectplan_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='btnRemoveSalesProjectPlanData' onclick='removeSalesProjectPlanData("+sales_projectplan_idx+");'><i class='fa fa-trash'></i></button>"+
                    "</td> " +
                "</tr>"
    );

    getTotalDaysProjectPlan();
}


function showSalesQuotationProjectPlanData(myObj){
    sales_projectplan_idx = 1;
    var id_max = [1];
    for (i in myObj) {

        id = myObj[i].id;

        phase   = myObj[i].phase;
        desc    = myObj[i].desc;
        days    = myObj[i].days;

        id_max.push(id);

        $('#sales-quotation-projectplan-list').append(
                "<tr data-row='sales-quotation-projectplan-rows-"+id+"' id='sales-quotation-projectplan-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 txtSalesProjectPlanId' name='input_row' id='txtSalesProjectPlanId"+id+"' value='"+id+"' />"+
                    "</td> " +
                    
                    "<td data-field='text' class='kt-datatable__cell' id='col_number"+sales_projectplan_idx+"' style='width: 50px;vertical-align:top;'>"+
                        "<span>"+
                        "<input type='text' class='form-control' style='width:40px;text-align:right' name='input_row' id='txtSalesProjectPlanNo"+sales_projectplan_idx+"' placeholder='No.' value='"+sales_projectplan_idx+"' disabled />"+
                        "</span>"+
                    "</td> " +
                    
                    "<td style='vertical-align:top;'>"+
                        "<input type='text' class='form-control txtSalesProjectPlanPhase' name='input_row' id='txtSalesProjectPlanPhase"+id+"' style='width: 100px;' value='"+phase+"'/>"+
                    "</td>"+

                    "<td style='vertical-align:top;'>"+
                        "<input type='text' class='form-control txtSalesProjectPlanDesc' name='input_row' id='txtSalesProjectPlanDesc"+id+"' style='width: 410px;' value='"+desc+"'/>"+
                    "</td>"+

                    "<td style='vertical-align:top;'>"+
                        "<input type='number' class='form-control text-right txtSalesProjectPlanDays' name='input_row' id='txtSalesProjectPlanDays"+id+"' onchange='getTotalDaysProjectPlan();' style='width: 90px;' value='"+days+"'/>"+
                    "</td>"+

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

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

function showProjectPlanTemplate(myObj){
    sales_projectplan_idx = 1;
    var id_max = [1];

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

        id = myObj[i].id;

        phase   = myObj[i].phase;
        desc    = myObj[i].desc;
        days = myObj[i].days;

        id_max.push(id);

        $('#sales-quotation-projectplan-list').append(
                "<tr data-row='sales-quotation-projectplan-rows-"+id+"' id='sales-quotation-projectplan-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 txtSalesProjectPlanId' name='input_row' id='txtSalesProjectPlanId"+id+"' value='-1' />"+
                    "</td> " +
                    
                    "<td data-field='text' class='kt-datatable__cell' id='col_number"+sales_projectplan_idx+"' style='width: 50px;vertical-align:top;'>"+
                        "<span>"+
                        "<input type='text' class='form-control' style='width:40px;text-align:right' name='input_row' id='txtSalesProjectPlanNo"+sales_projectplan_idx+"' placeholder='No.' value='"+sales_projectplan_idx+"' disabled />"+
                        "</span>"+
                    "</td> " +
                    
                    "<td style='vertical-align:top;'>"+
                        "<input type='text' class='form-control txtSalesProjectPlanPhase' name='input_row' id='txtSalesProjectPlanPhase"+id+"' style='width: 100px;' value='"+phase+"'/>"+
                    "</td>"+

                    "<td style='vertical-align:top;'>"+
                        "<input type='text' class='form-control txtSalesProjectPlanDesc' name='input_row' id='txtSalesProjectPlanDesc"+id+"' style='width: 410px;' value='"+desc+"'/>"+
                    "</td>"+

                    "<td style='vertical-align:top;'>"+
                        "<input type='number' class='form-control txtSalesProjectPlanDays' name='input_row' id='txtSalesProjectPlanDays"+id+"' onchange='getTotalDaysProjectPlan();' style='width: 90px;' value='"+days+"'/>"+
                    "</td>"+

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

        sales_projectplan_idx = parseInt(id) +1;
    }

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

    getTotalDaysProjectPlan();
}

function removeSalesProjectPlanData(idx) {
    $("#sales-quotation-projectplan-rows-"+idx).remove();
    sales_projectplan_idx--;
    updateProjectPlanCounter();
    getTotalDaysProjectPlan();
}

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

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

        var data = {     
                    "id"            :$('.txtSalesProjectPlanId',this).val()  ,
                    "phase"         :$('.txtSalesProjectPlanPhase',this).val()  ,
                    "desc"          :$('.txtSalesProjectPlanDesc',this).val()  ,
                    "days"          :$('.txtSalesProjectPlanDays',this).val()
                   };   

        arrays.push(data);
    }); 

    return JSON.stringify(arrays) ;
}

function clearProjectPlan() {
    sales_projectplan_idx = 0;
    $('#sales-quotation-projectplan-list').html('');
}

function getTotalDaysProjectPlan() {
    var total_hours = 0;

    $('#sales-quotation-projectplan-list tr').each(function() {
        var hours = $('.txtSalesProjectPlanDays',this).val()  ; 
        if(hours == "") {
            hours = 0;
        }     

        total_hours += parseFloat(hours);
        // console.log(total_hours);
    }); 

    $('#txtSalesProjectPlanTotalDays').val(parseFloat(total_hours).toFixed(2));
}

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

function saveTemplateProjectPlan() {
    var data = { 
            "name"                  : $('#ProjectPlanTemplateName').val(),

            "data"                  : getProjectPlan(),
            "project_plan_id"       : $('#txtSalesProjectPlanId').val(),
            "project_plan_amount"   : $('#txtSalesProjectPlanTotalAmount').val(),
            "project_plan_currency" : $('#lblSalesProjectPlanCurr').text(),

            "option_template"       : $('#txtChkProjectPlanTempOption').val(),
            "template_id"           : $('#ProjectPlanTemplateList').val(),
            // "new_name"              : $('#projectPlanTemplateNewName').val(),
            "type"                  : "C_SAVE_PROJECTPLAN_TEMPLATE"
            };

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

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

function changeProjectPlanTempOption() {
    if($('#chkProjectPlanTempOption').is(':checked')){
        $('#divProjectPlanTemplateList').hide();
        $('#divProjectPlanTemplateName').show();

        $('#txtChkProjectPlanTempOption').val("1");
    }else{
        $('#divProjectPlanTemplateList').show();
        $('#divProjectPlanTemplateName').hide();

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

    populateProjectPlanTemplateList();
}

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

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

function DeleteTemplateProjectPlan() {
    $("#LoadProjectPlanTemplateConfirmationView").modal("hide");
    var data = { 
            "template_id"          : $('#ProjectPlanTemplateLoad').val(),
            "type"                 : "C_DELETE_PROJECTPLAN_TEMPLATE"
            };
    var url = "index.pzx?c=" + sales_quotation_url + "&d=" + new Date().getTime() ;
  
    callServer(url,"json",data,onProjectPlanTemplateResponse,null) ;
}

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

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

    if(resp.type == "C_SAVE_PROJECTPLAN_TEMPLATE"){
        $('#ProjectPlanTemplateName').val("");
        $('#chkProjectPlanTempOption').prop('checked', true);
        $('#ProjectPlanTemplateList').val(0);
        // $('#ProjectPlanTemplateNewName').val("");

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

        $('#modalLoadProjectPlanTemplateConfirmationButton').html(strBtn);
        $("#ProjectPlanTemplateLoad").html(resp.data.load_option);
      
    } else if(resp.type == "C_VIEW_LOAD_PROJECTPLAN_TEMPLATE_LIST"){
        $("#ProjectPlanTemplateList").html(resp.data.load_option);
    }
    else if(resp.type == "C_LOAD_PROJECTPLAN_TEMPLATE"){
        $('#LoadProjectPlanTemplateConfirmationView').modal('hide'); 

         var myObj = JSON.parse(resp.data.salesquotation_project_plan);
         showProjectPlanTemplate(myObj);

         $('#txtSalesProjectPlanId').val(resp.data.project_plan_id);
         $('#txtSalesProjectPlanTotalAmount').val(resp.data.project_plan_amount);
         $('#lblSalesProjectPlanCurr').text(resp.data.project_plan_currency);
    } else if(resp.type == "C_DELETE_PROJECTPLAN_TEMPLATE"){
         Swal.fire("Success", resp.mesg, "success");
    }
}