<?php
require './template/header.php';
require './library/database.php';
require './library/thaidate.php';

if (isset($_SESSION["session_user"]) && $_SESSION["session_user"] === true) {
	header("location: " . $app_url . "/index.php");
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
	$userid = $con->real_escape_string($_POST['userid']);
	$password = $con->real_escape_string($_POST['password']);
	$code = $con->real_escape_string($_POST['code']);

	$result = $con->query("SELECT account_id,userid FROM `login` WHERE userid='{$userid}' AND user_pass='{$password}'");

	if (empty($userid) || empty($password) || empty($code)) {
		echo '<script type="text/javascript">';
		echo 'setTimeout(function () {';
		echo 'Swal.fire("การเข้าสู่ระบบไม่สำเร็จ","กรุณาระบุข้อมูลให้ชัดเจนมากกว่านี้ !","error").then( function(result) {';
		echo 'if (result.isConfirmed) window.location.href = \'' . $app_url . '/login.php\';';
		echo '});';
		echo '}, 200);</script>';
	} elseif ($code != $_SESSION['security_code']) {
		echo '<script type="text/javascript">';
		echo 'setTimeout(function () {';
		echo 'Swal.fire("ไม่สามารถทำรายการได้","Security Code ไม่ถูกต้อง ระบุเฉพาะตัวเลขเท่านั้น","error").then( function(result) {';
		echo 'if (result.isConfirmed) window.location.href = \'' . $app_url . '/login.php\';';
		echo '});';
		echo '}, 200);</script>';
	} else {
		if ($result->num_rows == 1) {

			$row = $result->fetch_object();

			$last_ip = getIP();
			$con->query("UPDATE `login` SET lastlogin=NOW(),last_ip='{$last_ip}' WHERE account_id={$row->account_id}");

			$_SESSION["session_user"] = true;
			$_SESSION['userid'] = $row->userid;
			$_SESSION['account_id'] = $row->account_id;
			echo '<script type="text/javascript">';
			echo 'setTimeout(function () {';
			echo 'Swal.fire("การเข้าสู่ระบบสำเร็จ","ยินดีต้อนรับเข้าสู่ระบบสมาชิก","success").then( function(result) {';
			echo 'if (result.isConfirmed) window.location.href = \'' . $app_url . '/login.php\';';
			echo '});';
			echo '}, 200);  </script>';
		} else {
			echo '<script type="text/javascript">';
			echo 'setTimeout(function () {';
			echo 'Swal.fire("การเข้าสู่ระบบไม่สำเร็จ","Username / Passowrd ของคุณอาจไม่ถูกต้อง","error").then( function(result) {';
			echo 'if (result.isConfirmed) window.location.href = \'' . $app_url . '/login.php\';';
			echo '});';
			echo '}, 200);</script>';
		}
	}
}
?>
<section class="fxt-template-animation fxt-template-layout22" data-bg-image="<?= $app_url ?>/assets/img/figure/bg.jpg">
	<!-- Star Animation Start Here -->
	<div class="star-animation">
		<div id="stars4"></div>
		<div id="stars5"></div>
	</div>
	<!-- Star Animation End Here -->
	<div class="container">
		<div class="row align-items-center justify-content-md-center">
			<div class="col-6 col-12 fxt-bg-color">
				<div class="fxt-content">
					<div class="fxt-form">
						<h2>เข้าสู่ระบบสมาชิก</h2>
						<p>ลงชื่อเข้าใช้งานบัญชีของคุณ</p>
						<form action="<?= $app_url ?>/login.php" method="post">
							<div class="form-group">
								<label for="userid" class="input-label">Username ID</label>
								<input type="text" id="userid" class="form-control" name="userid" placeholder="Ex.Hear-RO" required="required">
							</div>
							<div class="form-group">
								<label for="password" class="input-label">Password</label>
								<input id="password" type="password" class="form-control" name="password" placeholder="********" required="required">
								<i toggle="#password" class="fa fa-fw fa-eye toggle-password field-icon"></i>
							</div>
							<div class="form-group">
								<div class="fxt-checkbox-area">
									<label for="pass" class="input-label">Security Code</label>
									<p><img src="<?= $app_url ?>/library/captcha.php"></p>
									<input id="number" type="pass" class="form-control" name="code" placeholder="Ex.โปรดระบุอักษรตามรูปภาพข้างบน" required="required">
								</div>
							</div>
							<div class="form-group">
								<button type="submit" class="fxt-btn-fill">เข้าสู่ระบบ</button>
							</div>
						</form>
					</div>
					<div class="fxt-style-line">
						<h3>หรือ สมัครสมาชิก</h3>
					</div>
					<ul class="fxt-socials">
						<li class="fxt-facebook"><a href="<?= $app_url ?>/register.php" title="สมัครสมาชิก">สมัครสมาชิก</a></li>
					</ul>
				</div>
			</div>
		</div>
	</div>
</section>
<?php
require './template/footer.php';
?>