<?php
    include "head.php" ;
    session_start();
    date_default_timezone_set("Asia/Kolkata");

    if(isset($_SESSION['user_id']) && isset($_SESSION['user_name']) && $_SESSION['user_cate'] === "user"){
        $user_id = $_SESSION['user_id'] ;
        $username = $_SESSION['user_name'] ;
        $email = $_SESSION['user_email'] ;
        $phone = $_SESSION['user_phone'] ;
        $user_image = $_SESSION['user_image'] ;
        echo $user_image;
    }else{
        header("Location:login.php") ;
    }
?>

<?php  
    $nameErr = $fnameErr = $dobErr = $genderErr = $countryErr = $schoolErr= $inErr= $stateErr = $cityErr = $addressErr = $emailErr = $mobileErr = "" ; 
    $name = $fname = $dob = $gender = $country = $state = $school_name= $name_initial= $city = $address = $email = $phone = "" ;
    $result = array();
    if(isset($_POST['submit'])){
        $user_id = $_POST['user_id'] ;
        // name
        if(empty($_POST['name'])){
            $nameErr = "Name is required" ;
            $result['name'] = "Name is required" ;
        }else{
            $name = $config->real_escape_string(strtoupper($_POST['name']));
        }
        if(empty($_POST['name_initial'])){
        $inErr = "Initial is required" ;
        $result['name_initial'] = "Initial is required" ;
        }else{
        $name_initial = $config->real_escape_string(strtoupper($_POST['name_initial']));
       }
       if(empty($_POST['school_name'])){
        $schoolErr = "School Name is required" ;
        $result['school_name'] = "School Name is required" ;
       }else{
        $school_name = $config->real_escape_string(strtoupper($_POST['school_name']));
       }
        // fname
        if(empty($_POST['fname'])){
            $fnameErr = "Name is required" ;
            $result['fname'] = "Name is required" ;
        }else{
            $fname = $config->real_escape_string($_POST['fname']);
        }
        // dob
        if(empty($_POST['dob'])){
            $dobErr = "Date of Birth is required" ;
            $result['dob'] = "dob is required" ;
        }else{
            $dob = $config->real_escape_string($_POST['dob']);
        }
        // Gender
        if(empty($_POST['gender'])){
            $genderErr = "Gender is required" ;
            $result['gender'] = "Gender is required" ;
        }else{
            $gender = $config->real_escape_string($_POST['gender']);
        }
        // Country
        if(empty($_POST['country'])){
            $countryErr = "Country is required" ;
            $result['country'] = "country is required" ;
        }else{
            $country = $config->real_escape_string($_POST['country']);
        }
        // state
        if(empty($_POST['state'])){
            $stateErr = "State is required" ;
            $result['state'] = "state is required" ;
        }else{
            $state = $config->real_escape_string($_POST['state']);
        }
        // City
        if(empty($_POST['city'])){
            $cityErr = "City is required" ;
            $result['city'] = "city is required" ;
        }else{
            $city = $config->real_escape_string($_POST['city']);
        }
        // Address
        if(empty($_POST['address'])){
            $addressErr = "Address is required" ;
            $result['address'] = "address is required" ;
        }else{
            $address = $config->real_escape_string($_POST['address']);
        }
        // email
        if(empty($_POST['email'])){
            $emailErr = "Email is required" ;
            $result['email'] = "Email is required" ;
        }else{
            $check_email = $config->real_escape_string($_POST['email']);
            if(!filter_var($check_email,FILTER_VALIDATE_EMAIL)){
                $emailErr = "Invalid email format";
                $result['email'] = "Invalid email format" ;
            }else{
                $email = $check_email ;
            }
        }
        // mobile
        if(empty($_POST["phone"])) {
            $phoneErr = "Mobile Number is required";
            $result['phone'] = "Mobile Number is required" ;
        } else {
            $check_phone = $config->real_escape_string($_POST['phone']);
            $phone_len = strlen($check_phone);
            if($phone_len == 10){
                $phone = $check_phone ;
            }else{
                $phoneErr = "Invalid Mobile number" ;
                $result['phone'] = "Invalid Mobile number" ;
            }
        }

        $fide_id = $config->real_escape_string($_POST['fide_id']);
        $aicf_id = $config->real_escape_string($_POST['aicf_id']);
        $tnsca_id = $config->real_escape_string($_POST['tnsca_id']);
        $chess_id = $config->real_escape_string($_POST['chess_id']);
        $lichess_id = $config->real_escape_string($_POST['lichess_id']);
        $date = date('Y-m-d',strtotime('today'));

        if(count($result) == 0 ){
            $sql = "INSERT INTO players(user_id, name, f_name, dob, gender, country, state, city, address, email, phone, fide_id, aicf_id, tnsca_id, chess_id, lichess_id, status, created_at,name_initial,school_name)
                VALUES('$user_id', '$name', '$fname', '$dob', '$gender', '$country', '$state', '$city', '$address' , '$email', '$phone', '$fide_id', '$aicf_id', '$tnsca_id', '$chess_id', '$lichess_id', 1, '$date','$name_initial','$school_name') ";
            if($config->query($sql) === TRUE){
                $name = $fname = $dob = $gender = $name_initial= $school_name= $country = $state = $city = $address = $check_email = $check_phone = $fide_id = $aicf_id = $tnsca_id = $chess_id = $lichess_id = "" ;
                $_SESSION['status'] = "Added Successfully";
    		    $_SESSION['alert'] = "success";
    		    
            }else{
                $_SESSION['status'] = "Failed";
    		    $_SESSION['alert'] = "error";
            }
        }
    }
    
    
?>

	<body>

		<!-- Main Wrapper -->
		<div class="main-wrapper">
		
			<!-- Header -->
			<?php include "header.php" ; ?>
			<!-- /Header -->
			
			<!-- Breadcrumb -->
			<?php include "breadcrumbuser.php" ; ?>
			<!-- /Breadcrumb -->
			
			<!-- Page Content -->
			<div class="content">
				<div class="container-fluid">

					<div class="row">
						<div class="col-md-5 col-lg-4 col-xl-3 theiaStickySidebar">
						
							<!-- Profile Sidebar -->
							<?php include "sidebar.php" ; ?>
							<!-- /Profile Sidebar -->
							
						</div>
						<div class="col-md-7 col-lg-8 col-xl-9">
						    <div class="d-flex justify-content-between align-items-start">
							    <h4 class="card-title">Add Players</h4>
							    <a href="players-list.php" class="btn btn-sm bg-info-light">View</a>
							</div>
						    <?php
						        $pro_sql = "SELECT * FROM v_profile WHERE v_id = '$user_id' " ;
						        $pro_res = $config->query($pro_sql);
						        $pro = $pro_res->fetch_assoc();
						    ?>
						    <form method="post" enctype="multipart/form-data">
    							<!-- Basic Information -->
    							<div class="card">
    								<div class="card-body">
    								    <h4 class="card-title">Personal Details</h4>
        								<div class="row form-row">
    										<div class="col-md-4">
    											<div class="form-group">
    												<label for="name">Player Name<span class="text-danger">*</span></label>
    												<input type="text" class="form-control" name="name" id="name" value="<?= $name ?>" >
    											    <span class="text-danger py-2"><?= $nameErr ?></span>
    											</div>
    										</div>
                                                <div class="col-md-2">
                                                <div class="form-group">
                                                    <label for="name">Name Initial<span class="text-danger">*</span></label>
                                                    <input type="text" class="form-control" name="name_initial" id="initial" value="<?= $name_initial ?>" >
                                                    <span class="text-danger py-2"><?= $inErr ?></span>
                                                </div>
                                            </div>
                                           <div class="col-md-6">
                                                <div class="form-group">
                                                    <label for="name">School Name<span class="text-danger"></span></label>
                                                    <input type="text" class="form-control" name="school_name" id="s_name" value="<?= $school_name ?>" >
                                                    <span class="text-danger py-2"><?= $schoolErr ?></span>
                                                </div>
                                            </div> 
    										<div class="col-md-6">
    											<div class="form-group">
    												<label for="fname">Father Name/ Guardian Name<span class="text-danger">*</span></label>
    												<input type="text" class="form-control" name="fname" id="fname" value="<?= $fname ?>" >
    											    <span class="text-danger py-2"><?= $fnameErr ?></span>
    											</div>
    										</div>
    										<div class="col-md-6">
    											<div class="form-group">
    												<label for="dob">Date Of Birth<span class="text-danger">*</span></label>
    												<input type="date" class="form-control" name="dob" id="dob" value="<?= $dob ?>" >
    												<span class="text-danger py-2"><?= $dobErr ?></span>
    											</div>
    										</div>
    										<div class="col-md-6"> 
    											<div class="form-group">
    												<label for="gender">Gender<span class="text-danger">*</span></label>
    												<select class="form-control" name="gender" id="gender" >
    												    <option value="">--SELECT--</option>
    												    <option value="Female" <?= $gender == 'Female' ? 'selected' : '' ; ?>>Female</option>
    												    <option value="Male" <?= $gender == 'Male' ? 'selected' : '' ; ?>>Male</option>
    												    <option value="Others" <?= $gender == 'Others' ? 'selected' : '' ; ?>>Others</option>
    												</select>
    												<span class="text-danger py-2"><?= $genderErr ?></span>
    											</div>
    										</div>
    									</div>
    								</div>
    							</div>
    							<div class="card">
    								<div class="card-body">
    								    <h4 class="card-title">Contact Details</h4>
        								<div class="row form-row">
    										<div class="col-md-6">
    											<div class="form-group">
    												<label for="country" class="control-label">Country<span class="text-danger">*</span></label>
    												<input type="text" class="form-control" name="country" id="country" value="<?= $country ?>">
    												<span class="text-danger py-2"><?= $countryErr ?></span>
    											</div>
    										</div>
    										<div class="col-md-6">
    											<div class="form-group">
    												<label for="state" class="control-label">State<span class="text-danger">*</span></label>
    												<input type="text" class="form-control" name="state" id="state" value="<?= $state ?>">
    												<span class="text-danger py-2"><?= $stateErr ?></span>
    											</div>
    										</div>
    										<div class="col-md-6">
    											<div class="form-group">
    												<label for="city" class="control-label">City<span class="text-danger">*</span></label>
    												<input type="text" class="form-control" name="city" id="city" value="<?= $city ?>">
    												<span class="text-danger py-2"><?= $cityErr ?></span>
    											</div>
    										</div>
    										<div class="col-md-6">
    											<div class="form-group">
    												<label for="address" >Address<span class="text-danger">*</span></label>
    												<input type="text" class="form-control" name="address" id="address" value="<?= $address ?>">
    												<span class="text-danger py-2"><?= $addressErr ?></span>
    											</div>
    										</div>
    										<div class="col-md-6">
    											<div class="form-group">
    												<label for="com_map">Contact Email <span class="text-danger">*</span></label>
    												<input type="email" class="form-control" name="email" id="email" value="<?= $check_email ?>" >
    												<span class="text-danger py-2"><?= $emailErr ?></span>
    											</div>
    										</div>
    										<div class="col-md-6">
    											<div class="form-group">
    												<label>Contact Number <span class="text-danger">*</span></label>
    												<input type="text" class="form-control" name="phone" id="phone" value="<?= $check_phone ?>" maxlength="10" onkeypress="if(this.value.length == 10) return false;">
    												<span class="text-danger py-2"><?= $phoneErr ?></span>
    											</div>
    										</div>
    									</div>
    						        </div>
        							<!-- /Basic Information -->
        						</div>	
    							<div class="card">
    								<div class="card-body">
    								    <h4 class="card-title">Professional Details</h4>
        								<div class="row form-row">
    										<div class="col-md-6">
    											<div class="form-group">
    												<label for="fide_id" class="control-label">FIDE ID</label>
    												<input type="text" class="form-control" name="fide_id" id="fide_id" value="<?= $fide_id ?>">
    											</div>
    										</div>
    										<div class="col-md-6">
    											<div class="form-group">
    												<label for="aicf_id" class="control-label">AICF ID</label>
    												<input type="text" class="form-control" name="aicf_id" id="aicf_id" value="<?= $aicf_id ?>">
    											</div>
    										</div>
    										<div class="col-md-6">
    											<div class="form-group">
    												<label for="tnsca_id" >TNSCA ID</label>
    												<input type="text" class="form-control" name="tnsca_id" id="tnsca_id" value="<?= $tnsca_id ?>">
    											</div>
    										</div>
    										<div class="col-md-6">
    											<div class="form-group">
    												<label for="chess_id">Chess.com Id</label>
    												<input type="text" class="form-control" name="chess_id" id="chess_id" value="<?= $chess_id ?>" >
    											</div>
    										</div>
    										<div class="col-md-6">
    											<div class="form-group">
    												<label for="lichess_id">Lichess ID</label>
    												<input type="text" class="form-control" name="lichess_id" id="lichess_id" value="<?= $lichess_id ?>" >
    											</div>
    										</div>
    									</div>
    						        </div>
        							<!-- /Basic Information -->
        						</div>	
    							<div class="submit-section submit-btn-bottom">
    							    <input type="hidden" name="user_id" value="<?= $user_id ?>" >
    								<button type="submit" class="btn btn-primary submit-btn" name="submit">Submit</button>
    							</div>
							</form>
						</div>
					</div>

				</div>

			</div>		
			<!-- /Page Content -->
   
			<!-- Footer -->
		    <?php include "footer.php" ; ?>
			<!-- /Footer -->
		   
		</div>
		<!-- /Main Wrapper -->
	  
		<!-- jQuery -->
		<script src="assets/js/jquery.min.js"></script>
		
		<!-- Bootstrap Core JS -->
		<script src="assets/js/popper.min.js"></script>
		<script src="assets/js/bootstrap.min.js"></script>
		
		<!-- Sticky Sidebar JS -->
        <script src="assets/plugins/theia-sticky-sidebar/ResizeSensor.js"></script>
        <script src="assets/plugins/theia-sticky-sidebar/theia-sticky-sidebar.js"></script>
		
		<!-- Select2 JS -->
		<script src="assets/plugins/select2/js/select2.min.js"></script>
		
		<!-- Dropzone JS -->
		<script src="assets/plugins/dropzone/dropzone.min.js"></script>
		
		<!-- Bootstrap Tagsinput JS -->
		<script src="assets/plugins/bootstrap-tagsinput/js/bootstrap-tagsinput.js"></script>
		
		<!-- Profile Settings JS -->
		<script src="assets/js/profile-settings.js"></script>
		
		<!-- Custom JS -->
		<script src="assets/js/script.js"></script>
		<script>
    		<?php if(isset($_SESSION['status']) && !empty($_SESSION['status'])) { ?>
    		    swal({
                  title: "<?= $_SESSION['status'] ?>",
                  text: "",
                  icon: "<?= $_SESSION['alert'] ?>",
                });
            <?php unset($_SESSION['status']) ; } ?>
        </script>
        <script>
    		<?php if(isset($_SESSION['check']) && !empty($_SESSION['check'])) { ?>
    		    swal({
                    title: "<?= $_SESSION['check'] ?>", 
                    text: "",
                    icon: "<?= $_SESSION['alert'] ?>",
                });
            <?php unset($_SESSION['check']) ; } ?>
        </script>
	</body>

<!-- doccure/doctor-profile-settings.html  30 Nov 2019 04:12:15 GMT -->
</html>