<?php include 'template/header.php';?>
<?php 
$kodeCart = generate_invoice_number();
$umum_id = get_pelanggan_umum_id();
?>
<div class="row mt-3">

<div class="col-lg-3 mb-3">
    <div class="card small mb-3">
        <div class="card-header p-2">
            <div class="card-tittle"><i class="far fa-file mr-1"></i> Informasi Nota</div>
        </div>
        <div class="card-body p-2">
            <div class="row">
                <div class="col-4 mb-2 text-right pt-1 pr-1">No. Nota : </div>
                <div class="col-8 mb-2 pl-0">
                    <input type="text" class="form-control form-control-sm bg-white" value="<?php echo $kodeCart ?>" readonly>
                </div>
                <div class="col-4 mb-2 text-right pt-1 pr-1">Tanggal : </div>
                <div class="col-8 mb-2 pl-0">
                    <input type="text" class="form-control form-control-sm bg-white"  id="date-time" readonly>
                </div>
                <div class="col-4 text-right pt-1 pr-1">Kasir : </div>
                <div class="col-8 pl-0">
                    <input type="text" class="form-control form-control-sm bg-white" value="<?php echo $_SESSION['username'] ?>" readonly>
                </div>
            </div>
        </div>
    </div>

    <div class="card small mb-3">
        <div class="card-header p-2">
            <div class="card-tittle"><i class="far fa-user mr-1"></i> Informasi Pelanggan 
                <a class="float-right"href="#" onclick="TambahBaru()">
                    Tambah Baru ?
                </a>
            </div>
        </div>
        <div class="card-body p-2">
            <div style="display:none;width: 100%;" id="Tambah1">
            <?php
            if(isset($_POST['alamat_pelanggan']))
            {
                $nama_pelanggan = htmlspecialchars($_POST['nama_pelanggan']);
                $telepon_pelanggan = htmlspecialchars($_POST['telepon_pelanggan']);
                $alamat_pelanggan = htmlspecialchars($_POST['alamat_pelanggan']);

                $tambahPel = mysqli_query($conn,"INSERT INTO pelanggan(nama_pelanggan,telepon_pelanggan,alamat_pelanggan)
                 values ('$nama_pelanggan','$telepon_pelanggan','$alamat_pelanggan')");
                if ($tambahPel){
                    echo '<script>alert("Tambah Data Pelanggan Berhasil");window.location="index.php"</script>';
                } else {
                    echo '<script>alert("Maaf! data yang anda masukan salah.");history.go(-1);</script>';
                }
                
            };
            ?>
                <form method="post">
                    <div class="row">
                        <div class="col-4 mb-2 text-right pt-1 pr-1 text-primary">Pelanggan : </div>
                        <div class="col-8 mb-2 pl-0">
                            <input type="text" class="form-control form-control-sm" name="nama_pelanggan" required>
                        </div>
                        <div class="col-4 mb-2 text-right pt-1 pr-1 text-primary">Telepon : </div>
                        <div class="col-8 mb-2 pl-0">
                            <input type="number" class="form-control form-control-sm" name="telepon_pelanggan" required>
                        </div>
                        <div class="col-4 text-right pt-1 pr-1 text-primary">Alamat : </div>
                        <div class="col-8 pl-0">
                            <input type="text" class="form-control form-control-sm" name="alamat_pelanggan" onchange="form.submit()" required>
                        </div>
                    </div>
                </form>
            </div><!-- end tambah1 -->
            <div id="Ada1">
            <div class="row">
                <div class="col-4 mb-2 text-right pt-1 pr-1">Pelanggan : </div>
                <div class="col-8 mb-2 pl-0">
                <?php 
                    $plgn=mysqli_query($conn, "SELECT * FROM pelanggan order by idpelanggan ASC");
                    $jsArrayp = "var telepon_pelanggan = new Array();";
                    $jsArrayp1 = "var alamat_pelanggan = new Array();";
                    $jsArrayp2 = "var idpelanggan = new Array();";
                    ?>
                    <input type="text" class="form-control form-control-sm bg-white"  list="datalist2"
                 onchange="changeValuePelanggan(this.value)" value="UMUM" required>
                 <datalist id="datalist2">
                <?php  
                if(mysqli_num_rows($plgn)) {
                 while($row_p= mysqli_fetch_array($plgn)) {?>

                        <option value="<?php echo $row_p["nama_pelanggan"]?>"> <?php echo $row_p["nama_pelanggan"]?> 
                        
                                <?php 
                                $jsArrayp .= "telepon_pelanggan['" . $row_p['nama_pelanggan'] . "'] = {telepon_pelanggan:'" . addslashes($row_p['telepon_pelanggan']) . "'};";
                                $jsArrayp1 .= "alamat_pelanggan['" . $row_p['nama_pelanggan'] . "'] = {alamat_pelanggan:'" . addslashes($row_p['alamat_pelanggan']) . "'};"; 
                                $jsArrayp2 .= "idpelanggan['" . $row_p['nama_pelanggan'] . "'] = {idpelanggan:'" . addslashes($row_p['idpelanggan']) . "'};";
                                }
                                }
                                ?>
                    </datalist>
                </div>
                <div class="col-4 mb-2 text-right pt-1 pr-1">Telepon : </div>
                <div class="col-8 mb-2 pl-0">
                    <input type="text" class="form-control form-control-sm bg-white" id="telepon_pelanggan" readonly value="0">
                </div>
                <div class="col-4 text-right pt-1 pr-1">Alamat : </div>
                <div class="col-8 pl-0">
                    <input type="text" class="form-control form-control-sm bg-white" id="alamat_pelanggan" readonly value="UMUM">
                </div>
            </div>
            </div><!-- end ada1 -->
        </div>
    </div>
</div>

<div class="col-lg-9" id="print">
    <form id="myCartNew" method="post">
    <div class="row print-none">
        <div class="col-12 col-lg-3 m-pr-0">
        <?php 
            $barang=mysqli_query($conn, "SELECT * FROM produk ORDER BY idproduk ASC");
            $jsArray = "var harga_jual = new Array();";
            $jsArray1 = "var nama_produk = new Array();";
            $jsArray3 = "var idproduk = new Array();";
            $jsArray4 = "var stock = new Array();";
            ?>
            <label class="mb-1">Kode Produk / Barcode</label>
            <div class="input-group">
                <input type="text" class="form-control form-control-sm border-right-0" id="barcode_input" list="datalist1"
                 onchange="changeValue(this.value)" onkeypress="handleBarcodeInput(event)" aria-describedby="basic-addon2" required>
                <datalist id="datalist1">
                <?php  
                if(mysqli_num_rows($barang)) {
                 while($row_brg= mysqli_fetch_array($barang)) {?>

                        <option value="<?php echo $row_brg["kode_produk"]?>"> <?php echo $row_brg["kode_produk"]?> 
                        
                        <?php $jsArray .= "harga_jual['" . $row_brg['kode_produk'] . "'] = {harga_jual:'" . addslashes($row_brg['harga_jual']) . "'};";
                                $jsArray1 .= "nama_produk['" . $row_brg['kode_produk'] . "'] = {nama_produk:'" . addslashes($row_brg['nama_produk']) . "'};";
                                $jsArray3 .= "idproduk['" . $row_brg['kode_produk'] . "'] = {idproduk:'" . addslashes($row_brg['idproduk']) . "'};";
                                $jsArray4 .= "stock['" . $row_brg['kode_produk'] . "'] = {stock:'" . addslashes($row_brg['stock']) . "'};";
                                }
                                }
                                ?>
                    </datalist>
                    <div class="input-group-append">
                        <span class="input-group-text bg-white border-left-0 pr-1" id="basic-addon2">
                        <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" fill="currentColor" class="bi bi-upc-scan" viewBox="0 0 16 16">
                            <path d="M1.5 1a.5.5 0 0 0-.5.5v3a.5.5 0 0 1-1 0v-3A1.5 1.5 0 0 1 1.5 0h3a.5.5 0 0 1 0 1h-3zM11 .5a.5.5 0 0 1 .5-.5h3A1.5 1.5 0 0 1 16 1.5v3a.5.5 0 0 1-1 0v-3a.5.5 0 0 0-.5-.5h-3a.5.5 0 0 1-.5-.5zM.5 11a.5.5 0 0 1 .5.5v3a.5.5 0 0 0 .5.5h3a.5.5 0 0 1 0 1h-3A1.5 1.5 0 0 1 0 14.5v-3a.5.5 0 0 1 .5-.5zm15 0a.5.5 0 0 1 .5.5v3a1.5 1.5 0 0 1-1.5 1.5h-3a.5.5 0 0 1 0-1h3a.5.5 0 0 0 .5-.5v-3a.5.5 0 0 1 .5-.5zM3 4.5a.5.5 0 0 1 1 0v7a.5.5 0 0 1-1 0v-7zm2 0a.5.5 0 0 1 1 0v7a.5.5 0 0 1-1 0v-7zm2 0a.5.5 0 0 1 1 0v7a.5.5 0 0 1-1 0v-7zm2 0a.5.5 0 0 1 .5-.5h1a.5.5 0 0 1 .5.5v7a.5.5 0 0 1-.5.5h-1a.5.5 0 0 1-.5-.5v-7zm3 0a.5.5 0 0 1 1 0v7a.5.5 0 0 1-1 0v-7z"/>
                            </svg></span>
                    </div>
                </div>
        </div>
        <div class="col-6 col-lg-2 pr-0">
            <label class="mb-1">Nama Produk</label>
            <input type="hidden" class="form-control form-control-sm bg-white" name="idproduk" id="idproduk" readonly>
            <input type="text" class="form-control form-control-sm bg-white" id="nama_produk" readonly>
        </div>
        <div class="col-6 col-lg-2 m-pr-0">
            <label class="mb-1">Harga</label>
            <input type="text" class="form-control form-control-sm bg-white" id="harga_jual"
             onchange="total()">
        </div>
        <div class="col-6 col-lg-1 pr-0">
            <label class="mb-1">Stock</label>
            <input type="text" class="form-control form-control-sm bg-white" id="stock" readonly>
        </div>
        <div class="col-6 col-lg-1 m-pr-0">
            <label class="mb-1">Qty</label>
            <input type="number" class="form-control form-control-sm" id="quantity" onchange="updateQuantity()"
            name="quantity" placeholder="0" min="1" value="1" required>
        </div>
        <div class="col-lg-3">
            <label class="mb-1">Subtotal</label>
            <div class="input-group">
                <input type="number" class="form-control form-control-sm bg-white" id="subtotal" name="tambahcuy" onchange="total()" readonly>
            <div class="input-group-append">
                <button class="btn btn-success btn-sm border-0" type="button" onclick="addProduct()" id="addButton">
                    <i class="fa fa-plus mr-1"></i> Tambah</button>
                <button class="btn btn-danger btn-sm border-0 ml-1" type="reset" onclick="resetForm()">
                    <i class="fa fa-trash-restore-alt"></i> Reset</button>
            </div>
            </div>
        </div>
    </div><!-- end row -->
    
    <!-- Search Product Button -->
    <div class="row print-none mt-2">
        <div class="col-12">
            <button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#searchProductModal">
                <i class="fa fa-search mr-1"></i> Cari Produk Berdasarkan Nama
            </button>
        </div>
    </div>
</form>

<!-- Modal Pencarian Produk -->
<div class="modal fade" id="searchProductModal" tabindex="-1" role="dialog" aria-labelledby="searchProductModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="searchProductModalLabel"><i class="fa fa-search mr-2"></i>Cari Produk</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="form-group">
              <label for="searchInput">Cari Produk Berdasarkan Nama:</label>
              <input type="text" class="form-control" id="searchInput" placeholder="Ketik nama produk...">
          </div>
          
          <div class="table-responsive">
              <table class="table table-striped table-sm">
                  <thead>
                      <tr>
                          <th>Kode Produk</th>
                          <th>Nama Produk</th>
                          <th>Kategori</th>
                          <th>Stok</th>
                          <th>Harga Jual</th>
                          <th>Aksi</th>
                      </tr>
                  </thead>
                  <tbody id="searchResults">
                      <!-- Results will be populated by JavaScript -->
                  </tbody>
              </table>
          </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Tutup</button>
      </div>
    </div>
  </div>
</div>

<!-- Manual Product Input Section -->
<div class="row print-none mt-3">
    <div class="col-12">
        <div class="card">
            <div class="card-header p-2">
                <div class="card-tittle"><i class="fas fa-keyboard mr-1"></i> Input Produk Manual</div>
            </div>
            <div class="card-body p-2">
                <div class="row">
                    <div class="col-12 col-lg-4 mb-2">
                        <label>Nama Produk</label>
                        <input type="text" class="form-control form-control-sm" id="manual_nama_produk" placeholder="Nama Produk">
                    </div>
                    <div class="col-12 col-lg-3 mb-2">
                        <label>Harga Jual</label>
                        <input type="number" class="form-control form-control-sm" id="manual_harga_jual" placeholder="Harga" min="0">
                    </div>
                    <div class="col-12 col-lg-2 mb-2">
                        <label>Qty</label>
                        <input type="number" class="form-control form-control-sm" id="manual_quantity" value="1" min="1">
                    </div>
                    <div class="col-12 col-lg-3 mb-2">
                        <label>&nbsp;</label>
                        <button class="btn btn-primary btn-sm btn-block" onclick="addManualProduct()">
                            <i class="fa fa-plus mr-1"></i> Tambah Produk Manual</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php
if(isset($_POST['tambahcuy'])){
    $idproduk  = $_POST['idproduk'];
    $quantity  = $_POST['quantity'];
    $cekBarang = mysqli_query($conn, "SELECT * FROM produk WHERE idproduk='$idproduk'");
    $stocknya  = mysqli_fetch_array($cekBarang);
    $stock     = $stocknya['stock'];
    $sisa      = $stock-$quantity;
    
    if ($stock < $quantity) {
    echo '<script>alert("Oops! Jumlah pengeluaran lebih besar dari stok ...");window.location="index.php"</script>';
    }   
    else{
     $insert = mysqli_query($conn, "INSERT INTO keranjang (idproduk,quantity) VALUES ('$idproduk','$quantity')");
      if($insert){
        $upstok = mysqli_query($conn, "UPDATE produk SET stock='$sisa' WHERE idproduk='$idproduk'");
        echo '<script>window.location="index.php"</script>';
     }
    else { echo '<script>alert("ERROR.");history.go(-1);</script>';}
    }
}

// Update quantity in cart
if(isset($_POST['update_qty'])){
    $idcart = $_POST['idcart'];
    $new_qty = $_POST['new_quantity'];
    $idproduk = $_POST['idproduk'];
    
    // Get current product info
    $cekBarang = mysqli_query($conn, "SELECT * FROM produk WHERE idproduk='$idproduk'");
    $stocknya = mysqli_fetch_array($cekBarang);
    $current_qty = $stocknya['stock'] + $cekBarang['quantity']; // Get original stock
    
    if($new_qty > $current_qty) {
        echo '<script>alert("Jumlah melebihi stok yang tersedia!");</script>';
    } else {
        // Update cart quantity
        mysqli_query($conn, "UPDATE keranjang SET quantity='$new_qty' WHERE idcart='$idcart'");
        echo '<script>window.location="index.php"</script>';
    }
}
?>

<div class="d-none pt-5 px-4 print-show">
    <div class="row">
        <div class="col-12 text-center mb-2">
            <h1 style="font-size:60px;font-weight:700;"><?php echo $toko ?></h1>
            <h4 class="mb-0"><?php echo $alamat ?></h4>
            <h4 class="mb-2">Tel : <?php echo $telp  ?></h4>
        </div>
        <div class="col-7">
            <h3 class="mb-0" style="text-transform: uppercase;">INVOICE : <?php echo $kodeCart ?></h3>
            <h3 class="mb-0" style="text-transform: uppercase;">KASIR : <?php echo $_SESSION['username'] ?></h3>
        </div>
        <div class="col-5">
            <div class="row">
                <div class="col-6 text-right mb-1"><h3 class="mb-0">TANGGAL :</h3></div>
                <div class="col-6 pl-1 mb-1"><h3 class="mb-0"><?php echo date('d-m-Y') ?></h3></div>
                <div class="col-6 text-right"><h3 class="mb-0">PUKUL :</h3></div>
                <div class="col-6 pl-1"><h3 class="mb-0" id="jam-print"></h3></div>
            </div>
        </div>
        <div class="col-12 bg-secondary border my-3"></div>
        <div class="col-12 mb-3">
            <div class="row">
                <div class="col-1 text-center"><h3 style="font-weight:700;">QTY</h3></div>
                <div class="col"><h3 style="font-weight:700;">PRODUK</h3></div>
                <div class="col text-center"><h3 style="font-weight:700;">HARGA</h3></div>
                <div class="col text-right"><h3 style="font-weight:700;">SUBTOTAL</h3></div>
            </div>
        </div>
        <?php
            $subtotalcart2= 0;
            $no=1;
            $data_produk1=mysqli_query($conn,"SELECT * FROM keranjang c, produk p
            WHERE p.idproduk=c.idproduk ORDER BY idcart ASC");
            while ($c = $data_produk1->fetch_assoc()) {
                $subtotalcart3 = $c['harga_jual'] * $c['quantity'];
                $subtotalcart2 += $subtotalcart3;
                ?>
        <div class="col-12 mb-2">
            <div class="row">
                <div class="col-1 text-center"><h3><?php echo $c['quantity'] ?></h3></div>
                <div class="col"><h3><?php echo $c['nama_produk'] ?></h3></div>
                <div class="col text-center"><h3>Rp.<?php echo ribuan($c['harga_jual']) ?></h3></div>
                <div class="col text-right"><h3>Rp.<?php echo ribuan($subtotalcart3) ?></h3></div>
            </div>
        </div>
        <?php }?>
        <div class="col-12 bg-secondary border my-3"></div>
        <div class="col-12">
            <h3>Total Belanja <span class="float-right">Rp.<?php echo ribuan($subtotalcart2) ?></span></h3>
            <h3>Tunai <div class="float-right">Rp.<span id="bayarnya1"></span></div></h3>
            <h3>Kembali <div class="float-right">Rp.<span id="total2"></span></div></h3>
        </div>
        <div class="col-12 bg-secondary border my-3"></div>
        <div class="col-12">
            <h4>Catatan : <span id="new_catatan"></span></h4>
        </div>
        <div class="col-12 bg-secondary border my-3"></div>
        <div class="col-12 text-center">
            <h3>* Terima Kasih Telah Berbelanja Di Toko Kami *</h3>
            <p class="h4 text-muted">Powered By.Ghanim Software</p>
        </div>
    </div><!-- end row -->
</div><!-- end box print -->

    <table class="table table-striped table-sm table-bordered dt-responsive nowrap print-none" id="cart" width="100%">
                        <thead>
                        <tr>
                            <th>#</th>
                            <th>Kode Produk</th>
                            <th>Nama Produk</th>
                            <th>Harga</th>
                            <th>Qty</th>
                            <th>Subtotal</th>
                            <th>Opsi</th>
                        </tr>
                        </thead>
                        <tbody>
                            <?php
                                $subtotalcart1= 0;
                                $no=1;
                                $data_produk=mysqli_query($conn,"SELECT * FROM keranjang c, produk p
                                WHERE p.idproduk=c.idproduk ORDER BY idcart ASC");
                                while ($d = $data_produk->fetch_assoc()) {
                                    $idcart = $d['idcart'];
                                    $subtotalcart = $d['harga_jual'] * $d['quantity'];
                                    $subtotalcart1 += $subtotalcart;
                                    ?>
                                    
                                    <tr>
                                        <td><?php echo $no++ ?></td>
                                        <td><?php echo $d['kode_produk'] ?></td>
                                        <td><?php echo $d['nama_produk'] ?></td>
                                        <td>Rp.<?php echo ribuan($d['harga_jual']) ?></td>
                                        <td>
                                            <input type="number" class="form-control form-control-sm" value="<?php echo $d['quantity'] ?>" onchange="updateCartItem(<?php echo $idcart . ',' . $d['idproduk'] ?>, this.value)" min="1">
                                        </td>
                                        <td>Rp.<?php echo ribuan($subtotalcart) ?></td>
                                        <td>
                                            <form method="post" class="d-inline-block">
                                            <input type="hidden" name="idpr" value="<?php echo $d['idproduk'] ?>">
                                            <input type="hidden" name="idcc" value="<?php echo $d['idcart'] ?>">
                                                <input type="hidden" name="jml" value="<?php echo $d['quantity'] ?>">
                                                <button type="submit" name="upone" class="btn btn-danger btn-xs"><i class="fa fa-trash fa-xs mr-1"></i>Hapus</a></button>
                                            </form>
                                        </td>
                                    </tr>		
                        <?php 
                        if(isset($_POST['upone'])){
                            $idcartt = $_POST['idcc'];
                            $idproduk = $_POST['idpr'];
                            $jml = $_POST['jml'];
                        
                            $cekBarang1 = mysqli_query($conn, "SELECT * FROM produk WHERE idproduk='$idproduk'");
                            $stocknya1  = mysqli_fetch_array($cekBarang1);
                            $stockk     = $stocknya1['stock'];
                            //menghitung sisa stok
                            $sisa1    =$stockk+$jml;
                            
                            if ($stockk < $jml) {
                        
                            }
                            //proses    
                            else{
                                $insert1 = mysqli_query($conn, "UPDATE produk SET stock='$sisa1' WHERE idproduk='$idproduk'");
                                    if($insert1){
                                        //update stok
                                        $hapus_data = mysqli_query($conn, "DELETE FROM keranjang WHERE idcart ='$idcartt'");
                                        echo '<script>alert("Data Berhasil Di Hapus");window.location="index.php"</script>';
                                    } else {
                                        echo '<script>alert("ERROR.");history.go(-1);</script>';
                                    }
                            }
                            }
                    }?>
					</tbody>
                </table>
                <div class="bg-total p-2 text-right print-none">
                    Rp.<?php echo ribuan($subtotalcart1) ?>
                </div>
                <form method="POST" class="mt-2 print-none">
                    <div class="row">
                        <div class="col-lg-7 mb-2">
                            <textarea name="catatan" class="form-control form-control-sm" id="catatan_baru"
                            placeholder="Catatan Transaksi (Jika Ada)"cols="10" rows="5" onchange="new_catatan()"></textarea>
                        </div>
                        <div class="col-lg-5 mb-2 print-none">
                            <div class="row">
                            <div class="col-5 mb-2 text-right pt-1 pr-2" style="font-weight:500;">Pembayaran :</div>
                                <div class="col-7 mb-2 pl-0">
                                    <input type="hidden" name="no_nota" value="<?php echo $kodeCart ?>">
                                    <input type="hidden" name="idpelanggan" id="idpelanggan" value="<?php echo $umum_id; ?>" required>
                                    <input type="hidden" name="totalbeli" value="<?php echo $subtotalcart1 ?>" id="hargatotal">
                                    <input type="number" class="form-control form-control-sm bg-white" placeholder="0"
                                    name="pembayaran" id="bayarnya" onchange="totalnya()" required>
                                </div>
                                <div class="col-5 text-right pt-1 pr-2" style="font-weight:500;">Kembalian :</div>
                                <div class="col-7 pl-0">
                                    <input type="text" class="form-control form-control-sm bg-white" 
                                    placeholder="0" name="kembalian" id="total1" readonly>
                            </div>
                            </div>
                            <div class="col-12 text-right pr-0 mt-2">
                            <button type="button" class="btn btn-light btn-sm px-3" onclick="prosesCetak()">
                                    <i class="fa fa-print mr-1"></i> Cetak
                                </button>
                                <button type="reset" class="btn btn-danger btn-sm px-3">
                                    <i class="fa fa-trash-restore-alt mr-1"></i> Reset
                                </button>
                                <button type="submit" name="save" class="btn btn-primary btn-sm px-3">
                                    <i class="far fa-file mr-1"></i> Simpan
                                </button>
                            </div>
                        </div>
                    </div><!-- end row -->
                </form>
                </div><!-- end print -->
</div><!-- end col-lg-9 -->

</div><!-- end row -->
<b><marquee bgcolor="orange" >SELAMAT DATANG DI INDOMART | MOHON AGAR TELITI ULANG BARANG YANG SUDAH DI INPUT | INDOMART BELANJA PUAS HARGA PAS KALI YA </marquee></b>
<?php 
if(isset($_POST['save']) || isset($_POST['save_print'])){
    $nonota = $_POST['no_nota'];
    $idpell = $_POST['idpelanggan'];
    $pembayaran= $_POST['pembayaran'];
    $kembalian = $_POST['kembalian'];
    $totalbeli = $_POST['totalbeli'];
    $catatan = htmlspecialchars($_POST['catatan']);

    $updatetkeranjang = mysqli_query($conn,"UPDATE keranjang SET
      no_nota='$nonota'") 
     or die (mysqli_connect_error()); 

     $ambildatacart = mysqli_query($conn,"INSERT INTO tb_nota
     (no_nota,idproduk,quantity)
    SELECT no_nota,idproduk,quantity FROM keranjang") 
     or die (mysqli_connect_error()); 

     $insertlaporan = mysqli_query($conn, "INSERT INTO laporan (no_nota,idpelanggan,catatan,pembayaran,kembalian,totalbeli)
      VALUES ('$nonota','$idpell','$catatan','$pembayaran','$kembalian','$totalbeli')");

    $hapusdatacart = mysqli_query($conn,"DELETE FROM keranjang");
    
    if($updatetkeranjang&&$ambildatacart&&$insertlaporan ){
        if(isset($_POST['save_print'])){
            // If from print action, redirect to print page
            echo '<script>window.location="cetak.php?nota='.$nonota.'"</script>';
        } else {
            // If from save action, return to index
            echo '<script>window.location="index.php"</script>';
        }
    } else {
        echo '<script>window.location="index.php"</script>';
    }

}
?>

<script type="text/javascript">
      <?php echo $jsArray,$jsArray1,$jsArray3,$jsArray4,$jsArrayp,$jsArrayp1,$jsArrayp2; ?>
    function changeValue(kode_produk) {
      document.getElementById("nama_produk").value = nama_produk[kode_produk].nama_produk;
      document.getElementById("harga_jual").value = harga_jual[kode_produk].harga_jual;
      document.getElementById("idproduk").value = idproduk[kode_produk].idproduk;
      document.getElementById("stock").value = stock[kode_produk].stock;
      total();
    };

function total() {
   var harga =  parseInt(document.getElementById('harga_jual').value) || 0;
   var jumlah_beli =  parseInt(document.getElementById('quantity').value) || 0;
   var jumlah_harga = harga * jumlah_beli;
    document.getElementById('subtotal').value = jumlah_harga;
  }

function addProduct() {
    var idproduk = document.getElementById("idproduk").value;
    var quantity = document.getElementById("quantity").value;
    var subtotal = document.getElementById("subtotal").value;
    
    if(idproduk && quantity && parseInt(quantity) > 0) {
        // Send data via AJAX to add to cart
        var form = document.createElement("form");
        form.method = "POST";
        form.style.display = "none";
        
        var inputId = document.createElement("input");
        inputId.type = "hidden";
        inputId.name = "idproduk";
        inputId.value = idproduk;
        form.appendChild(inputId);
        
        var inputQty = document.createElement("input");
        inputQty.type = "hidden";
        inputQty.name = "quantity";
        inputQty.value = quantity;
        form.appendChild(inputQty);
        
        var inputSubtotal = document.createElement("input");
        inputSubtotal.type = "hidden";
        inputSubtotal.name = "tambahcuy";
        inputSubtotal.value = subtotal;
        form.appendChild(inputSubtotal);
        
        document.body.appendChild(form);
        form.submit();
    } else {
        alert("Silakan pilih produk dan masukkan jumlah yang valid!");
    }
}

function updateQuantity() {
    total();
}

function resetForm() {
    document.getElementById("barcode_input").value = "";
    document.getElementById("nama_produk").value = "";
    document.getElementById("harga_jual").value = "";
    document.getElementById("idproduk").value = "";
    document.getElementById("stock").value = "";
    document.getElementById("quantity").value = "1";
    document.getElementById("subtotal").value = "";
}

function handleBarcodeInput(event) {
    if(event.keyCode === 13) { // Enter key
        event.preventDefault();
        var barcode = document.getElementById("barcode_input").value;
        if(barcode) {
            // Check if it's a product barcode
            var xhr = new XMLHttpRequest();
            xhr.open("GET", "get_product_by_barcode.php?barcode=" + barcode, true);
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    var response = JSON.parse(xhr.responseText);
                    if(response.success) {
                        document.getElementById("idproduk").value = response.idproduk;
                        document.getElementById("nama_produk").value = response.nama_produk;
                        document.getElementById("harga_jual").value = response.harga_jual;
                        document.getElementById("stock").value = response.stock;
                        document.getElementById("quantity").value = "1"; // Reset quantity to 1
                        
                        // Auto-calculate subtotal
                        total();
                        
                        // Auto-add to cart
                        addProduct();
                    } else {
                        // If not found as product, it might be a manual product or error
                        alert("Barcode tidak ditemukan. Silakan cek kembali.");
                    }
                }
            };
            xhr.send();
        }
    }
}

function addManualProduct() {
    var nama_produk = document.getElementById("manual_nama_produk").value;
    var harga_jual = document.getElementById("manual_harga_jual").value;
    var quantity = document.getElementById("manual_quantity").value;
    
    if(nama_produk && harga_jual && quantity) {
        // Send data via AJAX to add manual product
        var xhr = new XMLHttpRequest();
        xhr.open("POST", "add_manual_product.php", true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4 && xhr.status == 200) {
                var response = JSON.parse(xhr.responseText);
                if(response.success) {
                    // Add to cart
                    var form = document.createElement("form");
                    form.method = "POST";
                    form.style.display = "none";
                    
                    var inputId = document.createElement("input");
                    inputId.type = "hidden";
                    inputId.name = "idproduk";
                    inputId.value = response.idproduk;
                    form.appendChild(inputId);
                    
                    var inputQty = document.createElement("input");
                    inputQty.type = "hidden";
                    inputQty.name = "quantity";
                    inputQty.value = quantity;
                    form.appendChild(inputQty);
                    
                    var inputSubtotal = document.createElement("input");
                    inputSubtotal.type = "hidden";
                    inputSubtotal.name = "tambahcuy";
                    inputSubtotal.value = harga_jual * quantity;
                    form.appendChild(inputSubtotal);
                    
                    document.body.appendChild(form);
                    form.submit();
                } else {
                    alert(response.message || "Gagal menambahkan produk manual");
                }
            }
        };
        xhr.send("nama_produk=" + encodeURIComponent(nama_produk) + "&harga_jual=" + harga_jual);
    } else {
        alert("Silakan lengkapi semua field produk manual!");
    }
}

function updateCartItem(idcart, idproduk, newQty) {
    // Send AJAX request to update quantity
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "update_cart_qty.php", true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
            var response = JSON.parse(xhr.responseText);
            if(response.success) {
                // Refresh page to update cart
                window.location.reload();
            } else {
                alert(response.message || "Gagal mengupdate jumlah");
            }
        }
    };
    xhr.send("idcart=" + idcart + "&idproduk=" + idproduk + "&new_quantity=" + newQty);
}

function totalnya() {
   var harga =  parseInt(document.getElementById('hargatotal').value);
   var pembayaran =  parseInt(document.getElementById('bayarnya').value);
   var kembali = pembayaran - harga;
    document.getElementById('total1').value = kembali;
    document.getElementById('total2').innerHTML = kembali;
    document.getElementById('bayarnya1').innerHTML = pembayaran;
}

function changeValuePelanggan(nama_pelanggan) {
    if(nama_pelanggan === "UMUM") {
        document.getElementById("telepon_pelanggan").value = "0";
        document.getElementById("alamat_pelanggan").value = "UMUM";
        document.getElementById("idpelanggan").value = <?php echo $umum_id; ?>;
    } else {
        document.getElementById("telepon_pelanggan").value = telepon_pelanggan[nama_pelanggan].telepon_pelanggan;
        document.getElementById("alamat_pelanggan").value = alamat_pelanggan[nama_pelanggan].alamat_pelanggan;
        document.getElementById("idpelanggan").value = idpelanggan[nama_pelanggan].idpelanggan;
    }
};

function new_catatan() {
    var c = document.getElementById("catatan_baru").value;
    document.getElementById("new_catatan").innerHTML = c;
}

// Barcode scanning functionality
document.addEventListener('DOMContentLoaded', function() {
    // Focus on barcode input when page loads
    document.getElementById('barcode_input').focus();
    
    // Barcode scanning logic
    let barcodeInput = '';
    let barcodeTimer;
    
    document.getElementById('barcode_input').addEventListener('keypress', function(e) {
        if (e.key === 'Enter') {
            // If input from scanner (usually ends with Enter), reset timer
            clearTimeout(barcodeTimer);
            // Process collected barcode
            if (barcodeInput) {
                this.value = barcodeInput;
                // Trigger the changeValue function to populate product info
                changeValue(barcodeInput);
                barcodeInput = '';
            }
        } else {
            // Add character to temporary input
            barcodeInput += e.key;
            // Reset timer every time there's input
            clearTimeout(barcodeTimer);
            barcodeTimer = setTimeout(function() {
                barcodeInput = '';
            }, 100); // Reset after 100ms of no input
        }
    });
});

// Product search functionality
document.addEventListener('DOMContentLoaded', function() {
    const searchInput = document.getElementById('searchInput');
    const searchResults = document.getElementById('searchResults');
    
    // Load all products initially
    loadAllProducts();
    
    searchInput.addEventListener('input', function() {
        const searchTerm = this.value.toLowerCase();
        
        if (searchTerm.length === 0) {
            loadAllProducts();
        } else {
            searchProducts(searchTerm);
        }
    });
    
    function loadAllProducts() {
        fetch('get_all_products.php')
        .then(response => response.json())
        .then(data => {
            displayProducts(data.products);
        })
        .catch(error => console.error('Error:', error));
    }
    
    function searchProducts(searchTerm) {
        fetch('search_products.php?term=' + encodeURIComponent(searchTerm))
        .then(response => response.json())
        .then(data => {
            displayProducts(data.products);
        })
        .catch(error => console.error('Error:', error));
    }
    
    function displayProducts(products) {
        searchResults.innerHTML = '';
        
        if (products.length === 0) {
            searchResults.innerHTML = '<tr><td colspan="6" class="text-center">Produk tidak ditemukan</td></tr>';
            return;
        }
        
        products.forEach(function(product) {
            const row = document.createElement('tr');
            row.innerHTML = `
                <td>${product.kode_produk}</td>
                <td>${product.nama_produk}</td>
                <td>${product.nama_kategori}</td>
                <td>${product.stock}</td>
                <td>Rp. ${product.harga_jual}</td>
                <td>
                    <button class="btn btn-primary btn-xs" onclick="selectProduct(${product.idproduk}, '${product.nama_produk}', ${product.harga_jual}, ${product.stock}, '${product.kode_produk}')">
                        <i class="fa fa-plus mr-1"></i>Pilih
                    </button>
                </td>
            `;
            searchResults.appendChild(row);
        });
    }
});

// Function to select product from search results
function selectProduct(idproduk, nama_produk, harga_jual, stock, kode_produk) {
    document.getElementById('idproduk').value = idproduk;
    document.getElementById('nama_produk').value = nama_produk;
    document.getElementById('harga_jual').value = harga_jual;
    document.getElementById('stock').value = stock;
    document.getElementById('barcode_input').value = kode_produk;
    
    // Auto-calculate subtotal
    total();
    
    // Auto-add to cart
    addProduct();
    
    // Close modal
    $('#searchProductModal').modal('hide');
}

// Auto-add when quantity changes (for quick adding)
document.getElementById('quantity').addEventListener('change', function() {
    // Update subtotal when quantity changes
    total();
});

// Auto-add when product is selected from datalist
document.getElementById('barcode_input').addEventListener('change', function() {
    // Only auto-add if the product was selected from datalist (not typed manually)
    if(this.value && document.getElementById('idproduk').value) {
        // Update subtotal
        total();
    }
});

// Function to process print - save transaction and then print
function prosesCetak() {
    // Get form data
    var no_nota = document.querySelector('input[name="no_nota"]').value;
    var idpelanggan = document.getElementById('idpelanggan').value;
    var totalbeli = document.getElementById('hargatotal').value;
    var pembayaran = document.getElementById('bayarnya').value;
    var kembalian = document.getElementById('total1').value;
    var catatan = document.getElementById('catatan_baru').value;
    
    // Validate required fields
    if (!pembayaran || pembayaran == 0) {
        alert('Silakan masukkan jumlah pembayaran terlebih dahulu!');
        return;
    }
    
    // Create form to submit data to save transaction
    var form = document.createElement('form');
    form.method = 'POST';
    form.style.display = 'none';
    
    var inputs = {
        'no_nota': no_nota,
        'idpelanggan': idpelanggan,
        'totalbeli': totalbeli,
        'pembayaran': pembayaran,
        'kembalian': kembalian,
        'catatan': catatan,
        'save_print': '1'  // Flag to indicate this is from print action
    };
    
    for (var name in inputs) {
        var input = document.createElement('input');
        input.type = 'hidden';
        input.name = name;
        input.value = inputs[name];
        form.appendChild(input);
    }
    
    document.body.appendChild(form);
    form.submit();
}

// Change button text to indicate auto-add functionality
document.getElementById('addButton').innerHTML = '<i class="fa fa-plus mr-1"></i> Tambah Otomatis';
</script>

<script type="text/javascript">
timer();
function timer(){
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date+' '+time;
document.getElementById('date-time').value = dateTime;
document.getElementById('jam-print').innerHTML = time;
setTimeout(timer,1000);
        }
  </script>
  <script>
function TambahBaru() {
  var x = document.getElementById("Ada1");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
  var y = document.getElementById("Tambah1");
  if (y.style.display === "block") {
    y.style.display = "none";
  } else {
    y.style.display = "block";
  }
}
</script>
<?php include 'template/footer.php';?>