<script>
    function sum() {
        var stok = document.getElementById('stok').value;
        var jumlahmasuk = document.getElementById('jumlahmasuk').value;
        var result = parseInt(stok) + parseInt(jumlahmasuk);
        if (!isNaN(result)) {
            document.getElementById('jumlah').value = result;
        }
    }
</script>

<?php
$koneksi = new mysqli("localhost", "bpjw9637_inventori", "inventori123", "bpjw9637_inventori");

// Periksa koneksi
if ($koneksi->connect_error) {
    die("Koneksi gagal: " . $koneksi->connect_error);
}

$no = mysqli_query($koneksi, "SELECT id_transaksi FROM barang_masuk ORDER BY id_transaksi DESC LIMIT 1");
$idtran = mysqli_fetch_array($no);
$kode = isset($idtran['id_transaksi']) ? $idtran['id_transaksi'] : 'TRM-000000';

$urut = substr($kode, 8, 3);
$tambah = (int)$urut + 1;
$bulan = date("m");
$tahun = date("y");

if (strlen($tambah) == 1) {
    $format = "TRM-" . $bulan . $tahun . "00" . $tambah;
} elseif (strlen($tambah) == 2) {
    $format = "TRM-" . $bulan . $tahun . "0" . $tambah;
} else {
    $format = "TRM-" . $bulan . $tahun . $tambah;
}

$tanggal_masuk = date("Y-m-d");
?>

<div class="container-fluid">
    <!-- DataTales Example -->
    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">Tambah Barang Masuk</h6>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <div class="body">
                    <form method="POST" enctype="multipart/form-data">
                        <label for="">Id Transaksi</label>
                        <div class="form-group">
                            <div class="form-line">
                                <input type="text" name="id_transaksi" class="form-control" id="id_transaksi" value="<?php echo $format; ?>" readonly />
                            </div>
                        </div>

                        <label for="">Tanggal Masuk</label>
                        <div class="form-group">
                            <div class="form-line">
                                <input type="date" name="tanggal_masuk" class="form-control" id="tanggal_masuk" value="<?php echo $tanggal_masuk; ?>" />
                            </div>
                        </div>

                        <label for="">Barang</label>
                        <div class="form-group">
                            <div class="form-line">
                                <select name="barang" id="cmb_barang" class="form-control">
                                    <option value="">-- Pilih Barang  --</option>
                                    <?php
                                    $sql = $koneksi->query("SELECT * FROM gudang ORDER BY kode_barang");
                                    while ($data = $sql->fetch_assoc()) {
                                        echo "<option value='" . $data['kode_barang'] . "." . $data['nama_barang'] . "'>" . $data['kode_barang'] . " | " . $data['nama_barang'] . "</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>

                        <div class="tampung"></div>

                        <label for="">Jumlah</label>
                        <div class="form-group">
                            <div class="form-line">
                                <input type="text" name="jumlahmasuk" id="jumlahmasuk" onkeyup="sum()" class="form-control" />
                            </div>
                        </div>

                        <label for="jumlah">Total Stok</label>
                        <div class="form-group">
                            <div class="form-line">
                                <input readonly="readonly" name="jumlah" id="jumlah" type="number" class="form-control">
                            </div>
                        </div>

                        <div class="tampung1"></div>

                        <label for="">Supplier</label>
                        <div class="form-group">
                            <div class="form-line">
                                <select name="pengirim" class="form-control">
                                    <option value="">-- Pilih Supplier  --</option>
                                    <?php
                                    $sql = $koneksi->query("SELECT * FROM tb_supplier ORDER BY nama_supplier");
                                    while ($data = $sql->fetch_assoc()) {
                                        echo "<option value='" . $data['nama_supplier'] . "'>" . $data['nama_supplier'] . "</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>

                        <!-- Tambahkan input 'satuan' jika diperlukan -->
                       

                        <input type="submit" name="simpan" value="Simpan" class="btn btn-primary">
                    </form>

                    <?php
                    if (isset($_POST['simpan'])) {
                        $id_transaksi = $koneksi->real_escape_string($_POST['id_transaksi']);
                        $tanggal = $koneksi->real_escape_string($_POST['tanggal_masuk']);

                        $barang = $koneksi->real_escape_string($_POST['barang']);
                        $pecah_barang = explode(".", $barang);
                        $kode_barang = isset($pecah_barang[0]) ? $koneksi->real_escape_string($pecah_barang[0]) : '';
                        $nama_barang = isset($pecah_barang[1]) ? $koneksi->real_escape_string($pecah_barang[1]) : '';

                        $jumlah = isset($_POST['jumlahmasuk']) ? (int)$_POST['jumlahmasuk'] : 0;
                        $satuan = isset($_POST['satuan']) ? $koneksi->real_escape_string($_POST['satuan']) : '';
                        $pengirim = isset($_POST['pengirim']) ? $koneksi->real_escape_string($_POST['pengirim']) : '';

                        // Validasi data sebelum disimpan
                        if (!empty($id_transaksi) && !empty($tanggal) && !empty($kode_barang) && !empty($nama_barang) && !empty($jumlah) && !empty($satuan) && !empty($pengirim)) {
                            $sql_insert = "INSERT INTO barang_masuk (id_transaksi, tanggal, kode_barang, nama_barang, jumlah, satuan, pengirim) VALUES ('$id_transaksi', '$tanggal', '$kode_barang', '$nama_barang', '$jumlah', '$satuan', '$pengirim')";
                            $sql = $koneksi->query($sql_insert);

                            if ($sql) {
                                echo "<script>alert('Simpan Data Berhasil'); window.location.href='?page=barangmasuk';</script>";
                            } else {
                                echo "<script>alert('Simpan Data Gagal: " . $koneksi->error . "');</script>";
                            }
                        } else {
                            echo "<script>alert('Semua field harus diisi.');</script>";
                        }
                    }

                    // Tutup koneksi
                    $koneksi->close();
                    ?>
                </div>
            </div>
        </div>
    </div>
</div>
