$(document).ready(function(){
    
    //DEPENDABLE DROPDOWN FOR LGA FROM STATE OF ORIGIN ONLOAD
	var stateId = $('#state').val();
	$.ajax({
            type: 'POST',
            url: 'backend.php',
            data: { stateId: stateId },
            error: function(){
		        // will fire when timeout is reached
		        alert('Timeout: Please check your internet connection');
		    },
            success: function (data) {
            	$('#lga').html(data);
            },
            timeout: 50000 // sets timeout to 3 seconds
        }
    );
    
    
    
    //DEPENDABLE DROPDOWN FOR LGA FROM STATE OF ORIGIN ONLOAD
	var stateId2 = $('#state1').val();
	$.ajax({
            type: 'POST',
            url: 'backend.php',
            data: { stateId: stateId2 },
            error: function(){
		        // will fire when timeout is reached
		        alert('Timeout: Please check your internet connection');
		    },
            success: function (data) {
            	$('#lga1').html(data);
            },
            timeout: 50000 // sets timeout to 3 seconds
        }
    );


	$('.reason-div').hide();
	$('.barracks-div').hide();
	$('.interdicted-div').hide();
	$('.unit-div').hide();
	$('.role-div').hide();
	$('.unitEmolument-div').hide();
	$('.unitUser-div').hide();
	$('.barrack_allocation_div').hide();
	setTimeout(function() {
    $('#auto-close').trigger('click');
}, 5000);
});


//TERMINAL SELECTOR BASE ON COMMAND

$('#command').change(function(){
	var commandId = $('#command').val();
    $.post('../personnel/backend.php', { commandId: commandId},function(data) //send id to updating page
	 {		
		$('#unit').html(data);

	 });

});



//DEPENDABLE DROPDOWN FOR LGA FROM STATE OF ORIGIN SELECTION
$('#state').change(function(){
	var stateId = $('#state').val();
	$.ajax({
            type: 'POST',
            url: 'backend.php',
            data: { stateId: stateId },
            error: function(){
		        // will fire when timeout is reached
		        alert('Timeout: Please check your internet connection');
		    },
            success: function (data) {
            	$('#lga').html(data);
            },
            timeout: 50000 // sets timeout to 3 seconds
        }
    );
});





//DEPENDABLE DROPDOWN FOR LGA FROM STATE OF RESIDENCE SELECTION
$('#state1').change(function(){
	var stateId = $('#state1').val();
	$.ajax({
            type: 'POST',
            url: 'backend.php',
            data: { stateId: stateId },
            error: function(){
		        // will fire when timeout is reached
		        alert('Timeout: Please check your internet connection');
		    },
            success: function (data) {
            	$('#lga1').html(data);
            },
            timeout: 50000 // sets timeout to 3 seconds
        }
    );
});


//NEW LICENCE APPROVAL
$('.licenceApproval').click(function(){
   var licenceApprovalID = $(this).attr('data-licence-approvalId');
   var licenceApprovalCode = $(this).attr('data-licence-approvalCode');
    $('#licenceApprovalOprID').val(licenceApprovalID);
    $('#licenceApprovalLicenceCode').val(licenceApprovalCode);
   
});


//NEW LICENCE FINAL APPROVAL
$('.licenceFinalApproval').click(function(){
   var licenceFinalApprovalID = $(this).attr('data-licence-final-approvalId');
    $('#licenceFinalApprovalOprID').val(licenceFinalApprovalID);
   
});


//NEW LICENCE QUERY
$('.licenceQuery').click(function(){
   var licenceQueryID = $(this).attr('data-licence-queryId');
    $('#licenceQueryApplicationID').val(licenceQueryID);
   
});


//NEW LICENCE FINAL QUERY
$('.licenceFinalQuery').click(function(){
   var licenceFinalQueryID = $(this).attr('data-licence-final-queryId');
    $('#licenceFinalQueryApplicationID').val(licenceFinalQueryID);
   
});


//NEW LICENCE REJECTION
$('.licenceReject').click(function(){
   var licenceRejectID = $(this).attr('data-licence-rejectId');
    $('#licenceRejectionApplicationID').val(licenceRejectID);
   
});



//NEW LICENCE FINAL REJECTION
$('.licenceFinalReject').click(function(){
   var licenceFinalRejectID = $(this).attr('data-licence-final-rejectId');
    $('#licenceFinalRejectionApplicationID').val(licenceFinalRejectID);
   
});


//NEW DIRECTOR APPROVAL
$('.directorApproval').click(function(){
   var directorApproveID = $(this).attr('data-director-approval');
   var directorApplicationID = $(this).attr('data-director-approval-applicationId');
    $('#reviewCommentDirectorID').val(directorApproveID);
    $('#reviewCommentApplicationID').val(directorApplicationID);
   
});


//NEW DIRECTOR REJECTION
$('.directorRejection').click(function(){
   var directorRejectID = $(this).attr('data-director-rejection');
   var directorApplicationID = $(this).attr('data-director-rejection-applicationId');
    $('#reviewCommentRejectDirectorID').val(directorRejectID);
    $('#reviewCommentRejectApplicationID').val(directorApplicationID);
   
});



//NEW DOCUMENT APPROVAL
$('.documentApproval').click(function(){
   var documentApproveID = $(this).attr('data-document-approval');
    $('#reviewCommentDocumentID').val(documentApproveID);
   
});


//NEW DOCUMENT APPROVAL
$('.documentRejection').click(function(){
   var documentRejectID = $(this).attr('data-document-rejection');
    $('#reviewCommentRejectDocumentID').val(documentRejectID);
   
});



//NEW AUTH USER APPROVAL
$('.authUserApproval').click(function(){
   var authUserApprovalID = $(this).attr('data-auth-user-approve');
   var authUserOprID = $(this).attr('data-auth-user-command-approve');
   $('#reviewCommentApproveAuthUserID').val( authUserApprovalID);
   $('#reviewCommentApproveOprID').val( authUserOprID);
});


//NEW AUTH USER REJECTION
$('.authUserReject').click(function(){
   var authUserRejectID = $(this).attr('data-auth-user-reject');
   var authUserOprRejectID = $(this).attr('data-auth-user-command-reject');
   $('#reviewCommentRejectAuthUserID').val( authUserRejectID);
   $('#reviewCommentRejectOprID').val( authUserOprRejectID);
});


//NEW AUTH CARD APPROVAL
$('.authCardApproval').click(function(){
   var authCardApprovalID = $(this).attr('data-licence-approvalId');
   $('#reviewApprovalOprID').val( authCardApprovalID);
});




//NEW PAYMENT DOCUMENT APPROVAL
$('.paymentDocumentApproval').click(function(){
   var paymentDocumentApprovalID = $(this).attr('data-payment-document-approval');
   $('#reviewPaymentDocumentID').val( paymentDocumentApprovalID);
});


//NEW PAYMENT DOCUMENT APPROVAL
$('.paymentDocumentReject').click(function(){
   var paymentDocumentRejectID = $(this).attr('data-payment-document-reject');
   $('#reviewPaymentDocumentRejectID').val( paymentDocumentRejectID);
});

//Load Officer Postings to Table on Modal
$('.officerId').click(function(){
   var officerId = $(this).attr('data-officerId');
   $('#officerID').attr('value', officerId);
   	$.ajax({
            type: 'POST',
            url: 'backend.php',
            data: { officerId: officerId },
            error: function(){
		        // will fire when timeout is reached
		        alert('Timeout: Please check your internet connection');
		    },
            success: function (data) {
                //alert ('Try Again');
            	$('.tbl').html(data);
            	//$('.stockListDiv').hide();
            },
            timeout: 50000 // sets timeout to 3 seconds
        }
    );

});


//USER UPDATE

$('#barracks').change(function(){
	var barracks = $('#barracks').val();
	if (barracks == "YES") {
		$('.barracks-div').show();
		$('#dateQuartered').removeAttr('disabled');
		
	}else{
		
		$('#dateQuartered').removeAttr('required');
		$('#dateQuartered').attr('disabled', 'disabled');
		$('.barracks-div').hide();
	}
});


//Barracks & Interdicted Selector

$('#userRole').change(function(){
	var userRole = $('#userRole').val();
	if (userRole != "Officer") {
		$('.role-div').show();
		$('#roleCommand').removeAttr('disabled');
		$('#roleCommand').removeAttr('required');
		
		$('.unitUser-div').show();
		$('#unitUser').removeAttr('disabled');
		$('#unitUser').removeAttr('required');
		
	}else{
		
		$('#roleCommand').removeAttr('required');
		$('#roleCommand').attr('disabled', 'disabled');
		$('.role-div').hide();
		
		$('#unitUser').removeAttr('required');
		$('#unitUser').attr('disabled', 'disabled');
		$('.unitUser-div').hide();
	}
});

//Picture Source Selector
$('#source').change(function(){
	var source = $('#source').val();
	if (source == "Upload") {
		$('.passport-div').show();
		$('.photograph-div').hide();
	}else{
		$('.passport-div').hide();
		$('.photograph-div').show();
	}
});

//Command Selector

$('#commandPost').change(function(){
	var command = $('#commandPost').val();
	if (command == "1") {
		$('.unit-div').show();
		$('#unit').removeAttr('disabled');
		$('#unit').attr('required', 'required');
	}else{
		$('#unit').attr('disabled', 'disabled');
		$('#unit').removeAttr('required');
		$('.unit-div').hide();
	}
});


$('#commandPostEmolument').change(function(){
	var command = $('#commandPostEmolument').val();
	if (command == "1") {
		$('.unitEmolument-div').show();
		$('#unitEmolument').removeAttr('disabled');
		$('#unitEmolument').attr('required', 'required');
	}else{
		$('#unitEmolument').attr('disabled', 'disabled');
		$('#unitEmolument').removeAttr('required');
		$('.unitEmolument-div').hide();
	}
});

$('#interdicted').change(function(){
	var interdicted = $('#interdicted').val();
	if (interdicted == "YES") {
		$('.interdicted-div').show();
		$('#dateInterdicted').removeAttr('disabled');
	}else{
		
		$('#dateInterdicted').removeAttr('required');
		$('#dateInterdicted').attr('disabled', 'disabled');
		$('.interdicted-div').hide();
	}
});


//DEPENDABLE DROPDOWN FOR UNITS FROM COMMAND SELECTION
$('#commandPost').change(function(){
	var commId = $('#commandPost').val();
	$.ajax({
            type: 'POST',
            url: 'backend.php',
            data: { commId: commId },
            error: function(){
		        // will fire when timeout is reached
		        alert('Timeout: Please check your internet connection');
		    },
            success: function (data) {
            	$('#unit').html(data);
            },
            timeout: 50000 // sets timeout to 3 seconds
        }
    );
});


//DEPENDABLE DROPDOWN FOR UNITS FROM COMMAND SELECTION FROM USER RECORD UPDATE
$('#roleCommand').change(function(){
	var commId = $('#roleCommand').val();
	$.ajax({
            type: 'POST',
            url: 'backend.php',
            data: { commId: commId },
            error: function(){
		        // will fire when timeout is reached
		        alert('Timeout: Please check your internet connection');
		    },
            success: function (data) {
            	$('#unitUser').html(data);
            },
            timeout: 50000 // sets timeout to 3 seconds
        }
    );
});

//DEPENDABLE DROPDOWN FOR UNITS FROM COMMAND SELECTION
$('#commandPostEmolument').change(function(){
	var commId = $('#commandPostEmolument').val();
	$.ajax({
            type: 'POST',
            url: 'backend.php',
            data: { commId: commId },
            error: function(){
		        // will fire when timeout is reached
		        alert('Timeout: Please check your internet connection');
		    },
            success: function (data) {
            	$('#unitEmolument').html(data);
            },
            timeout: 50000 // sets timeout to 3 seconds
        }
    );
});


//DEPENDABLE DROPDOWN FOR DESIGNATION FROM DEPARTMENT SELECTION
$('#department').change(function(){
	var departmentId = $('#department').val();
	$.ajax({
            type: 'POST',
            url: 'backend.php',
            data: { departmentId: departmentId },
            error: function(){
		        // will fire when timeout is reached
		        alert('Timeout: Please check your internet connection');
		    },
            success: function (data) {
            	$('#designation').html(data);
            },
            timeout: 50000 // sets timeout to 3 seconds
        }
    );
});


//DEPENDABLE DROPDOWN FOR BARRACK LGA FROM BARRACK STATE OF ORIGIN
$('#barrackState').change(function(){
	var barrackstateId = $('#barrackState').val();
	$.ajax({
            type: 'POST',
            url: 'backend.php',
            data: { stateId: barrackstateId },
            error: function(){
		        // will fire when timeout is reached
		        alert('Timeout: Please check your internet connection');
		    },
            success: function (data) {
            	$('#barrackLGA').html(data);
            },
            timeout: 50000 // sets timeout to 3 seconds
        }
    );
});


//DEPENDABLE DROPDOWN FOR BARRACK UNIT FROM BARRACK SELECTION
$('#barrackNameSelect').change(function(){
	var barrackAllocationId = $('#barrackNameSelect').val();
	$.ajax({
            type: 'POST',
            url: 'backend.php',
            data: { barrackAllocationId: barrackAllocationId },
            error: function(){
		        // will fire when timeout is reached
		        alert('Timeout: Please check your internet connection');
		    },
            success: function (data) {
            	$('#barrackUnitSelect').html(data);
            	$('.barrack_allocation_div').hide();
            },
            timeout: 50000 // sets timeout to 3 seconds
        }
    );
});


//LOAD BARRACK AND UNIT INFORMATION TO NEW ALLOCATION MODAL
$('#barrackUnitSelect').change(function(){
	var barrackUnitSelectId = $('#barrackUnitSelect').val();
	$.ajax({
            type: 'POST',
            url: 'backend.php',
            data: { barrackUnitSelectId: barrackUnitSelectId },
            dataType: 'json',
            error: function(){
		        // will fire when timeout is reached
		        alert('Timeout: Please check your internet connection');
		    },
            success: function (data) {
            	$('#barrackCode').val(data.barrack_code);
            	$('#barrackCategory').val(data.category);
            	$('#barrackUnitName').val(data.unit_name);
            	$('#barrackUnitCode').val(data.unit_code);
            	$('#barrackUnitStatus').val(data.unit_status);
            	$('#barrackAllocationStatus').val(data.allocation_status);
            	$('#barrackState').val(data.barrack_state);
            	$('#barrackLGA').val(data.barrack_lga);
            	$('#barrackAddress').val(data.address);
            	$('.barrack_allocation_div').show();
            },
            timeout: 50000 // sets timeout to 3 seconds
        }
    );
});


//LOAD BARRACK UNIT IMAGES MODAL
$('.barrackImageId').click(function(){
 var barrackImageId = $(this).attr('data-barrackUnitImageId');
 
	$.ajax({
            type: 'POST',
            url: 'backend.php',
            data: { barrackImageId: barrackImageId },
            dataType: 'json',
            error: function(){
		        // will fire when timeout is reached
		        alert('Timeout: Please check your internet connection');
		    },
            success: function (data) {
            	$('#image1').attr('src', '../barrackImages/' + data.image3);
            	$('.imageTitle1').html(data.imageTitle1);
            	$('#image2').attr('src', '../barrackImages/' + data.image2);
            	$('.imageTitle2').html(data.imageTitle2);
            	$('#image3').attr('src', '../barrackImages/' + data.image1);
            	$('.imageTitle3').html(data.imageTitle3);
            	$('#image4').attr('hrsrcef', 'barrackImages/' + data.image4);
            	$('.imageTitle4').html(data.imageTitle4);
            	$('#image5').attr('src', '../barrackImages/' + data.image5);
            	$('.imageTitle5').html(data.imageTitle5);
            	$('.image6').attr('src', '../barrackImages/' + data.image6);
            	$('.imageTitle6').html(data.imageTitle6);
            	$('.image7').attr('src', '../barrackImages/' + data.image7);
            	$('.imageTitle7').html(data.imageTitle7);
            	$('.image8').attr('src', '../barrackImages/' + data.image8);
            	$('.imageTitle8').html(data.imageTitle8);
            },
            timeout: 50000 // sets timeout to 3 seconds
        }
    );
});