<?php 
$stmt = $pdo->prepare("SELECT 
  id,
  nama_perusahaan,
  telepon_perusahaan,
  alamat_perusahaan,
  nama_bendahara,
  catatan_perusahaan,
  logo_perusahaan 
  FROM data_server WHERE id = '$SvId'");
$stmt->execute();
$data = $stmt->fetchAll();

// echo "<pre>";
// print_r($data);
// echo "</pre>";
?>
<section class="content" style="zoom: 90%;">
  	<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">
              				<h5>Invoice Setting</h5>
            				</div>
            				<div class="col-sm-6">
            				</div>
          			</div>
        			</div>
        			<!-- /.card-header -->
        			<div class="card-body">
        				<div class="row">
        					<div class="col-md-4">
        						<form method="post" enctype="mulipart/form-data" id="invoiceForm">
                    <div class="form-group">
                      <label>Nama Perusahaan</label>
                      <input type="text" name="nama_perusahaan" value="<?php echo $data[0]['nama_perusahaan']; ?>" class="form-control">
                    </div>
                    <div class="form-group">
                      <label>Alamat Perusahaan</label>
                      <input type="text" name="alamat_perusahaan" value="<?php echo $data[0]['alamat_perusahaan']; ?>" class="form-control">
                    </div>
                    <div class="form-group">
                      <label>Telepon Perusahaan</label>
                      <input type="text" name="telepon_perusahaan" value="<?php echo $data[0]['telepon_perusahaan']; ?>" class="form-control">
                    </div>
                    <div class="form-group">
                      <label>Catatan</label>
                      <textarea class="form-control" rows="4" name="catatan_perusahaan"><?php echo $data[0]['catatan_perusahaan']; ?></textarea>
                      <!-- <input type="text" name="Catatan" class="form-control"> -->
                    </div>
                    <div class="form-group">
                      <label>Nama Bendahara</label>
                      <input type="text" name="nama_bendahara" value="<?php echo $data[0]['nama_bendahara']; ?>" class="form-control">
                    </div>
		          		</div>
		          		<div class="col-md-3">
		          			<label>Logo Invoice</label>
                    <div class="uploadImage" data-id="invoice-<?php echo $data[0]['id']; ?>">
                      <span>Click to upload image</span>
                      <img src="#" alt="Image preview">
                      <div class="progressContainer">
                        <div class="progressBar"></div>
                      </div>
                    </div>
                    <i>Harus Format JPG / JPEG Ratio 1 x 1</i>
                    <div class="form-group mt-4" style="text-align: right;">
                      <input type="hidden" name="mode" value="edit">
                      <input type="hidden" name="id" value="<?php echo $data[0]['id']; ?>">
                      <input type="submit" onclick="generateInvoice()" name="edit" value="Simpan" class="btn btn-primary btn-sm m-1">
                    </div>
                    </form>
		          		</div>
                  <div class="col-md-5">
                    <center>
                      <?php if (file_exists("invoice/inv_contoh/inv-server-".$SvId.".pdf")) { ?>
                        <iframe src="invoice/inv_contoh/inv-server-<?php echo $SvId; ?>.pdf" width="80%" height="700px" style="zoom: 80%;"></iframe>
                      <?php } ?>
                    </center>
                  </div>
		          	</div>
	          	</div>
			     </div>
  			</div>
		</div>
	</div>
</section>

<?php 
if (isset($_POST['edit'])) {
  $logo = $_SESSION['uploaded_files']['invoice-'.$data[0]['id']];

  try {
    $stmt = $pdo->prepare("UPDATE data_server SET
    nama_perusahaan = :nama_perusahaan,
    telepon_perusahaan = :telepon_perusahaan,
    alamat_perusahaan = :alamat_perusahaan, 
    nama_bendahara = :nama_bendahara,
    catatan_perusahaan = :catatan_perusahaan,
    logo_perusahaan = :logo_perusahaan
    WHERE id = :id");

    $stmt->bindParam(':nama_perusahaan', $_POST['nama_perusahaan']);
    $stmt->bindParam(':telepon_perusahaan', $_POST['telepon_perusahaan']);
    $stmt->bindParam(':alamat_perusahaan', $_POST['alamat_perusahaan']);
    $stmt->bindParam(':nama_bendahara', $_POST['nama_bendahara']);
    $stmt->bindParam(':catatan_perusahaan', $_POST['catatan_perusahaan']);
    $stmt->bindParam(':logo_perusahaan', $logo);
    $stmt->bindParam(':id', $SvId);
    $stmt->execute();

    //Data PDF POST
    $nama = $_POST['nama_perusahaan'];
    $alamat = $_POST['alamat_perusahaan'];
    $telepon = $_POST['telepon_perusahaan'];// Detail pelanggan dan invoice
    $customerName = 'Nama Customer';
    $customerAddress = 'Alamat Customer';
    $invoiceNumber = 'INV20240001';
    $invoiceDate = date('Y-m-d');
    $invoiceLimit = "2045-08-17";
    $customerNumber = "08xxxxxxxxxxxx";
    $Keterangan = $_POST['catatan_perusahaan'];
    $Pembuat = $_POST['nama_bendahara'];
    
    // Total Amount (sederhana, untuk demo)
    $subtotal = '27.000.000';
    $lainnya = '0';
    $totalAmount = '27.000.000'; // Contoh jumlah total
    $terbilang = 'Dua Puluh Tujuh Juta Rupiah';
    $header = array('No', 'Tanggal', 'Deskripsi', 'Qty', 'Harga');

    $data = array(
        array("1", "2024-10-10", "Item Invoice 0001", "10 Pcs", "22.000.000"),
        array("2", "2024-10-10", "Item Invoice 0002", "10 Roll", "2.000.000"),
        array("3", "2024-10-10", "Item Invoice 0003", "150 Client", "1.500.000"),
        array("4", "2024-10-10", "Item Invoice 0004", "10 Pcs", "1.000.000"),
        array("5", "2024-10-10", "Item Invoice 0005", "10 Pcs", "500.000"),
    );


    // 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-server-' . $SvId . '.pdf');
        
    sweetAlert("Data berhasil di simpan", "success");
  } catch (PDOException $e) {
    // Handle error
    echo 'Error: ' . $e->getMessage();
  }
}

$_SESSION['uploaded_files']['invoice-'.$data[0]['id']] = $data[0]['logo_perusahaan'];

?>