//add siti 09/04/2020 - reports module
var C_REPORT_FILTER                   		= "901";
var C_REPORT_DETAIL_FILTER            		= "902";
var C_REPORT_DETAIL_PRINT            		= "903";
var C_REPORT_JOB_STATUS_FILTER            	= "905";
var C_REPORT_JOB_STATUS_PRINT            	= "906";
var C_REPORT_JOB_STATUS_EXPORT 				= "937";
var C_REPORT_JOB_BILLING_STATUS_FILTER      = "908";
var C_REPORT_JOB_BILLING_STATUS_PRINT      	= "909";
var C_REPORT_BILLING_LISTING_FILTER      	= "911";
var C_REPORT_BILLING_LISTING_PRINT	     	= "912";
var C_REPORT_PLAN_SCHEDULE_FILTER      		= "914";
var C_REPORT_PLAN_SCHEDULE_PRINT      		= "915";
var C_REPORT_JOB_PLAN_ACTUAL_FILTER   		= "917";
var C_REPORT_JOB_PLAN_ACTUAL_PRINT   		= "918";
var C_REPORT_PLAN_ACTUAL_SUMMARY_FILTER  	= "920";
var C_REPORT_PLAN_ACTUAL_SUMMARY_PRINT  	= "921";
var C_REPORT_PLAN_ACTUAL_CONTRACT_FILTER  	= "923";
var C_REPORT_PLAN_ACTUAL_CONTRACT_PRINT  	= "924";
var C_REPORT_AGENT_STATUS_FILTER            = "926";
var C_REPORT_AGENT_STATUS_PRINT            	= "927";
var C_REPORT_ACTUAL_VS_BILLING_FILTER      	= "929";
var C_SHOW_DETAIL_ACTUAL_VS_BILLING 		= "930";
var C_REPORT_ACTUAL_VS_BILLING_PRINT      	= "931";;
var C_REPORT_SALES_QUOTATION_SUMMARY_FILTER = "933";
var C_SHOW_DETAIL_SALES_QUOTATION_SUMMARY 	= "934";
var C_REPORT_SALES_QUOTATION_SUMMARY_PRINT  = "935";
var C_REPORT_SALES_QUOTATION_SUMMARY_EXPORT = "936";

var report_obj     = null;

// $(document).ready(function() {
// });
// function getReportAutoSearch(type){
//     var data = { "type": type } ;
//     var url = "request.pzx?c=" + report_url + "&d=" + new Date().getTime() ;
//     callServer(url,"json",data,onReportAutoSearch,report_obj) ;
// }

// function onReportAutoSearch(obj,resp) { 
//     if (resp.status == C_OK) {
// 		if (resp.data != null || resp.data.length != 0 ) {
// 			if(resp.type == "C_SEARCH") {
// 				var filteredCustomer = resp.data;

// 				$('#txtCustomerFilter').autocomplete({
// 					source: filteredCustomer,
// 					minLength: 1,
// 					autoFocus: true
// 				});
// 			}
// 		}
//     }
// }

function showContentIndex(html) {
	console.log(screen_index);
    if(screen_index == 0 ){
        $("#screen_container").html(html);
    
    }else{

        /**hide the previous index */
        $('#screen'+(screen_index-1)+'').hide();
        
        /**fill the index-1a or the next screen with HTML from controller */
        $('#screen'+(screen_index-1)+'a').html(html);
        /**show the main power */
        $('#screen'+(screen_index-1)+'a').show();

    }

    return;
}

function filterReport(rpt){
	var data = {};
	// if(filterMandatory(rpt)){
		switch(rpt) {
			case C_REPORT_JOB_STATUS_FILTER:
				var fromDate = $('#txtFilterFromDate').val();
				var toDate = $('#txtFilterToDate').val();
				var jobStatus = $('#cmbStatusFilter').val();
				var customer = $('#txtCustomerFilter').val();
				var jobNo = $('#txtJobNoFilter').val();
				var coyId = $('#cmbCompanyFilter').val();
				data = {
					"type": C_REPORT_JOB_STATUS_FILTER, 
					"id": 0, 
					"status": jobStatus, 
					"custid": customer, 
					"fdate": fromDate, 
					"tdate": toDate, 
					"jobno": jobNo,
					"coy": coyId,
					"sorts": sorts,
					"index": screen_index,
				};
				console.log(data);
			break;
			case C_REPORT_JOB_BILLING_STATUS_FILTER:
				var fromDate = $('#txtFilterFromDate').val();
				var toDate = $('#txtFilterToDate').val();
				// var jobType = $('#cmbTypeFilter').val();
				// var customerId = $('#txtCustomerFilter').val();
				// var jobNo = $('#txtJobNoFilter').val();
				var coyId = $('#cmbCompanyFilter').val();
				data = {
					"type": C_REPORT_JOB_BILLING_STATUS_FILTER, 
					"id": 0, 
					// "jobtype": jobType, 
					// "custid": customerId, 
					"fdate": fromDate,
					"tdate": toDate, 
					// "jobno": jobNo,
					"coy": coyId,
					"sorts": sorts,
					"index": screen_index,
				};
			break;
			case C_REPORT_BILLING_LISTING_FILTER:
				var fromMonth = $('#cmbFromMonthFilter').val();
				var fromYear = $('#cmbFromYearFilter').val();
				var toMonth = $('#cmbToMonthFilter').val();
				var toYear = $('#cmbToYearFilter').val();
				var customerId = $('#txtCustomerFilter').val();
				var coyId = $('#cmbCompanyFilter').val();
				data = {
					"type": C_REPORT_BILLING_LISTING_FILTER,
					"id": 0, 
					"fmonth": fromMonth, 
					"fyear": fromYear, 
					"tmonth": toMonth,
					"tyear": toYear,
					"custid": customerId,
					"coy": coyId
				};
			break;
			case C_REPORT_PLAN_SCHEDULE_FILTER:
				var fromDate = $('#txtFilterFromDate').val();
				var toDate = $('#txtFilterToDate').val();
				var jobType = $('#cmbTypeFilter').val();
				var customerId = $('#txtCustomerFilter').val();
				var jobNo = $('#txtJobNoFilter').val();
				var coyId = $('#cmbCompanyFilter').val();
				data = {
					"type": C_REPORT_PLAN_SCHEDULE_FILTER, 
					"id": 0, 
					"jobtype": jobType, 
					"custid": customerId, 
					"fdate": fromDate, 
					"tdate": toDate, 
					"jobno": jobNo,
					"coy": coyId
				};
			break;
			case C_REPORT_JOB_PLAN_ACTUAL_FILTER:
				var jobType = $('#cmbTypeFilter').val();
				var customerId = $('#txtCustomerFilter').val();
				var jobNo = $('#txtJobNoFilter').val();		
				var coyId = $('#cmbCompanyFilter').val();		
				data = {
					"type": C_REPORT_JOB_PLAN_ACTUAL_FILTER, 
					"id": 0, 
					// "jobtype": jobType, 
					// "custid": customerId, 
					// "jobno": jobNo,
					"coy": coyId,
					"sorts": sorts,
					"index": screen_index,
				};
			break;
			case C_REPORT_PLAN_ACTUAL_SUMMARY_FILTER:
				var fromDate = $('#txtFilterFromDate').val();
				var toDate = $('#txtFilterToDate').val();
				var jobType = $('#cmbTypeFilter').val();
				var customerId = $('#txtCustomerFilter').val();
				var jobNo = $('#txtJobNoFilter').val();
				var coyId = $('#cmbCompanyFilter').val();
				data = {
					"type": C_REPORT_PLAN_ACTUAL_SUMMARY_FILTER, 
					"id": 0, 
					// "jobtype": jobType, 
					// "custid": customerId, 
					"fdate": fromDate, 
					"tdate": toDate, 
					// "jobno": jobNo,
					"coy": coyId,
					"sorts": sorts,
					"index": screen_index,
				};
			break;
			case C_REPORT_PLAN_ACTUAL_CONTRACT_FILTER:
				var fromDate = $('#txtFilterFromDate').val();
				var toDate = $('#txtFilterToDate').val();
				var customerId = $('#txtCustomerFilter').val();
				var jobNo = $('#txtJobNoFilter').val();
				var coyId = $('#cmbCompanyFilter').val();
				data = {
					"type": C_REPORT_PLAN_ACTUAL_CONTRACT_FILTER, 
					"id": 0, 
					"jobtype": 3, 
					"custid": customerId, 
					"fdate": fromDate, 
					"tdate": toDate, 
					"jobno": jobNo,
					"coy": coyId
				};
			break;
			case C_REPORT_AGENT_STATUS_FILTER:
				var fromDate = $('#txtFilterFromDate').val();
				var toDate = $('#txtFilterToDate').val();
				var jobStatus = $('#cmbStatusFilter').val();
				var agentId = $('#txtAgentFilter').val();
				data = {
					"type": C_REPORT_AGENT_STATUS_FILTER, 
					"id": 0,
					"status": jobStatus,
					"agentid": agentId,
					"fdate": fromDate, 
					"tdate": toDate
				};
			break;
		}
	// }
	
    var url  = "index.pzx?c=" + report_url + "&t=" + rpt  + "&d=" + new Date().getTime() ;
    callServer(url,"json",data,onResponseReport,null) ;
}
// function filterMandatory(rpt){
	// switch(rpt) {
		// case C_REPORT_JOB_PLAN_ACTUAL_FILTER:
			// var customer = $('#cmbCustomer').val();
			// var jobno = $('#txtJobNo').val();

			// if ($('#cmbCustomer').val() == "") {
				// showMessage('error', "Customer can't be blank.", "Warning");
				// return false;
			// }
			// if ($('#txtJobNo').val() == "") {
				// showMessage('error', "Job No can't be blank.", "Warning");
				// return false;
			// }
		// break;
	// }
	// return true;
// }

function filterActVsBillReport(){
    var data = { 
            "type": C_REPORT_ACTUAL_VS_BILLING_FILTER, 
            "status": 'all',
            "sorts": sorts,
            "index": screen_index,
        };
    var url  = "index.pzx?c=" + report_url + "&t=" + C_REPORT_ACTUAL_VS_BILLING_FILTER  + "&d=" + new Date().getTime() ;
    callServer(url,"json",data,onResponseReport,null) ;
    return false;
}

function showReportActVsBillDetail(id){
    showProgress("loading Job Centre Print Preview" ) ;
    screen_index++;
    var data = { "type": C_SHOW_DETAIL_ACTUAL_VS_BILLING,
                 "jobCentreId"  : id, 
                 "index": screen_index} ;
    var url  = "index.pzx?c=" + report_url + "&t=" + C_SHOW_DETAIL_ACTUAL_VS_BILLING  + "&d=" + new Date().getTime() ;
    callServer(url,"json",data,onResponseReport,null) ;
}

function printReportActVsBillReport(id) {
    var lines = "";
    var separator = "";
    $('#ActVsBillBody tr.parent').each(function() {
        /**check whether th icon is up or down  */
        line_id           = $('.tdDetailLineId',this).html();
        colapse_condition = $('.tdCollapseActVsBill',this).html();

        lines = lines +  separator + line_id + "~" + colapse_condition;

        separator = "|";

    });

    var url = "index.pzx?c=" + report_url + "&id=" + id + "&t=" + C_REPORT_ACTUAL_VS_BILLING_PRINT+"&fn=" + lines  + "&r=1";
    showReport(url) ;
}

function onResponseReport(obj,resp){
    if (resp.status != C_OK ) {
        Swal.fire('Error', resp.mesg, "error");
        return false;
    }
	
	if(resp.type == C_REPORT_AGENT_STATUS_FILTER) {
		$('#testData').html('');
		$('#testData').append(resp.data);
	} else if(resp.type == C_REPORT_ACTUAL_VS_BILLING_FILTER) {
		$('#report-act-vs-bill-table').html('');
		$('#report-act-vs-bill-table').append(resp.data);
	} else if (resp.type == C_SHOW_DETAIL_ACTUAL_VS_BILLING) {
        showScreen(resp);
        showContentIndex(resp.data.all);

        var strBtn = "<button id=\"\" class=\"btn btn-success\" onclick=\"javascript:printReportActVsBillReport("+resp.data.id+");\" title=\"Print\"><i class='fa fa-print'></i>Print</button>"+
                    "<button type=\"button\" class=\"btn btn-secondary ml-2\" onclick=\"prevscreen();\" data-dismiss=\"modal\">Close</button>";

        $('#btnActVsBillDetail').html(strBtn);

    } else if (resp.type == C_REPORT_SALES_QUOTATION_SUMMARY_FILTER) {
		var wrapper = $('#report-row-filter');
        wrapper.nextAll().remove();
        wrapper.after(resp.data.body);

		// console.log(resp.data.footer);
		var wrapper1 = $('#report-row-footer');
        wrapper1.nextAll().remove();
        wrapper1.after(resp.data.footer);
	} else {
		// console.log(resp.data);
		var wrapper = $('#report-row-filter');
        wrapper.nextAll().remove();
        wrapper.after(resp.data);
	}
}
function showReportPrint(rpt,id){
    showProgress("Loading Print Preview" ) ;
    var data = { "type": rpt,
				 "id"  : id,
				 "sorts": sorts,
				} ;
    var url  = "index.pzx?c=" + report_url + "&t=" + rpt  + "&d=" + new Date().getTime() ;
    callServer(url,"json",data,onResponseReport,null) ;
}
function showReportPrintPreview(rpt,id){
	var fromDate = $('#txtFilterFromDate').val();
	var toDate = $('#txtFilterToDate').val();
	var jobStatus = ""; //$('#cmbStatusFilter').val();
	var customerId = ""; //$('#txtCustomerFilter').val();
	var jobNo = ""; //$('#txtJobNoFilter').val();
	var jobType = ""; //$('#cmbTypeFilter').val();
	var fromMonth = $('#cmbFromMonthFilter').val();
	var fromYear = $('#cmbFromYearFilter').val();
	var toMonth = $('#cmbToMonthFilter').val();
	var toYear = $('#cmbToYearFilter').val();
	var agentId = $('#txtAgentFilter').val();
	var coyId = $('#cmbCompanyFilter').val();
	
    var url = "report.pzx?c=" + report_url + "&id=" + id + "&status=" + jobStatus + "&jobtype=" + jobType + "&custid=" + customerId + "&jobno=" + jobNo + "&fdate=" + fromDate+ "&tdate=" + toDate + "&fmonth=" + fromMonth + "&fyear=" + fromYear + "&tmonth=" + toMonth + "&tyear=" + toYear + "&agentid=" + agentId + "&t=" + rpt + "&co=" + coyId + "&r=1" + "&sorts=" + sorts;
    showReport(url) ;
}
function showReportExport(rpt){
	var fromDate = $('#txtFilterFromDate').val();
	var toDate = $('#txtFilterToDate').val();
	var jobStatus = $('#cmbStatusFilter').val();
	var customerId = ""; //$('#txtCustomerFilter').val();
	var jobNo = ""; //$('#txtJobNoFilter').val();
	var jobType = ""; //$('#cmbTypeFilter').val();
	var fromMonth = $('#cmbFromMonthFilter').val();
	var fromYear = $('#cmbFromYearFilter').val();
	var toMonth = $('#cmbToMonthFilter').val();
	var toYear = $('#cmbToYearFilter').val();
	var agentId = $('#txtAgentFilter').val();
	var coyId = $('#cmbCompanyFilter').val();
	
    var url = "report.pzx?c=" + report_url + "&id=0&status=" + jobStatus + "&jobtype=" + jobType + "&custid=" + customerId + "&jobno=" + jobNo + "&fdate=" + fromDate+ "&tdate=" + toDate + "&fmonth=" + fromMonth + "&fyear=" + fromYear + "&tmonth=" + toMonth + "&tyear=" + toYear + "&agentid=" + agentId + "&t=" + rpt + "&co=" + coyId + "&r=1" + "&sorts=" + sorts;

    showReport(url) ;
}
function expandAgentPanel(panelid){
    $('#agentPanelButton'+panelid).removeClass("fa-plus").addClass("fa-minus");
    $('#agentPanelButton'+panelid).attr('onclick','collapseAgentPanel('+panelid+')');
    $('#agentPanelHeader'+panelid).attr('onclick','collapseAgentPanel('+panelid+')');
    $('#agentPanelBody'+panelid).show();
}
function collapseAgentPanel(panelid){
    $('#agentPanelButton'+panelid).removeClass("fa-minus").addClass("fa-plus");
    $('#agentPanelButton'+panelid).attr('onclick','expandAgentPanel('+panelid+')');
    $('#agentPanelHeader'+panelid).attr('onclick','expandAgentPanel('+panelid+')');
    $('#agentPanelBody'+panelid).hide();
}

// sales quotation
function filterSalesQuotationSummary(){
    showProgress("Loading Sales Quotation Summary" ) ;
    
    var data = { 
        "type"          : C_REPORT_SALES_QUOTATION_SUMMARY_FILTER, 
        "coy"        	: $('#cmbSalesQuotationBUFilter').val(),
        "status"        : $('#cmbSalesQuotationStatusFilter').val(),
        "fdate"     	: $('#txtSalesQuotationFromDateFilter').val(),
        "tdate"       	: $('#txtSalesQuotationToDateFilter').val(),
        "date"   		: $('#cmbSalesQuotationFilterDate').val(),
		"sorts"			: sorts,
		"index"			: screen_index,
    } ;
    var url  = "index.pzx?c=" + report_url + "&t=" + C_REPORT_SALES_QUOTATION_SUMMARY_FILTER  + "&d=" + new Date().getTime() ;
    callServer(url,"json",data,onResponseReport,null) ;
}
function showSalesQuotationExport(){
    // var url = "index.pzx?c=" + report_url + "&id=0&t=" + C_REPORT_SALES_QUOTATION_SUMMARY_EXPORT+"&r=1";
	    var url = "report.pzx?c=" + report_url + "&id=0&status=" + $('#cmbSalesQuotationStatusFilter').val() + "&dt=" + $('#cmbSalesQuotationFilterDate').val() + "&fdate=" + $('#txtSalesQuotationFromDateFilter').val() + "&tdate=" +  $('#txtSalesQuotationToDateFilter').val() + "&t=" + C_REPORT_SALES_QUOTATION_SUMMARY_EXPORT + "&co=" + $('#cmbSalesQuotationBUFilter').val() + "&r=1" + "&sorts=" + sorts;
    showReport(url) ;
}
// function showReportSalesQuotationPrintPreview(rpt,id){
// 	var fromDate = $('#txtSalesQuotationFromDateFilter').val();
// 	var toDate = $('#txtSalesQuotationToDateFilter').val();
// 	var jobStatus = $('#cmbSalesQuotationStatusFilter').val();
// 	var filterDate = $('#cmbSalesQuotationFilterDate').val();
// 	var coyId = $('#cmbSalesQuotationBUFilter').val();
	
//     var url = "report.pzx?c=" + report_url + "&id=" + id + "&status=" + jobStatus + "&jobtype=" + filterDate + "&fdate=" + fromDate+ "&tdate=" + toDate + "&t=" + rpt + "&co=" + coyId + "&r=1" + "&sorts=" + sorts;
//     showReport(url) ;
// }