<?php 
    
    include "Admin/config/setup.php";
    session_start();

    // $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'])){
        $id = $_POST['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']);
        if(count($result) == 0 ){
            $sql = "UPDATE players SET name = '$name', f_name = '$fname', dob = '$dob', gender = '$gender', country = '$country', state = '$state', city = '$city', address = '$address',
            email = '$email', phone = '$phone', fide_id = '$fide_id', aicf_id = '$aicf_id', tnsca_id = '$tnsca_id', chess_id = '$chess_id', lichess_id = '$lichess_id',name_initial='$name_initial',school_name='$school_name' WHERE id = '$id' ";
            if($config->query($sql) === TRUE){
                $_SESSION['status'] = "Updated Successfully";
    		    $_SESSION['alert'] = "success";
    		    header('Location:players-edit.php?pid='.$id);
            }else{
                $_SESSION['status'] = "Failed";
    		    $_SESSION['alert'] = "error";
    		    header('Location:players-edit.php?pid='.$id);
            }
        }else{
            $_SESSION['status'] = "Failed";
		    $_SESSION['alert'] = "error";
		    header('Location:players-edit.php?pid='.$id);
        }
    }
    
    
?>