<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>PRAYATNA - Admin - Add Coord</title>
	<link rel="stylesheet" href="admin_style.css">
	<link href="https://fonts.googleapis.com/css2?family=Asap+Condensed&display=swap" rel="stylesheet">
	<link href="https://fonts.googleapis.com/css2?family=Orbitron:wght@500&display=swap" rel="stylesheet"><link href="https://fonts.googleapis.com/css2?family=Arizonia&display=swap" rel="stylesheet">
</head>
<body>
<?php
    require('db.php');
    // When form submitted, insert values into the database.
    if (isset($_REQUEST['mobile'])) {
        // removes backslashes
        $cname = stripslashes($_REQUEST['cname']);
        //escapes special characters in a string
        $cname = mysqli_real_escape_string($con, $cname);
		$mobile = stripslashes($_REQUEST['mobile']);
        $mobile = mysqli_real_escape_string($con, $mobile);
		$password= stripslashes($_REQUEST['password']);
        $password = mysqli_real_escape_string($con, $password);
		$query    = "SELECT * FROM `coord` WHERE mobile='$mobile'";
        $result = mysqli_query($con, $query) or die(mysql_error());
        $rows = mysqli_num_rows($result);
		//echo $rows;
        if ($rows == 0) {
			$query    = "INSERT into `coord` (cname, mobile, password)
						 VALUES ('$cname','$mobile','" . md5($password) . "')";
			$result   = mysqli_query($con, $query);
			if ($result) {
				echo "<div class='form'>
					 <!--<h3>Coordinator added successfully.</h3><br/>
					  //<p class='link'>Click here to <a href='admin_dashboard.php'>Go to Admin Dashboard</a></p>--->
					  </div>";
					  header("Location: admin_add_coord.php");
			} else {
				echo "<div class='form'>
					  <h3>Required fields are missing.</h3><br/>
				</div>";
				?>
				<a href="admin_add_coord.php"> <input type = "button" value = "ADD COORD" class="admin_login_button"></a><p></p>
				<a href="admin_dashboard.php"> <input type = "button" value = "ADMIN DASHBOARD" class="admin_login_button"></a>
				
				<?php
					  
            }
		}
		
		else {
			    echo "<div class='form'>
				<h3>Coordinator already exists.</h3><br/>
				
				</div>";
				?>
				<a href="admin_add_coord.php"> <input type = "button" value = "ADD COORD" class="admin_login_button"></a><p></p>
				<a href="admin_dashboard.php"> <input type = "button" value = "ADMIN DASHBOARD" class="admin_login_button"></a>
				<?php
		}
    } else {
?>

    <form class="admin_login_form" action="" method="post">
        <h1 class="login-title">ADD COORD</h1>
        <input type="text" id="cname" name="cname" placeholder="Coordinator Name" required>
		<br><br><br>
        <input type="tel" id="mobile" name="mobile" placeholder="Mobile Number" pattern="[0-9]{10}" required>
		<br><br><br>
        <input type="text" id="password" name="password" placeholder="Password" required>
		<br><br><br>
        <input type="submit" name="submit" value="ADD COORD" class="admin_login_button"><p></p>
		<a href="admin_dashboard.php"> <input type = "button" value = "ADMIN DASHBOARD" class="admin_login_button"></a>
    </form>
<?php
    }
?>
</body>
</html>