<?php  
// untuk sembunyikan eror 
  // error_reporting(0);
  session_start();
  // ini untuk menyambungkan ke file lain
  include 'konek.php';
  // disini jika status udah login, maka akan ke profil
  if($_SESSION['status_login'] !=true) {
    echo '<script>window.location="loginn.php"</script>';
  }
    // memanggil data dalam database sesuai dengan admin id
  $query = mysqli_query($conn, "SELECT * FROM pelanggan WHERE costumer_id = '".$_SESSION['id']."' ");
  // menyimpan data dalam variable
  $c = mysqli_fetch_object($query);
?>


<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Fixit - Myprofile</title>
  <link rel="stylesheet" type="text/css" href="profil.css">
<link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">
</head>

<body>
  <!-- header -->
  <header>
    <div class="container">
  <h1><a href="database.php">FIXIT</a></h1>
  <ul>
    <li><a href="buying.php">Home</a></li>
    <li><a href="profil.php">Profile</a></li>
    <li><a href="logout.php">Logout</a></li>
  </ul>
  </div>
  </header>




<!-- container -->

<div class="section" style="background-image: url('service-bg.jpg')">
  <div class="container">
    <h3>Profile</h3>
    <div class="box">
      <form action="" method="POST">
        <input type="text" name="costumer_name" placeholder="Full Name" class="input-control"    value="<?php echo $c->costumer_name   ?>" required>
        <input type="text" name="costumer_num" placeholder="Phone Number" class="input-control" value="<?php echo $c->costumer_num   ?>" required>
        <input type="email" name="costumer_email" placeholder="Your Email" class="input-control" value="<?php echo $c->costumer_email   ?>" required>
        <input type="text" name="costumer_address" placeholder="Your Address" class="input-control" value="<?php echo $c->costumer_address   ?>" required>
        <input type="hidden" name="employee_id" placeholder="Employee ID" class="input-control" required>
        <input type="submit" name="submit" value= "Edit Profile"class="btn">
      </form>
         <?php

               if(isset($_POST['submit'])){
                                  // ucwords untuk menyesuaikan data
                                  $costumer_name    = ucwords($_POST['costumer_name']);
                                  $costumer_num     = $_POST['costumer_num'];
                                  $costumer_email   = $_POST['costumer_email'];
                                  $costumer_address = $_POST['costumer_address'];
                                  $employee_id      = ucwords($_POST['employee_id']);

                $update = mysqli_query($conn, "UPDATE pelanggan SET
                                  costumer_name     = '".$costumer_name."', 
                                  costumer_num      = '".$costumer_num."',
                                  costumer_email    = '".$costumer_email."',
                                  costumer_address  = '".$costumer_address."',
                                  employee_id       = '".$employee_id."'
                                  WHERE costumer_id = '".$c->costumer_id."' ");


          if($update){
            echo '<script>alert("Successfully Edit data")</script>';
            echo '<script>window.location=admin.php</script>';
          }else{
            
            echo '<script>alert("Failed to add data")</script>';
          }

    }

         ?>


    </div>

    <h3>Password</h3>
    <div class="box">
      <form action="" method="POST">
        <input type="password" name="pass1" placeholder="New Password" class="input-control"  required>
        <input type="password" name="pass2" placeholder="Password Confirmation" class="input-control"  required>

        
        <input type="submit" name="edit_password" value= "Edit Password"class="btn">
      </form>
      <?php
        // jika user click tombol edit PW, maka...
        if(isset($_POST['edit_password'])){

          // untuk input PW
          $pass1 = $_POST['pass1'];
          // untuk input config PW
          $pass2 = $_POST['pass2'];
        
          // kalau PW1 dan PW2 tidak sama, maka gagal diedit
          if($pass2 != $pass1){
            echo '<script>alert("New password confirmation does not match")</script>';
          }else{

            // jika sama, maka edit PW berhasil
            // PW akan update ke database
            $u_pass = mysqli_query($conn, "UPDATE pelanggan SET 
                  password = '".($pass1)."'
                  WHERE costumer_num = '".$c->costumer_num."' ");
            // kalau berhasil edit dan masuk ke database
            if($u_pass){
              echo '<script>alert("Successfully Editted")</script>';
              echo '<script>window.location="profil.php</script>';
            }else{
              // Jika gagal update ke database dan ditunjukkan erornya, lewat .mysqli_error($conn)
              echo 'Error, could not edit profile'.mysqli_error($conn);
            }

        }

      }

      ?>




  </div>
</div>






  <!-- footer -->
<footer>
  <div class="container" id="footer">
    <small>Copyright & copy; 2023 - FIXIT.</small>
  </div>
</footer>

  
</body>
</html>