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

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

  $serial_number = $_POST['serial_number'];
  $controller_type = $_POST['controller'];
  $location = $_POST['location'];
  $active = $_POST['active'];

  $sql_edit = "UPDATE devices SET serial_number = '$serial_number', mcu_type = '$controller_type', location = '$location', active = '$active' WHERE serial_number = '$old_id'";
  mysqli_query($connection,$sql_edit);

}else if(isset($_POST['serial_number'])){
  $serial_number = $_POST['serial_number'];
  $controller_type = $_POST['controller'];
  $location = $_POST['location'];

  $sql_insert = "INSERT INTO devices (serial_number,mcu_type,location) VALUES ('$serial_number','$controller_type','$location')";
  mysqli_query($connection,$sql_insert);
  $insert = true;
}

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

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

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

<div class="content-wrapper">
  <div class="content-header">
    <div class="container-fluid">
      <h1 class="m-0">perangkat</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">perangkat yang terdaftar</h3>
        </div>

        <div class="card-body">
          <table id="example1" class="table table-bordered table-striped">
            <thead>
              <tr>
                <th>Serial number</th>
                <th>MCU-type</th>
                <th>Lokasi</th>
                <th>Waktu</th>
                <th>Aktif</th>
                <th>Action</th>
              </tr>
            </thead>

            <tbody>
              <?php while($row = mysqli_fetch_assoc($result)){ ?>
              <tr>
                <td><?= $row['serial_number'] ?></td>
                <td><?= $row['mcu_type'] ?></td>
                <td><?= $row['location'] ?></td>
                <td><?= $row['created_time'] ?></td>
                <td><?= $row['active'] ?></td>
                <td>
                  <a href="?page=<?= $page ?>&edit=<?= $row['serial_number'] ?>">
                    <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=<?php echo $page?>">
          <div class="card-body">
                <div class="form-group">
                  <label>Serial number</label>
                  <input type="hidden" name="edit_data" value="<?= $data['serial_number']?>">
                  <input type="text" name="serial_number" class="form-control"
                  value="<?= $data['serial_number'] ?>" required>
                </div>
                <div class="form-group">
                  <label>MCU_type</label>
                  <input type="text" name="controller" class="form-control"
                  value="<?= $data['mcu_type'] ?>" required>
                </div>
                <div class="form-group">
                  <label>Location</label>
                  <input type="text" name="location" class="form-control"
                   value="<?= $data['location'] ?>" required>
                </div>
                <div class="form-group">
                  <label>Status</label>
                  <select name="active" class="form-control">
                    <option <?= ($data['active'] == "Yes") ? "selected" : "" ?>>Yes</option>
                    <option <?= ($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=<?php echo $page?>">
          <div class="card-body">
            <div class="form-group">
              <label>Serial number</label>
              <input type="text" name="serial_number" class="form-control" placeholder="Serial number tidak boleh sama" required>
            </div>
            <div class="form-group">
                <label>MCU_type</label>
                <input type="text" name="controller" class="form-control" required>
            </div>
            <div class="form-group">
                 <label>Location</label>
                 <input type="text" class="form-control" name="location" required>
            </div>
          </div>

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

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