<?php 

$aserver = mysqli_query($con,"SELECT * FROM v_aset WHERE id = '$page[1]'");
$server = mysqli_fetch_assoc($aserver);

?>
<!-- Main content -->
<section class="content">
  <div class="container-fluid">
    <div class="row">
      <div class="col-12">
        <div class="card">
          <div class="card-header bg-secondary">
            <div class="row">
              <div class="col-sm-6">
                <h3 class="card-title">Detail / Edit Aset ( <?php echo $server['nama']; ?> )</h3>
              </div>
              <div class="col-sm-6">
                <ol class="float-sm-right mb-0">
                </ol>
              </div>
            </div>
          </div>
          <div class="card-body">
            <div class="row">
              <div class="col-md-5">
                <label><b>Perhitungan Penyusutan</b></label>
                <table class="table table-bordered table-striped">
                  <thead>
                    <tr>
                      <th>Tahun</th>
                      <th>Nilai Penyusutan</th>
                      <th>Nilai Akhir</th>
                    </tr>
                  </thead>
                  <tbody>
                    <?php
                    $nilai = ($server['harga'] - $server['residu']) / $server['penyusutan'];

                    $x = $server['penyusutan'];
                    $y = $server['penyusutan'];
                    while ($x >= 0 AND 0 <= $y) {
                    ?>
                    <tr>
                      <td>Lebih Dari => <?php 
                      $a = $y-$server['penyusutan'];
                      echo date('d/m/Y', strtotime('+'.$a.' year', strtotime($server['start_time'])));
                      ?></td>
                      <td>Rp. <?php echo number_format($nilai * $a); ?></td>
                      <td>Rp. <?php echo number_format($nilai * $x + $server['residu']); ?></td>
                    </tr>
                    <?php $x--; $y++; } ?>
                  </tbody>
                </table>
              </div>
              <div class="col-md-7">
                <form method="post">
                <div class="row">
                  <div class="col-md-6">
                    <div class="form-group">
                        <label>Nama Server</label>
                        <select class="form-control" name="ids" required="required">
                            <option value="">Pilih</option>
                            <?php 
                            $List = new Listdata();
                            $List->set_list("data_server", $SqlDataWH);
                            foreach ($List->get_list() as $key => $value): ?>
                                <option value="<?php echo $value['id']; ?>" <?php if($value['id'] == $server['id_data_server']) { echo "selected"; } ?>><?php echo $value['nama_server']; ?></option>
                            <?php endforeach ?>
                        </select>
                    </div>
                    <div class="form-group">
                      <label>Nama Barang</label>
                      <input type="text" class="form-control" name="nama" value="<?php echo $server['nama']; ?>" required="required">
                    </div>
                    <div class="form-group">
                      <label>Harga Barang</label>
                      <input type="text" class="form-control rupiah" id="rupiah0" value="<?php echo $server['harga']; ?>" name="harga" required="required">
                    </div>
                    <div class="form-group">
                      <label>Jumlah</label>
                      <input type="text" class="form-control" name="jumlah" value="<?php echo $server['jumlah']; ?>" required="required">
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="form-group">
                      <label>Penyusutan</label>
                      <input type="text" class="form-control" name="penyusutan" value="<?php echo $server['penyusutan']; ?>" required="required">
                    </div>
                    <div class="form-group">
                      <label>Nilai Residu</label>
                      <input type="text" class="form-control rupiah" id="rupiah0" value="<?php echo $server['residu']; ?>" name="residu" required="required">
                    </div>
                    <div class="form-group">
                      <label>Tanggal Pembelian</label>
                      <input type="date" class="form-control" name="pembelian" value="<?php echo $server['start_time']; ?>" required="required">
                    </div>
                    <div class="form-group">
                      <label>Keterangan</label>
                      <textarea  class="form-control" name="keterangan" required="required" rows="5"><?php echo $server['keterangan']; ?></textarea>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="card-footer">
            <input type="submit" class="btn btn-primary btn-sm" name="edit" value="Simpan">
          </div>
          </form>
          <?php 
          if (isset($_POST['edit'])) {

              $sql = "UPDATE aset SET id_data_server = :ids, id_user_akses = :userId, nama = :nama, harga = :harga, jumlah = :jumlah, penyusutan = :penyusutan, residu = :residu, start_time = :waktu, keterangan = :keterangan WHERE id = :id";
              try {
                  // Prepare the statement
                  $stmt = $pdo->prepare($sql);
                  $harga = preg_replace("/[^0-9]/","",$_POST['harga']);
                  $residu = preg_replace("/[^0-9]/","",$_POST['residu']);

                  // Bind the parameters
                  $stmt->bindParam(':ids', $_POST['ids']);
                  $stmt->bindParam(':userId', $UserId);
                  $stmt->bindParam(':nama', $_POST['nama']);
                  $stmt->bindParam(':harga', $harga);
                  $stmt->bindParam(':jumlah', $_POST['jumlah']);
                  $stmt->bindParam(':penyusutan', $_POST['penyusutan']);
                  $stmt->bindParam(':residu', $residu);
                  $stmt->bindParam(':waktu', $_POST['pembelian']);
                  $stmt->bindParam(':keterangan', $_POST['keterangan']);
                  $stmt->bindParam(':id', $page[1]);

                  // Execute the statement
                  $stmt->execute();

                  // Success message
                  sweetAlert("Data Aset telah di ubah", "success");

              } catch (PDOException $e) {
                  // Handle error
                  echo 'Error: ' . $e->getMessage();
              }
          }
          ?>
        </div>
      </div>
    </div>
  </div>
</section>