<?php

include("header.php");


if (
	isset($_POST['cod_meli']) && !empty($_POST['cod_meli']) &&
	isset($_POST['name']) && !empty($_POST['name']) &&
	isset($_POST['password']) && !empty($_POST['password']) &&
	isset($_POST['txtpassword']) && !empty($_POST['txtpassword'])
) {

	$cod_meli = $_POST['cod_meli'];
	$name = $_POST['name'];
	$password = $_POST['password'];
	$txtpassword = $_POST['txtpassword'];
} else
	exit("برخی از فیلد ها مقداردهی نشده است");
if ($password != $txtpassword) {
	exit("پسورد و تکرار آن یکی نیست");
}

$link = mysqli_connect("localhost", "root", "", "shop");
if (mysqli_connect_errno())
	exit("این خطا رخ داده است" . mysqli_connect_error());

$query1 = "SELECT * FROM user WHERE cod_meli='$cod_meli'";
$user = mysqli_query($link, $query1);

$user1 = mysqli_fetch_array($user);
if ($user1) {



	if ($user1['type'] === "0" && $user1['user_type'] === "public") {
		echo ("<p style='color:green;'><b>{$user1['name']}به فروشگاه مبل هفشجان خوش آمدید .</b></p>");

		$user1['type'] = 0;
		$user1['user_type'] = "public";
		$_SESSION["state_login"] = true;
		$_SESSION["name"] = $user1['name'];
		$_SESSION["type"] = $user1['type'] = 0;
		$_SESSION["cod_meli"] = $user1['cod_meli'];
		$_SESSION["user_type"] = $user1['user_type'] = "public";
?>
		<script type="text/javascript">
			location.replace("index.php");
		</script>

	<?php
	} elseif ($user1['type'] === "1" && $user1['user_type'] === "admin") {
		echo ("<p style='color:green;'><b>{$user1['name']}به فروشگاه مبل هفشجان خوش آمدید .</b></p>");

		$user1['type'] = 1;
		$user1['user_type'] = "admin";
		$_SESSION["state_login"] = true;
		$_SESSION["name"] = $user1['name'];
		$_SESSION["type"] = $user1['type'] = 1;
		$_SESSION["user_type"] = $user1['user_type'] = "admin";
	?>
		<script type="text/javascript">
			location.replace("perodact.php");
		</script>}

<?php
	}
} else
	echo ("<p style='color:red;'><b>{$user1['name']}مشخصات وارد شده اشتباه است .</b></p>");








mysqli_close($link);




?>