<?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 = $stateErr = $cityErr = $addressErr = $emailErr = $mobileErr = "" ; 
    $name = $fname = $dob = $gender = $country = $state = $city = $address = $email = $mobile = "" ;
    $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($_POST['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)
                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') ";
            if($config->query($sql) === TRUE){
                $name = $fname = $dob = $gender = $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";
            }
        }
    }
    
    
?>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js"></script>
	<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="row">
								<div class="col-md-12">
									<div class="d-flex justify-content-between align-items-start">
									    <h4 class="mb-4">Players List</h4>
									    <!-- <button type="button" class="btn btn-sm bg-info-light" id="download"><i class="fas fa-file-alt"></i> Export</button> -->
                                           <a class="btn btn-sm bg-info-light" target="_blank" href="exportCsv2.php?user=<?=$user_id?>"><i class="fas fa-file-alt"></i> Export</a>
									</div>
									<div class="appointment-tab">

										<div class="tab-content">
										
											 <!--Upcoming Appointment Tab -->
											<div class="tab-pane show active" id="active">
												<div class="card card-table mb-0" >
													<div class="card-body" id="view-list">
														<div class="table-responsive">
															<table class="table table-hover table-center mb-0">
																<thead>
																	<tr>
																		<th>S.No</th>
																		<th>Name</th>
                                                                        <th>Initial</th>   						
																		<th>City</th>
																		<th>Edit</th>
																	</tr>
																</thead>
																<tbody>
																    <?php
																        $sno = 1;
        																$sql = "SELECT * FROM players WHERE user_id = '$user_id' " ;
                    											        $res = $config->query($sql);
                    											        while($list = $res->fetch_assoc()){
																    ?>
																	<tr>
																	    <td><?= $sno;?></td>
                                                                        <td><?= $list['name'] ?></td>
																	    <td><?= $list['name_initial'] ?></td>
																	    <td><?= $list['city'] ?></td>
                    											        <td>
                    											            <form method="POST" action="players-edit.php">
                    											                <input type="hidden" name="pid" value="<?= $list['id'] ?>">
                    											                <input type="hidden" name="action" value="edit">
                    											                <button class="btn btn-primary"><i class="fas fa-pencil-alt"></i>Edit</button>
                    											            </form>
                    											        </td>
																	</tr>
																		<?php  $sno++;} ?>
																
																</tbody>
															</table>		
														</div>
													</div>
												</div>
											</div>
											 <!--/Upcoming Appointment Tab -->
											
										</div>
									</div>
								</div>
							</div>
						</div>
					</div>

				</div>

			</div>		
			<!-- /Page Content -->
   
			<!-- Footer -->
		    <?php include "footer.php" ; ?>
			<!-- /Footer -->
		   
		</div>
		<!-- /Main Wrapper -->
		<script>
            window.onload = function(){
                document.getElementById("download").addEventListener('click',()=>{
                    const viewlist = document.getElementById('view-list');
                    let opt = {
                      filename:     'players-list.pdf',
                      image:        { type: 'jpeg', quality: 0.98 },
                      html2canvas:  { scale: 2 },
                      jsPDF:        { unit: 'in', format: 'letter', orientation: 'portrait' }
                    };
                    html2pdf().set(opt).from(viewlist).save();
                })
            }
        </script>
	  
		<!-- 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>