<?php
//include auth_session.php file on all user panel pages
include("auth_session.php");
?>
<html>
<head>
    <meta charset="utf-8"/>
    <title>PRAYATNA - User Dashboard</title>
	<link rel="stylesheet" href="user_table.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>
<div class="form">
        <p>Hey, <?php echo $_SESSION['uname']; $mobile=$_SESSION['mobile'];?></p>
        <p>Welcome to PRAYATNA' 23</p>
    </div>
<h3>Registered Events</h3>
<?php
    require('db.php');
    // When form submitted, check and create user session.
        // Check user is exist in the database
		$query    = "SELECT * FROM `event`";
        $result = mysqli_query($con, $query) or die(mysql_error());
        $rows = mysqli_num_rows($result);
		$mobile=$_SESSION['mobile'];
        if ($rows > 0) {
			echo "<table border='1'><tr><th>EVENT CODE</th><th>EVENT NAME</th><th>VENUE</th><th>DATE</th><th>START TIME</th><th>EVENT DURATION</th><th>COORDINATOR NAME</th></tr>";
			while($record = mysqli_fetch_array($result))
			{
				$sql1    = "SELECT * FROM `participant` WHERE umobile='$mobile' AND ecode=left('$record[ecode]',5)" ;
                $sqlresult1 = mysqli_query($con, $sql1) or die(mysql_error());
                $sqlrows1 = mysqli_num_rows($sqlresult1);
				if ($sqlrows1 == 1) {
	               echo '<tr>';
			       echo "<td><center>" . $record['ecode'] . "</center></td><td><center>" . $record['ename'] . "</center></td><td><center>" . $record['venue'] . "</center></td><td><center>" . $record['edate'] . "</center></td><td><center>" . $record['stime'] . "</center></td><td><center>" . $record['duration'] . "</center></td><td><center>" . $record['cname'] . "</center></td></tr>";
				}
			}
			echo "</table>";
        } else {
            echo "<div class='form'>
                  <h3>No events are added</h3>
                  </div>";
        }

		echo '<h3>To Enrol</h3>';		
        $query1    = "SELECT * FROM `event`";
        $result1 = mysqli_query($con, $query1) or die(mysql_error());
        $rows1 = mysqli_num_rows($result1);
		$mobile=$_SESSION['mobile'];
        if ($rows1 > 0) {
			echo '<form action = "user_add_event_db.php" method = "POST" name = "eventaddform">';
            echo "<table border='1'><tr><th>ADD</th><th>EVENT CODE</th><th>EVENT NAME</th><th>VENUE</th><th>DATE</th><th>START TIME</th><th>EVENT DURATION</th><th>COORDINATOR NAME</th></tr>";
			while($record1 = mysqli_fetch_array($result1))
			{
				$sql    = "SELECT * FROM `participant` WHERE umobile='$mobile' AND ecode=left('$record1[ecode]',5)" ;
                $sqlresult = mysqli_query($con, $sql) or die(mysql_error());
                $sqlrows = mysqli_num_rows($sqlresult);
				if ($sqlrows == 0) {
	               echo '<tr><td><center><input type = "checkbox" style = "inline" name ="selectadd[]" value="'.$record1['ecode'].'>"</td></center>';
			       echo "<td><center>" . $record1['ecode'] . "</center></td><td><center>" . $record1['ename'] . "</center></td><td><center>" . $record1['venue'] . "</center></td><td><center>" . $record1['edate'] . "</center></td><td><center>" . $record1['stime'] . "</center></td><td><center>" . $record1['duration'] . "</center></td><td><center>" . $record1['cname'] . "</center></td></tr>";
				}
			}
			echo "</table> <br>";
			echo '<input type = "submit" value = "Enrol Now" name = "submitadd" class="add_button">';

            echo '</form>';
        }

		echo '<h3>Deregister</h3>';		
        $query2    = "SELECT * FROM `event`";
        $result2 = mysqli_query($con, $query2) or die(mysql_error());
        $rows2 = mysqli_num_rows($result2);
		$mobile=$_SESSION['mobile'];
        if ($rows2 > 0) {
			echo '<form action = "user_delete_event_db.php" method = "POST" name = "usereventdelform">';
            echo "<table border='1'><tr><th>DELETE</th><th>EVENT CODE</th><th>EVENT NAME</th><th>VENUE</th><th>DATE</th><th>START TIME</th><th>EVENT DURATION</th><th>COORDINATOR NAME</th></tr>";
			while($record2 = mysqli_fetch_array($result2))
			{
				$sql2    = "SELECT * FROM `participant` WHERE umobile='$mobile' AND ecode=left('$record2[ecode]',5)" ;
                $sqlresult2 = mysqli_query($con, $sql2) or die(mysql_error());
                $sqlrows2 = mysqli_num_rows($sqlresult2);
				//echo "$sqlrows2";
				if ($sqlrows2 == 1) {
	               echo '<tr><td><center><input type = "checkbox" style = "inline" name ="selectdel[]" value="'.$record2['ecode'].'>"</td></center>';
			       echo "<td><center>" . $record2['ecode'] . "</center></td><td><center>" . $record2['ename'] . "</center></td><td><center>" . $record2['venue'] . "</center></td><td><center>" . $record2['edate'] . "</center></td><td><center>" . $record2['stime'] . "</center></td><td><center>" . $record2['duration'] . "</center></td><td><center>" . $record2['cname'] . "</center></td></tr>";
				}
			}
			echo "</table> <br>";
			echo '<input type = "submit" value = "Deregister Now" name = "submitdel" class="delete_button">';

            echo '</form>';
        }
?>
				 <a href="user_logout.php"> <input type = "button" value = "LOGOUT" class="user_logout_button"></a>
</body>
</html>