<?php
$sql  = $pdo->query("SELECT * FROM users WHERE id = '".$_SESSION['id_user']."'");
$data = $sql->fetch(PDO::FETCH_ASSOC);
?>
<?php
if(isset($_POST['update']))
{
    $nama      = htmlspecialchars(trim($_POST['name']), ENT_QUOTES);
    $email     = htmlspecialchars(trim($_POST['email']), ENT_QUOTES);
    $username  = htmlspecialchars(trim($_POST['username']), ENT_QUOTES);
    $pass      = trim($_POST['password']);
    
    $dirUpload   = "../uploads/";
    $namaSementara = $_FILES['upl']['tmp_name'];
    
    if(empty($_FILES['upl']['tmp_name']))
    {
        
        $namaFile = $data['user_pic'];
        
    }
    else
    {
        unlink("../uploads/".$data['user_pic']."");
        $namaFile = rand().$_FILES['upl']['name'];
        move_uploaded_file($namaSementara, $dirUpload.$namaFile);
    }
    
    if(empty($pass))
    {
        $password = $data['password'];
    }
    else
    {
        $password = md5($pass);
    }
    
	$pdo->query(
	"UPDATE users SET 
	    name          = '".$nama."',
	    email         = '".$email."',
        username      = '".htmlspecialchars(trim($_POST['username'], ENT_QUOTES))."',
		password      = '".$password."',
		user_pic         ='".$namaFile."'
	    WHERE 
	    id_user       =  '".@$_SESSION['id_user']."'"
		)or die(print_r($pdo->errorInfo(), true));
		
	header("Location: ".$_SERVER['HTTP_REFERER']);
}
?>

<div class="container-xxl flex-grow-1 container-p-y">
              
              <div class="row">
                <div class="col-md-12">
                  <div class="card mb-4">
                    <h5 class="card-header">Akun</h5>
                    <!-- Account -->
                    
                    <form id="form" method="POST" action="<?php echo $actual_link; ?>" enctype="multipart/form-data">
                            
                    <div class="card-body">
					    
                      <div class="d-flex align-items-start align-items-sm-center gap-4">
					    <?php $image_path = (empty($data['user_pic'])) ? "assets/img/avatars/default-avatar.png" : "../uploads/".$data['user_pic'];// Example usage for: Ternary Operator ?>
                        <img
                          src="<?php echo $image_path; ?>"
                          alt="user-avatar"
                          class="d-block rounded"
                          height="100"
                          width="100"
                          id="uploadedAvatar"
                        />
                        <div class="button-wrapper">
                          <label for="upload" class="btn btn-primary me-2 mb-4" tabindex="0">
                            <span class="d-none d-sm-block">Upload foto baru</span>
                            <i class="bx bx-upload d-block d-sm-none"></i>
                            <input
                              type="file"
							  name="upl"
                              id="upload"
                              class="account-file-input"
                              hidden
                              accept="image/png, image/jpeg"
                            />
                          </label>
                          <!--
                          <button type="button" class="btn btn-secondary account-image-reset mb-4">
                            <i class="bx bx-reset d-block d-sm-none"></i>
                            <span class="d-none d-sm-block">Reset</span>
                          </button>
                          -->
                          <p class="text-muted mb-0">200 x 200px - Diizinkan JPG atau PNG.</p>
                        </div>
                      </div>
                    </div>
                    <hr class="my-0" />
                    <div class="card-body">
                            
						    <div class="mb-3 col-md-6">
                                <label class="form-label">Nama</label>
                                <input
                                    class="form-control"
                                    type="text"
                                    name="name"
                                    value="<?php echo $data['name']; ?>"
                                    placeholder=""
							        required
                                />
                            </div>
                            <div class="mb-3 col-md-6">
                                <label class="form-label">Email</label>
                                <input
                                    class="form-control"
                                    type="text"
                                    name="email"
                                    value="<?php echo $data['email']; ?>"
                                    placeholder=""
							        required
                                />
                            </div>
                            
                            <div class="mb-3 col-md-6">
                            <label for="" class="form-label">Username</label>
                            <input
                              class="form-control"
                              type="text"
                              name="username"
                              value="<?php echo $data['username']; ?>"
                            />
                          </div>
						    <div class="form-password-toggle mb-3 col-md-6">
                                <label class="form-label" for="">Password</label>
                                <div class="input-group">
                          <input
                            type="password"
                            class="form-control"
                            id="basic-default-password12"
							name="password"
                            placeholder="&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;"
                            aria-describedby="basic-default-password2"
							
                          />
                          <span id="basic-default-password2" class="input-group-text cursor-pointer"
                            ><i class="bx bx-hide"></i
                          ></span>
                        </div>
                            </div>
                          
                            <div class="mt-2">
						<input type="hidden" name="update">
						  <button type="submit" class="btn btn-primary me-2" name="update">
								<span id="spin" class="spinner-border spinner-border-sm" role="status" aria-hidden="true" style="display:none;"></span>
                                <span class="visually-hidden">Loading...</span>
								Simpan
							</button>
                          
                        </div>
						
                        
					  
                    </div>
                    
                    </form>
                    <!-- /Account -->
                    
                  </div>
                  
                </div>
              </div>
            </div>