<!-- Main content -->
<section class="content" style="zoom: 90%;">
  <div class="container-fluid">
    <div class="row">
      <div class="col-12">
        <div class="card">
          <div class="card-header">
            <div class="row">
              <div class="col-sm-6">
                <h3 class="card-title">Data Aset</h3>
              </div>
              <div class="col-sm-6">
                <ol class="float-sm-right mb-0">
                  <a type="button" class="btn btn-danger btn-sm m-1" data-toggle="modal" data-target="#staticBackdrop">Tambah Aset</a>
                </ol>
              </div>
              <!-- Modal Tambah -->
              <div class="modal fade" id="staticBackdrop" data-backdrop="static" data-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
                <div class="modal-dialog modal-xl">
                  <div class="modal-content">
                    <form method="post" enctype="multipart/form-data">
                      <div class="modal-header bg-danger">
                        <h5 class="modal-title" id="staticBackdropLabel">Tambah Aset</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                          <span aria-hidden="true">&times;</span>
                        </button>
                      </div>
                      <div class="modal-body">
                        <div class="row">
                          <div class="card-body pb-0">
                            <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 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" required="required">
                                </div>
                                <div class="form-group">
                                  <label>Harga Barang</label>
                                  <input type="text" class="form-control rupiah" id="rupiah0" name="harga" required="required">
                                </div>
                                <div class="form-group">
                                  <label>Jumlah</label>
                                  <input type="text" class="form-control" name="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" required="required">
                                </div>
                                <div class="form-group">
                                  <label>Nilai Residu</label>
                                  <input type="text" class="form-control rupiah" id="rupiah0" name="residu" required="required">
                                </div>
                                <div class="form-group">
                                  <label>Tanggal Pembelian</label>
                                  <input type="date" class="form-control" name="pembelian" required="required">
                                </div>
                                <div class="form-group">
                                  <label>Keterangan</label>
                                  <textarea  class="form-control" name="keterangan" required="required" rows="5"></textarea>
                                </div>
                              </div>
                            </div>
                          </div>
                        </div>
                      </div>
                      <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <input type="submit" name="tambah" class="btn btn-primary" value="Tambah">
                      </div>
                    </form>
                    <?php
                   if (isset($_POST['tambah'])) {

                        $sql = "INSERT INTO aset 
                        (id_data_server, id_user_akses, nama, harga, jumlah, penyusutan, residu, start_time, keterangan) 
                                VALUES (:ids, :userId, :nama, :harga, :jumlah, :penyusutan, :residu,  :waktu, :keterangan)";

                        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']);

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

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

                        } catch (PDOException $e) {
                            // Handle error
                            echo 'Error: ' . $e->getMessage();
                        }
                    }
                    ?>
                  </div>
                </div>
              </div>
              <!-- END Modal Tambah -->
            </div>
          </div>
          <!-- /.card-header -->
          <?php
            $aset1 = mysqli_query($con,"SELECT SUM(jumlah_harga) AS jumlah_harga, SUM(jumlah_residu) AS jumlah_residu, SUM(jumlah_nilai_saat_ini) AS jumlah_nilai_saat_ini FROM v_jumlah_aset $SqlDataWHId");
            $aset = mysqli_fetch_assoc($aset1);
          ?>
          <div class="card-body">
            <table id="example1" class="table table-bordered table-striped">
              <thead>
              <tr>
                <th style="vertical-align: top;">No</th>
                <th style="vertical-align: top;">Nama Barang</th>
                <th style="vertical-align: top;">Jumlah</th>
                <th style="vertical-align: top;">Penyusutan</th>
                <th>Harga Barang<br>Rp. <?php echo number_format($aset['jumlah_harga']); ?></th>
                <th>Residu<br>Rp. <?php echo number_format($aset['jumlah_residu']); ?></th>
                <th>Nilai Saat Ini<br>Rp. <?php echo number_format($aset['jumlah_nilai_saat_ini']); ?></th>
                <th style="vertical-align: top;">Tanggal Pembelian</th>
                <th style="vertical-align: top;">Action</th>
              </tr>
              </thead>
              <tbody>
              <?php
                $no = 1;
                $aserver = mysqli_query($con,"SELECT * FROM v_aset $SqlDataWHId");
                while ($server = mysqli_fetch_assoc($aserver)) 
                {
              ?>
              <tr>
                <td style="max-width: 30px;"><?php echo $no; ?></td>
                <td style="max-width: 200px;"><?php echo $server['nama']; ?></td>
                <td style="max-width: 200px;"><?php echo $server['jumlah']; ?></td>
                <td style="max-width: 200px;"><?php echo $server['penyusutan']; ?> Tahun</td>
                <td style="max-width: 200px;">Rp. <?php echo number_format($server['harga']); ?></td>
                <td style="max-width: 30px;">Rp. <?php echo number_format($server['residu']); ?></td>
                <td style="max-width: 30px;">Rp. <?php echo number_format($server['nilai_saat_ini']); ?></td>
                <td style="max-width: 30px;"><?php echo $server['start_time']; ?></td>
                <td>
                <form method="post">
                  <a href="?<?php echo encrypt_url('barang_aset_detail|'.$server['id']); ?>" class="btn btn-info btn-sm">Detail / Edit</a>
                  <input type="submit" class="btn btn-danger btn-sm m-1" name="hapus<?php echo $server['id']; ?>" value="Hapus">
                </form>
                </td>
              </tr>
            <?php
              if (isset($_POST['hapus'.$server['id']])) {
                $iddata = $server['id'];
                deleteAlert("aset",$iddata );
              }
             $no ++; } ?>
              </tbody>
            </table>
          </div>
          <!-- /.card-body -->
        </div>
        <!-- /.card -->
      </div>
      <!-- /.col -->
    </div>
    <!-- /.row -->
  </div>
  <!-- /.container-fluid -->
</section>