<?php 
$ainv = mysqli_query($con,"SELECT id_data_server,nomor_invoice FROM invoice WHERE id = '$page[1]'");
$inv = mysqli_fetch_assoc($ainv);
?>
<!-- Main content -->
<section class="content">
  <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">Detail Invoice : <b><?php echo $inv['nomor_invoice']; ?></b></h3>
              </div>
              <div class="col-sm-6">
                <ol class="float-sm-right mb-0">
                  <!-- <a type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#staticBackdrop">Tambah</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">
                  <div class="modal-content">
                    <form method="post" enctype="multipart/form-data">
                      <div class="modal-header bg-primary">
                        <h5 class="modal-title" id="staticBackdropLabel">Tambah Item Invoice</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="form-group">
                              <label>Tanggal</label>
                              <input type="date" class="form-control" name="tanggal" required="required">
                            </div>
                            <div class="form-group">
                              <label>Deskripsi</label>
                              <input type="text" class="form-control" name="nama" required="required">
                            </div>
                            <div class="form-group">
                              <label>Quantity</label>
                              <input type="text" class="form-control" name="quantity" required="required">
                            </div>
                            <div class="form-group">
                              <label>Harga</label>
                              <input type="text" class="form-control rupiah" id="rupiah0" name="harga" required="required">
                            </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'])) 
                    {
                      $nominal = preg_replace("/[^0-9]/","",$_POST['harga']);
                      $input = mysqli_query($con,"INSERT INTO invoice_list (id_invoice, tanggal, nama, quantity, harga) 
                        VALUES ('$page[1]', '$_POST[tanggal]', '$_POST[nama]', '$_POST[quantity]', '$nominal')");
                      if ($input) 
                      {
                        sweetAlert("Item Invoice telah di tambah", "success");
                      }
                    }
                    ?>
                  </div>
                </div>
              </div>
              <!-- END Modal Tambah -->
            </div>
          </div>
          <!-- /.card-header -->
          <div class="card-body">
            <div class="row">
              <div class="col-md-4">
                <ol class="float-sm-center mb-3"><br>
                </ol>
                <?php 
                  $ainvlist = mysqli_query($con,"SELECT * FROM v_invoice WHERE id = '$page[1]'");
                  $invlist = mysqli_fetch_assoc($ainvlist);
                ?>
                <table class="table table-borderless">
                  <tbody>
                    <tr>
                      <th>Nama</th>
                      <td style="width: 10px;">:</td>
                      <td><?php echo $invlist['nama']; ?></td>
                    </tr>
                    <tr>
                      <th>Alamat</th>
                      <td style="width: 10px;">:</td>
                      <td><?php echo $invlist['alamat']; ?></td>
                    </tr>
                    <tr>
                      <th>Nomor WhatsApp</th>
                      <td style="width: 10px;">:</td>
                      <td><?php echo $invlist['nomor_whatsapp']; ?></td>
                    </tr>
                    <tr>
                      <th>Tanggal Invoice</th>
                      <td style="width: 10px;">:</td>
                      <td><b><?php echo substr(formatIndo($invlist['tanggal_invoice']),3); ?></b></td>
                    </tr>
                    <tr>
                      <th>Limit Pembayaran</th>
                      <td style="width: 10px;">:</td>
                      <td><?php echo $invlist['limit_invoice']; ?></td>
                    </tr>
                    <tr>
                      <th>Total Pembayaran</th>
                      <td style="width: 10px;">:</td>
                      <td><b><?php echo number_format($invlist['total_list']); ?></b></td>
                    </tr>
                  </tbody>
                </table>
              </div>
              <div class="col-md-8">
                <ol class="float-sm-right mb-2">
                  <form method="post">
                    <input type="submit" name="ganerate" class="btn btn-success btn-sm m-1" value="Ganerate Invoice">
                    <a type="button" class="btn btn-danger btn-sm m-1" data-toggle="modal" data-target="#staticBackdrop">Tambah</a>
                  </form>
                </ol>
                <table class="table table-bordered table-striped">
                  <thead>
                    <tr>
                      <th>No</th>
                      <th>Tanggal</th>
                      <th>Deskripsi</th>
                      <th>Quantity</th>
                      <th>Harga</th>
                      <th>Action</th>
                    </tr>
                  </thead>
                  <tbody>
                  <?php
                    $no = 1;
                    $aserver = mysqli_query($con,"SELECT * FROM invoice_list WHERE id_invoice = '$page[1]'");
                    while ($server = mysqli_fetch_assoc($aserver)) 
                    {
                  ?>
                    <tr>
                      <td style="max-width: 30px;"><?php echo $no; ?></td>
                      <td><?php echo $server['tanggal']; ?></td>
                      <td><?php echo $server['nama']; ?></td>
                      <td><?php echo $server['quantity']; ?></td>
                      <td>Rp. <?php echo number_format($server['harga']); ?></td>
                      <td>
                        <form method="post">
                          <a type="button" class="btn btn-secondary btn-sm ml-1" data-toggle="modal" data-target="#modal-edit<?php echo $server['id'];?>">Edit</a>
                          <input type="submit" name="hapus<?php echo $server['id'];?>" value="Hapus" class="btn btn-danger btn-sm ml-1" />
                        </form>
                      </td>
                    </tr>
                    <!-- Modal Edit -->
                    <div class="modal fade" id="modal-edit<?php echo $server['id'];?>" data-backdrop="static" data-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
                      <div class="modal-dialog modal-md">
                        <div class="modal-content">
                          <form method="post" enctype="multipart/form-data">
                            <div class="modal-header bg-secondary">
                              <h5 class="modal-title" id="staticBackdropLabel">Edit Item Invoice</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="form-group">
                                    <label>Tanggal</label>
                                    <input type="date" class="form-control" name="tanggal" value="<?php echo $server['tanggal']; ?>" required="required">
                                  </div>
                                  <div class="form-group">
                                    <label>Deskripsi</label>
                                    <input type="text" class="form-control" name="nama" value="<?php echo $server['nama']; ?>" required="required">
                                  </div>
                                  <div class="form-group">
                                    <label>Quantity</label>
                                    <input type="text" class="form-control" name="quantity" value="<?php echo $server['quantity']; ?>" required="required">
                                  </div>
                                  <div class="form-group">
                                    <label>Harga</label>
                                    <input type="text" class="form-control rupiah" id="rupiah0" value="<?php echo $server['harga']; ?>" name="harga" required="required">
                                  </div>
                                </div>
                              </div>
                            </div>
                            <div class="modal-footer">
                              <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                              <input type="submit" name="edit<?php echo $server['id']; ?>" class="btn btn-warning" value="Edit">
                            </div>
                          </form>
                          <?php 
                          if (isset($_POST['edit'.$server['id']])) 
                            {
                              $nominal = preg_replace("/[^0-9]/","",$_POST['harga']);
                              $update = mysqli_query($con,"UPDATE invoice_list SET
                                id_invoice = '$page[1]',
                                tanggal    = '$_POST[tanggal]',
                                nama       = '$_POST[nama]',
                                quantity   = '$_POST[quantity]',
                                harga      = '$nominal'
                                WHERE id   = '$server[id]'");
                              if ($update) 
                              {
                                sweetAlert("Item Invoice ".$server['nomor_invoice']." telah di edit", "success");
                              }
                            }
                            if (isset($_POST['hapus'.$server['id']]))
                            {
                              $iddata = $server['id'];
                              deleteAlert("invoice_list",$iddata );
                            }
                          ?>
                        </div>
                      </div>
                    </div>
                  <?php $no++; } ?>
                  </tbody>
                </table>
              </div>
            </div>
          </div>
          <!-- /.card-body -->
        </div>
        <!-- /.card -->
      </div>
      <!-- /.col -->
    </div>
    <!-- /.row -->
  </div>
  <!-- /.container-fluid -->
</section>
<!-- /.content -->
<?php
if (isset($_POST['ganerate'])) {

  $ainvp = mysqli_query($con,"SELECT 
    nama_perusahaan,
    alamat_perusahaan,
    nama_bendahara,
    telepon_perusahaan,
    logo_perusahaan,
    catatan_perusahaan 
    FROM data_server WHERE id = '$inv[id_data_server]'");
  $invp = mysqli_fetch_assoc($ainvp);

  $nama = $invp['nama_perusahaan'];
  $alamat = $invp['alamat_perusahaan'];
  $telepon = $invp['telepon_perusahaan'];
  $Keterangan = $invp['catatan_perusahaan'];
  $Pembuat = $invp['nama_bendahara'];
  $logo = $invp['logo_perusahaan'];

  $ainvi = mysqli_query($con,"SELECT * FROM v_invoice WHERE id = '$page[1]'");
  $invi = mysqli_fetch_assoc($ainvi);

  $customerAddress = $invi['alamat'];
  $customerName = $invi['nama'];
  $customerNumber = $invi['nomor_whatsapp'];
  $invoiceNumber = $invi['nomor_invoice'];
  $invoiceDate = $invi['tanggal_invoice'];
  $invoiceLimit = $invi['limit_invoice'];

  $header = array('No', 'Tanggal', 'Deskripsi', 'Qty', 'Harga');
  $data = array();
  $no = 1;
  // Mengambil hasil query dan memformat data
  $aserver = mysqli_query($con,"SELECT * FROM invoice_list WHERE id_invoice = '$page[1]'");
  while ($datainv = mysqli_fetch_assoc($aserver)) {
      // Menggunakan data dari hasil query untuk membuat array
      $data[] = array(
          $no,
          $datainv['tanggal'],
          $datainv['nama'],
          $datainv['quantity'],
          number_format($datainv['harga'])
      );
  $no++ ;
  }
  $subtotal = number_format($invi['total_list']);
  $lainnya = '0';
  $totalAmount = number_format($invi['total_list']);
  $terbilang = terbilang($invi['total_list']).' rupiah';

  // Membuat instance PDF
  require ("invoice/generate_invoice.php");
  $pdf = new PDF();
  $pdf->setHeaderData($nama, $alamat, $telepon, $logo);
  $pdf->AddPage();
  $pdf->SetFont('Arial', '', 12);
  // Menambahkan detail pelanggan dan invoice ke PDF
  $pdf->CustomerDetails($customerName, $customerAddress, $invoiceNumber, $invoiceDate, $customerNumber, $invoiceLimit);
  // Menambahkan tabel ke PDF        
  $pdf->InvoiceTable($header, $data);
  // Menambahkan total amount ke PDF
  $pdf->Total($subtotal,$lainnya,$totalAmount,$terbilang);
  // Menambahkan keterangan ke PDF
  $pdf->Keterangan($Keterangan);
  // Menambahkan pembuat ke PDF
  $pdf->Pembuat($Pembuat);
  // Output PDF ke browser
  $pdf->Output('F', 'invoice/inv_contoh/INV-'.$customerName.'-' . $invoiceNumber . '.pdf');

  echo '<script type="text/javascript">window.open("invoice/inv_contoh/INV-'.$customerName.'-' . $invoiceNumber . '.pdf", "_blank")</script>';
// // sweetAlert("Data berhasil di simpan", "success");
}
?>