<?php
if($_SESSION['role'] != "admin"){
  echo "<script> location.href = 'index.php' </script>";
}

$page = isset($_GET['page']) ? $_GET['page'] : '';
$insert = false;

if (isset($_POST['edit_data'])){
  $old_id = $_POST['edit_data'];

  $username = $_POST['username'];
  $fullname = $_POST['fullname'];
  $access = $_POST['role'];
  $active = $_POST['active'];

  if($_POST['password']==""){
  $sql_edit = "UPDATE user SET Username = '$username', fullname = '$fullname', password = '$password', role = '$access', active = '$active' WHERE Username = '$old_id'";
  } else{
  $password = password_hash($_POST['password'],PASSWORD_BCRYPT);  
  $sql_edit = "UPDATE user SET Username = '$username', fullname = '$fullname', password = '$password', role = '$access', active = '$active' WHERE Username = '$old_id'";
  }

  
  mysqli_query($connection,$sql_edit);

}else if(isset($_POST['username'])){
  $username = $_POST['username'];
  $fullname = $_POST['fullname'];
  $password = password_hash($_POST['password'],PASSWORD_BCRYPT);
  $access = $_POST['role'];
  $active = $_POST['active'];

  $sql_insert = "INSERT INTO user (Username,fullname,password,role,active) VALUES ('$username','$fullname','$password','$access','$active')";
  mysqli_query($connection,$sql_insert);
  $insert = true;
}

if(isset($_GET['edit'])){
  $id = $_GET['edit'];
  $sql_select_data = "SELECT * FROM user WHERE Username = '$id' LIMIT 1";

  $result_data = mysqli_query($connection,$sql_select_data);
  $data = mysqli_fetch_assoc($result_data);
}

$sql = "SELECT * FROM user";
$result = mysqli_query($connection, $sql);
?>

<div class="content-wrapper">
  <div class="content-header">
    <div class="container-fluid">
      <h1 class="m-0">pengguna</h1>
    </div>
  </div>

  <div class="content">
    <div class="container-fluid">

      <?php if ($insert){ ?>
        <?php alertsSuccess("Data berhasil ditambahkan"); ?>
      <?php } ?>

      <div class="card">
        <div class="card-header">
          <h3 class="card-title">pengguna yang terdaftar</h3>
        </div>

        <div class="card-body">
          <table class="table table-bordered table-striped">
            <thead>
              <tr>
                <th>Username</th>
                <th>Fullname</th>
                <th>Hak akses</th>
                <th>Aktif</th>
                <th>Action</th>
              </tr>
            </thead>

            <tbody>
              <?php while($row = mysqli_fetch_assoc($result)){ ?>
              <tr>
                <td><?= $row['Username'] ?></td>
                <td><?= $row['fullname'] ?></td>
                <td><?= $row['role'] ?></td>
                <td><?= $row['active'] ?></td>
                <td>
                  <a href="?page=<?= $page ?>&edit=<?= $row['Username'] ?>">
                    <i class="fas fa-edit"></i>
                  </a>
                </td>
              </tr>
              <?php } ?>
            </tbody>

          </table>
        </div>
      </div>

      <?php if (isset($_GET['edit'])){ ?>
      <!-- EDIT -->
      <div class="card card-warning">
        <div class="card-header">
          <h3 class="card-title">Ubah data</h3>
        </div>

        <form method="post" action="?page=<?= $page ?>">
          <div class="card-body">
            <input type="hidden" name="edit_data" value="<?= $data['Username']?>">
            <div class="form-group">
              <label>Username</label>
              <input type="text" name="username" class="form-control"
              value="<?= $data['Username'] ?>" required>
            </div>

            <div class="form-group">
              <label>Fullname</label>
              <input type="text" name="fullname" class="form-control"
              value="<?= $data['fullname'] ?>" required>
            </div>

            <div class="form-group">
              <label>Password</label>
              <input type="password" name="password" class="form-control" placeholder="Boleh tidak diisi">
            </div>

            <div class="form-group">
              <label>Access</label>
              <select name="role" class="form-control">
                <option value="admin" <?= ($data['role'] == "admin") ? "selected" : "" ?>>admin</option>
                <option value="user" <?= ($data['role'] == "user") ? "selected" : "" ?>>user</option>
              </select>
            </div>

            <div class="form-group">
              <label>Status</label>
              <select name="active" class="form-control">
                <option value="Yes" <?= ($data['active'] == "Yes") ? "selected" : "" ?>>Yes</option>
                <option value="No" <?= ($data['active'] == "No") ? "selected" : "" ?>>No</option>
              </select>
            </div>

          </div>

          <div class="card-footer">
            <button type="submit" class="btn btn-warning">Update</button>
          </div>
        </form>
      </div>

      <?php } else { ?>
      <!-- TAMBAH -->
      <div class="card card-primary">
        <div class="card-header">
          <h3 class="card-title">Tambah data</h3>
        </div>

        <form method="post" action="?page=<?= $page ?>">
          <div class="card-body">
            <div class="form-group">
              <label>Username</label>
              <input type="text" name="username" class="form-control" placeholder="username tidak boleh sama" required>
            </div>
            <div class="form-group">
              <label>Fullname</label>
              <input type="text" name="fullname" class="form-control" required>
            </div>
            <div class="form-group">
              <label>Password</label>
              <input type="password" name="password" class="form-control" placeholder="Boleh tidak diisi">
            </div>
            <div class="form-group">
              <label>Access</label>
              <select name="role" class="form-control">
                <option value="admin">admin</option>
                <option value="user">user</option>
              </select>
            </div>

            <div class="form-group">
              <label>Active</label>
              <select name="active" class="form-control">
                <option value="Yes">Yes</option>
                <option value="No">No</option>
              </select>
            </div>

          </div>

          <div class="card-footer">
            <button type="submit" class="btn btn-primary">Tambah</button>
          </div>
        </form>
      </div>
      <?php } ?>

    </div>
  </div>
</div>