<div class="row">
	<?php include APPPATH . "views/backend/dashboard/admin_inc/alert_info.php"; ?>
</div>


<?php
$google_client_id = $this->social_auth->get('google')['client_id'];
?>


<?php if (USER_ROLE == 1): ?>
	<?php include 'inc/admin_info.php'; ?>
<?php endif; ?>



<?php include "inc/users_info.php"; ?>


<div class="modal fade" id="notificationModal">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
				<h4 class="modal-title"><?= lang('send_notification'); ?></h4>
			</div>
			<form action="<?= base_url("admin/notify/send?action=admin"); ?>" method="POST">
				<?= csrf(); ?>
				<div class="modal-body">
					<div class="form-group">
						<label> <?= lang('notification_list'); ?></label>
						<select name="notification_id" id="notify_id" class="form-control">
							<option value=""><?= lang('select_notification'); ?></option>
							<?php foreach ($notification_list as $key => $notify): ?>
								<option value="<?= $notify['id']; ?>"><?= $notify['title']; ?></option>
							<?php endforeach; ?>
						</select>
					</div>
					<input type="hidden" name="restaurant_id[]" value="<?= $restaurant['id']; ?>">
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal"><?= lang('close'); ?></button>
					<button type="submit" class="btn btn-primary"><?= lang('send'); ?></button>
				</div>
			</form>
		</div>
	</div>
</div>


<div class="modal fade" id="usernameModal">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
				<h4 class="modal-title"><?= lang('username'); ?></h4>
			</div>
			<form action="<?= base_url("admin/auth/change_username"); ?>" method="POST">
				<?= csrf(); ?>
				<div class="modal-body">
					<div class="form-group">
						<label> <?= lang('username'); ?></label>
						<input type="text" name="username" id="username" class="form-control"
							value="<?= $auth_info['username']; ?>">
						<span class="alert_msg mt-5"></span>
					</div>
					<input type="hidden" name="id" value="<?= $auth_info['id']; ?>">
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal"><?= lang('close'); ?></button>
					<button type="submit" class="btn btn-primary reg_btn" disabled><?= lang('save_change'); ?></button>
				</div>
			</form>
		</div>
	</div>
</div>











<?php if (!empty($google_client_id)): ?>
	<script>
		let googleClient;

		window.onload = function () {
			if (typeof google === 'undefined') return;

			googleClient = google.accounts.oauth2.initCodeClient({
				client_id: '<?= htmlspecialchars($google_client_id) ?>',
				scope: 'openid email profile',
				ux_mode: 'popup',
				callback: ({
					code
				}) => code && handleAuthCodeResponse(code),
			});

			document.getElementById('custom-google-login-btn')
				?.addEventListener('click', (e) => {
					e.preventDefault();
					googleClient.requestCode();
				});
		};

		function showError(msgArea, message) {
			if (msgArea) {
				msgArea.innerHTML = `<div class="alert alert-danger mt-2">${message}</div>`;
				msgArea.scrollIntoView({
					behavior: 'smooth',
					block: 'center'
				});
			} else {
				alert(message);
			}
		}

		function handleAuthCodeResponse(code) {
			const card = document.querySelector('.modern-login-card, .modern-card');
			const msgArea = document.querySelector('.reg_msg');

			const formData = new FormData();
			formData.append('code', code);
			formData.append('<?= $this->security->get_csrf_token_name() ?>', '<?= $this->security->get_csrf_hash() ?>');

			card?.classList.add('load');
			if (msgArea) msgArea.innerHTML = ''; // clear previous errors

			fetch('<?= base_url("login/google_callback") ?>', {
				method: 'POST',
				body: formData
			})
				.then(res => {
					if (!res.ok) throw new Error(`Server error: ${res.status} ${res.statusText}`);
					return res.json();
				})
				.then(({ st, url, msg }) => {
					card?.classList.remove('load');

					if (st === 1) {
						window.location.href = url;
					} else {
						// Server responded but auth failed — show the actual server message
						showError(msgArea, msg ?? 'Authentication failed. Please try again.');
					}
				})
				.catch(err => {
					card?.classList.remove('load');
					console.error('Google auth error:', err);

					// Network failure or non-2xx HTTP — show a generic connectivity message
					showError(msgArea, 'Could not connect to the server. Please check your connection and try again.');
				});
		}
	</script>
<?php endif; ?>