var C_JOB_DETAIL_VIEW	      		= "2505";
var C_JOB_CHECKLIST_VIEW     		= "2506";
var C_JOB_SERVICE_REPORT_VIEW  		= "2507";
var C_JOB_ATTACH_VIEW  				= "2508";
var C_ACCEPT_JOB		  			= "2509";
var C_JOB_SERVICE_START				= "2510";
var C_JOB_SERVICE_END				= "2511";
var C_JOB_SERVICE_SUBMIT			= "2512";
var C_JOB_CHECKLIST_UPDATE 			= "2513";
var C_JOB_CHECKLIST_REMARK	 		= "2514";
var C_JOB_ATTACH_UPLOAD 			= "2515";
var C_JOB_SERVICE_PICTURE 			= "2516";
var C_JOB_SERVICE_PICTURE_REMOVE	= "2517";
var C_JOB_SAVE_ADDITIONAL 			= "2518";

var record_obj = null ;
var record_id = 0;
var upload_form = null;
var m_filename = "";
var m_file = "";

$(document).ready(() => {
});

function plusHoursChange() {
	if($('#chkPlusHours').is(":checked")) {
		$('#txtPlusHours').prop("disabled","");
		$('#txtPlusDate').prop("disabled","");
	} else {
		$('#txtPlusHours').prop("disabled","disabled");
		$('#txtPlusDate').prop("disabled","disabled");
	}
}

function checkEmailNotification() {
	if($('#chkEmailNotif').is(':checked')){
        $('#btnSendEmail').show();
    }else{
        $('#btnSendEmail').hide();
    }
}

function changeAdditionalFooter() {
	if($('#cmbFooterInfId').val()!=0){
        $('#txtareaFooterInfDesc').val($('#cmbFooterInfId :selected').text());
    }else{
        $('#txtareaFooterInfDesc').val("");
    }
}

function showPrintPreview(id, checklist, additional) {
	var url = "index.pzx?c=" + agent_url + "&id=" + id + "~" + checklist + "~" + additional +  "&t=" + "SHOW_PRINT_PREVIEW" + "&r=1";
    showReport(url) ;
}

function saveAdditional(id) {
	var data = { 
					"type": C_JOB_SAVE_ADDITIONAL,
					"id": id,
					"send_email": ($('#chkEmailNotif').is(':checked') ? '1' : '0'),
					"include_additional": ($('#chkAdditionalInfo').is(':checked') ? '1' : '0'),
					"include_checklist": ($('#chkIncludeChecklist').is(':checked') ? '1' : '0'),
					"footer_id": $('#cmbFooterInfId').val(),
					"footer_desc": $('#txtareaFooterInfDesc').val(),
					"additional_email": $('#txtAdditionalEmail').val()
				} ;
	var url = "request.pzx?c=" + agent_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,onResponseUpdate,null) ;
}

function sendEmailQuotation(id,obj) {
    var that = obj;
    $(obj).attr("disabled", true);
    setTimeout(function() { enableSubmitEmailMobile(id,that) }, 5000);
    executeSendEmailMobile(id) ;

};

var enableSubmitEmailMobile = function(id,ele) {
    $(ele).removeAttr("disabled");
    
}

function executeSendEmailMobile(id) {
    var data = { "type": "MOBILE_SEND_EMAIL", "assign_id":id} ;
    var url = "request.pzx?c=" + agent_url + "&d=" + new Date().getTime() ;      
    callServer(url,"json",data,onEmailMobileResponse,record_obj) ;
}

function onEmailMobileResponse(obj,resp) {
    if (resp.status == C_OK) {
        if(resp.type == "MOBILE_SEND_EMAIL"){
            swal("Successfull!", resp.mesg, "success");
        }       
    } else {
        swal("Error!", resp.mesg, "error");
        return false;
    }
}

function removeJobServicePicture() {
	var data = { 
		"type": C_JOB_SERVICE_PICTURE_REMOVE,
		"id": record_id,
		"serviceid": $('#txtServiceId').val(),
		"pictureid": $('#txtPictureId').val(),
		"src": $("#enlarge_photo").attr("src")
	} ;
	var url = "request.pzx?c=" + agent_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,onResponseUpdate,null) ;
}
function getJobServicePicture(serviceid) {
	var data = { 
		"type": C_JOB_SERVICE_PICTURE,
		"id": record_id,
		"serviceid": serviceid
	} ;
	var url = "request.pzx?c=" + agent_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,onResponseUpdate,null) ;
}
function onUploadEnd(resp) {
	var s = resp.split("|") ;
	if (s[0]==C_OK) {
		$('#txtUploadPhoto').val("") ;
		m_file = s[1] ;
		
		var data = {
		"type": C_JOB_ATTACH_UPLOAD,
		"id": s[3],
		"serviceid": s[4],
		"photo": m_file
		} ;
		var url = "request.pzx?c=" + agent_url + "&d=" + new Date().getTime() ;
		callServer(url,"json",data,onResponseUpdate,record_obj);
	}
	else {
		alert(s[1]) ;
		m_file = "" ;
	}
}
function uploadPhoto(serviceid) {
	var fn = $('#txtUploadPhoto').val() ;
	if (fn == "") {
		alert('You must select a file for upload.') ;
		return false;
	} else {
		//$('#btnUploadPhoto').prop('disabled','disabled') ;
		var url = "upload.pzx?type=" + C_JOB_ATTACH_UPLOAD + "&t=p&n=txtUploadPhoto&i=" + record_id + "&sid=" + serviceid + "&fn=" + m_filename + "&d=" + new Date().getTime() ;	
		uploaded = false ;	
		ajaxUpload(upload_form,url,onUploadEnd);
	}
}
function onchangedPhoto(obj) {
	upload_form = obj ;
}
function addPhoto(serviceid) {
	$("#btnUploadPhoto").attr("onclick","uploadPhoto("+serviceid+");");
}
function enlargePhoto(serviceid,pictureid,src,type) {
	if(type == 0) {
		$("#btnRemovePhoto").hide();
	} else {
		$("#btnRemovePhoto").show();
	}
	$("#txtPictureId").val(pictureid);
	$("#txtServiceId").val(serviceid);
	$("#enlarge_photo").attr("src","");
	$("#enlarge_photo").attr("src",src);
}
function showModalRemark(serviceid,checklistid) {
	$("#btnSaveRemark").attr("onclick","updateChecklistRemark("+serviceid+","+checklistid+");");
	$("#txtChecklistId").val(checklistid);
	$("#txtChecklistRemark").val($("#lblChecklistRemark"+checklistid).text());
}
function updateServiceChecklist(serviceid,checklistid) {
	var check = 0;
	if($('#chkChecklist'+checklistid).is(":checked"))
		check = 1;
	var data = {
		"type": C_JOB_CHECKLIST_UPDATE,
		"serviceid": serviceid,
		"checklistid": checklistid,
		"check": check
		} ;
	var url = "request.pzx?c=" + agent_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,onResponseUpdate,record_obj);
}
function updateChecklistRemark(serviceid,checklistid) {
	var data = {
		"type": C_JOB_CHECKLIST_REMARK,
		"id": record_id,
		"serviceid": serviceid,
		"checklistid": checklistid,
		"remark": $('#txtChecklistRemark').val()
		} ;
	var url = "request.pzx?c=" + agent_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,onResponseUpdate,record_obj);
}
function startJobService(id) {
	record_id = id;
	var data = {
		"type": C_JOB_SERVICE_START,
		"id": id,
		"jobstart": $('#txtServiceStart').val()
		} ;
	var url = "request.pzx?c=" + agent_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,onResponseUpdate,record_obj);
}
function endJobService(serviceid) {
	var data = {
		"type": C_JOB_SERVICE_END,
		"serviceid": $('#serviceid').val(),
		"servicedesc": $('#txtServiceDesc').val(),
		"adddesc": $('#txtServiceAdditional').val(),
		"signname": $('#txtSignName').val(),
		"billable": ($('#chkBillable').is(":checked") ? 1:0),
		"plushours": ($('#chkPlusHours').is(":checked") ? 1:0),
		"addhours": $('#txtPlusHours').val(),
		"adddate": $('#txtPlusDate').val()
		} ;
	var url = "request.pzx?c=" + agent_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,onResponseUpdate,record_obj);
}
function submitJobService(obj) {
	var mycanvas = document.getElementById('canvas');
	var img = mycanvas.toDataURL("image/png");
	anchor = $("#signature");
	anchor.val(img);
	var data = {
		"type": C_JOB_SERVICE_SUBMIT,
		"id": record_id,
		"serviceid": $('#serviceid').val(),
		"servicedesc": $('#txtServiceDesc').val(),
		"adddesc": $('#txtServiceAdditional').val(),
		"signname": $('#txtSignName').val(),
		"signature": img,
		"billable": ($('#chkBillable').is(":checked") ? 1:0),
		"plushours":($('#chkPlusHours').is(":checked") ? 1:0),
		"addhours": $('#txtPlusHours').val(),
		"adddate": $('#txtPlusDate').val()
		} ;
	var url = "request.pzx?c=" + agent_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,onResponseUpdate,record_obj);
}
function acceptJob(id) {
	record_id = id;
	var data = {
		"type": C_ACCEPT_JOB,
		"id": id
		} ;
	var url = "request.pzx?c=" + agent_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,onResponseUpdate,record_obj);
}
function onResponseUpdate(obj, resp) {
	if (resp.status != C_OK) {
		alert(resp.mesg);
		return false;
	}
	
	if(resp.type == C_ACCEPT_JOB) {
		getMyJobList();
		getAssignedJobList();
	} else if(resp.type == C_JOB_SERVICE_START) {
		// console.log(resp.data);
		$("#detailid").val(record_id);
		$("#serviceid").val(resp.data);
		$("#btnStartService").prop("disabled","disabled");
		$("#txtServiceEnd").prop("disabled","");
		$("#btnEndService").prop("disabled","");
		$("#btnEndService").attr("onclick","endJobService("+resp.data+"); return false;");
		$("#btnSubmitService").prop("disabled","");
		$("#reset-btn").prop("disabled","");
		$("#btnJobChecklist").attr("onclick","viewJobChecklist("+record_id+"); return false;");
		$('#chkBillable').prop("disabled","");
		$('#chkPlusHours').prop("disabled","");
	} else if(resp.type == C_JOB_SERVICE_END) {
		$("#btnEndService").prop("disabled","disabled");
		$("#btnSubmitService").prop("disabled","");
		$("#reset-btn").prop("disabled","");
		$("#txtServiceEnd").val(resp.data.serviceEnd);
		$('#chkBillable').prop("disabled","");
		$('#chkPlusHours').prop("disabled","");
		// $('#chkBillable').prop("checked",resp.data.billable);
		// $('#chkPlusHours').prop("checked",resp.data.plushours);
		// $('#txtPlusHours').val(resp.data.additionalHours);
		// $('#txtPlusDate').val(resp.data.additionalDate);
	} else if(resp.type == C_JOB_SERVICE_SUBMIT) {
		openSite("index.pzx?c=" + home_url + "&d=" + new Date().getTime());
		//getMyJobList();
		//getAssignedJobList();
	} else if(resp.type == C_JOB_CHECKLIST_UPDATE) {
		viewJobChecklist(record_id);
	} else if(resp.type == C_JOB_CHECKLIST_REMARK) {
		viewJobChecklist(record_id);
	} else if(resp.type == C_JOB_ATTACH_UPLOAD) {
		getJobServicePicture(resp.data);
	} else if(resp.type == C_JOB_SERVICE_PICTURE) {
		$("#service_attach").html('');
		$("#service_attach").append(resp.data.serviceAttach);
	} else if(resp.type == C_JOB_SERVICE_PICTURE_REMOVE) {
		getJobServicePicture(resp.data);
	} else if(resp.type == C_JOB_SAVE_ADDITIONAL) {
		if (resp.status == C_OK) {
			swal("Successfull!", "Your Item Successfully updated to the system", "success");
		}
		viewJobAttach(resp.data.id);
		checkEmailNotification();
	}
}
function viewJobDetails(id) {
	if(id == 0) {
		return false;
	} else {
		record_id = id;
		var data = {
			"type": C_JOB_DETAIL_VIEW,
			"id": id
			} ;
		var url = "request.pzx?c=" + agent_url + "&d=" + new Date().getTime() ;
		callServer(url,"json",data,onResponseView,record_obj);
	}
}
function viewJobChecklist(id) {
	if(id == 0) {
		return false;
	} else {
		record_id = id;
		var data = {
			"type": C_JOB_CHECKLIST_VIEW,
			"id": id
			} ;
		var url = "request.pzx?c=" + agent_url + "&d=" + new Date().getTime() ;
		callServer(url,"json",data,onResponseView,record_obj);
	}
}
function viewJobServiceReport(id) {
	if(id == 0) {
		return false;
	} else {
		record_id = id;
		var data = {
			"type": C_JOB_SERVICE_REPORT_VIEW,
			"id": id
			} ;
		var url = "request.pzx?c=" + agent_url + "&d=" + new Date().getTime() ;
		callServer(url,"json",data,onResponseView,record_obj);
	}
}
function viewJobAttach(id) {
	if(id == 0) {
		return false;
	} else {
		record_id = id;
		var data = {
			"type": C_JOB_ATTACH_VIEW,
			"id": id
			} ;
		var url = "request.pzx?c=" + agent_url + "&d=" + new Date().getTime() ;
		callServer(url,"json",data,onResponseView,record_obj);
	}
}
function onResponseView(obj, resp) {
	if (resp.status != C_OK) 
		return false;
	
	$("#kt_content").html(resp.data.htmlContent);
	$("#kt_subheader_title").html(resp.data.headTitle);
	
	$("#btnJobDetails").attr("onclick","viewJobDetails("+record_id+"); return false;");
	
	if(resp.data.agentStatus) {
		$("#btnJobChecklist").attr("onclick","viewJobChecklist("+record_id+"); return false;");
		$("#btnJobServiceReport").attr("onclick","viewJobServiceReport("+record_id+"); return false;");
		$("#btnJobAttach").attr("onclick","viewJobAttach("+record_id+"); return false;");
	}
	
	if(resp.type == C_JOB_DETAIL_VIEW) {
		record_id = resp.data.id;
		$("#txtJobId").val(resp.data.id);
		$("#txtJobNumber").val(resp.data.jobNo);
		$("#txtJobStartDate").val(resp.data.jobDate);
		$("#txtLocationName").val(resp.data.custLocCode);
		// $("#txtLocationName").val(resp.data.custLocName);
		$("#txtLocationPhone").val(resp.data.custLocPhone);
		$("#txtLocationContact").val(resp.data.custLocContact);
		$("#txtLocationEmail").val(resp.data.custLocEmail);
		$("#txtLocationAddress").val(resp.data.custLocAddress);
		$("#lblJobTask").text(resp.data.jobTask);
		$("#txtJobDesc").val(resp.data.jobScope);
		$("#txtJobHours").val(resp.data.jobHours);
		$("#job_attach").html('');
		$("#job_attach").append(resp.data.jobAttach);
		$("#btnJobDetails").focus();
	} else if(resp.type == C_JOB_CHECKLIST_VIEW) {
		$("#job_checklist").html('');
		if(resp.data.checklist != "") {
			$("#job_checklist").append(resp.data.checklist);
		} else {
			alert("No checklist assigned to this job.");
			viewJobDetails(record_id);
		}
		$("#btnJobChecklist").focus();
	} else if(resp.type == C_JOB_SERVICE_REPORT_VIEW) {
		$("#detailid").val(record_id);
		$("#serviceid").val(resp.data.serviceId);
		$("#txtJobDesc").val(resp.data.jobScope);
		$("#txtServiceStart").val(resp.data.serviceStart);
		$("#txtServiceDesc").val(resp.data.serviceDesc);
		$("#txtServiceAdditional").val(resp.data.serviceAddDesc);
		$("#txtServiceEnd").val(resp.data.serviceEnd);
		$("#txtSignName").val(resp.data.signName);
		$("#btnStartService").attr("onclick","startJobService("+record_id+");");
		$("#btnStartService").prop("disabled",resp.data.startDisable);
		$("#btnEndService").attr("onclick","endJobService("+resp.data.serviceId+");");
		$("#btnEndService").prop("disabled",resp.data.endDisable);
		// $("#btnSubmitService").attr("onclick","submitJobService("+resp.data.serviceId+");");
		$("#btnSubmitService").prop("disabled",resp.data.submitDisable);
		$("#reset-btn").prop("disabled",resp.data.submitDisable);
		$("#btnAddPhoto").attr("onclick","addPhoto("+resp.data.serviceId+"); return false;");
		$("#service_attach").html('');
		$("#service_attach").append(resp.data.serviceAttach);
		
		if(resp.data.serviceId != 0) {
			$('#chkBillable').prop("disabled","");
			$('#chkPlusHours').prop("disabled","");
			$('#chkBillable').prop("checked",resp.data.billable);
			$('#chkPlusHours').prop("checked",resp.data.plusHours);
			$('#txtPlusHours').val(resp.data.additionalHours);
			$('#txtPlusDate').val(resp.data.additionalDate);
			plusHoursChange();
		}
		
		$("#btnJobServiceReport").focus();
	} else if(resp.type == C_JOB_ATTACH_VIEW) {
		$("#btnJobAttach").focus();

		if(resp.data.send_email==1) {
			$("#chkEmailNotif").prop('checked', true);
		} else {
			$('#chkEmailNotif').prop('checked', false);
		}

		if(resp.data.include_additional==1) {
			$("#chkAdditionalInfo").prop('checked', true);
		} else {
			$('#chkAdditionalInfo').prop('checked', false);
		}

		if(resp.data.include_checklist==1) {
			$("#chkIncludeChecklist").prop('checked', true);
		} else {
			$('#chkIncludeChecklist').prop('checked', false);
		}
		
		$("#cmbFooterInfId").val(resp.data.footer_id);
		$("#txtareaFooterInfDesc").val(resp.data.footer_desc);
		$("#txtAdditionalEmail").val(resp.data.additional_email);
		checkEmailNotification();
		//$("#btnUploadPhoto").attr("onclick","uploadPhoto("+resp.data.serviceId+");");
	}
}