<?php
    include('session.php');
    
    $id = $_GET['id'];

    $sql = "SELECT * FROM $tblcompany WHERE Prefix='$id'";
    
    $result = mysqli_query($conn, $sql);
        
        if($row = mysqli_fetch_assoc($result)){
            $prefix  = $row['Prefix'];
            $company = $row['Company'];
        }

    date_default_timezone_set('Asia/Kuala_Lumpur');

    $month = date('m');
    $day = date('d');
    $strmonth = date('m');
    $year = date('Y');

    $todaydate =  date('Y') . '-' . date('m') . '-' . date('d');
    $todaydatedisplay =  date('d') . '-' . date('m') . '-' . date('Y');
    
    $d = cal_days_in_month(CAL_GREGORIAN,date('m'),date('Y'));

    $begindate = date('Y') . '-' . date('m') . '-' .'01';
    $begindate = date('Y') . '-' . "01" . '-' .'01';
    $enddate = date('Y') . '-' . date('m') . '-' .$d;

    $displaybegindate = "01-". date('m') . '-' . date('Y');
    $displayenddate = $d."-". date('m') . '-' . date('Y') ;

?>


<!DOCTYPE html>
<html lang="en" class="h-100">
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, shrink-to-fit=9">
        <meta name="description" content="<?php echo $sitetitle ?>">
        <meta name="author" content="<?php echo $sitetitle ?>">
        <title><?php echo $sitetitle ?></title>
		
		<!-- Favicon Icon -->
        <link rel="icon" type="image/png" href="images/logo.png">
		
        <!-- Stylesheets -->
        <?php include 'plugincss.php' ?>
        <link rel="stylesheet" href="https://cdn.datatables.net/1.10.24/css/jquery.dataTables.min.css">


	</head>

<body class="d-flex flex-column h-100">

	<!-- Add Organisation Model End-->
	<!-- Header Start-->
	<header class="header">
        <?php include 'topmenu.php' ?>
	</header>
	<!-- Header End-->
	<!-- Left Sidebar Start -->
	<nav class="vertical_nav">
        <?php include 'sidemenu.php' ?>
	</nav>



	<!-- Left Sidebar End -->
	<!-- Body Start -->
	<div class="wrapper wrapper-body">
		<div class="dashboard-body">
			<div class="container-fluid">
				<div class="row">
					<div class="col-md-12">
						<div class="d-main-title">
							<h3>New Stock Entry [<?php echo $company ;?>]</h3>
						</div>
					</div>
                        
                        <div class="event-list">
                            <div class="tab-content">
                                <div class="tab-pane fade show active" id="orders-tab" role="tabpanel">
                                    <div class="table-card mt-4">
                                        <div class="main-table">
                                            <div class="table-responsive">
                                                <form id="stockForm"  class="form" method="post" action="">
                                                    <div class="modal-body">
                                                        <div class="model-content main-form">
                                                            <div class="row">
                                                                <div class="model-content main-form">
                                                                    <div class="row">
                                                                        <div class="col-lg-4 col-md-12">
                                                                            <div class="form-group mt-4">
                                                                                <label class="form-label">Record Date</label>
                                                                                <input class="form-control h_40" id="tdate" name="tdate" type="date">
                                                                            </div>
                                                                        </div>
                                                                        <div class="col-lg-12 col-md-12">
                                                                            <br>
                                                                            <label for="product" class="form-label"><strong>Product</strong></label>
                                                                            <select name="product" id="product" class="form-control" required>
                                                                                <option value=""></option>
                                                                                <?php
                                                                                $sql = "SELECT ProdCode, ProdName FROM $tblproducts WHERE Company LIKE '$company' ORDER BY ProdName ASC";
                                                                                $result = mysqli_query($conn, $sql);
                                                                        
                                                                                while($row = mysqli_fetch_assoc($result)){
                                                                                    $code = $row['ProdCode'];
                                                                                    $name = strtoupper($row['ProdName']);
                                                                                ?>
                                                                                    <option value="<?php echo $code; ?>">
                                                                                        <?php echo $code . " - " . $name; ?>
                                                                                    </option>
                                                                                <?php
                                                                                }
                                                                                ?>
                                                                        
                                                                            </select>
                                                                        </div>
                                                                        
                                                                        <div class="col-lg-4 col-md-12">
                                                                            <div class="form-group mt-4">
                                                                                <label class="form-label">Barcode</label>
                                                                                <input class="form-control h_40" id="bcode" name="bcode" type="text" readonly>
                                                                            </div>
                                                                        </div>
                                                                        <div class="col-lg-8 col-md-12">
                                                                            <div class="form-group mt-4">
                                                                                <label class="form-label">Product Name</label>
                                                                                <input class="form-control h_40" id="pname" name="pname" type="text" readonly>
                                                                            </div>
                                                                        </div>
                                                                        <div class="col-lg-6 col-md-12">
                                                                            <div class="form-group mt-4">
                                                                                <label class="form-label">Product Size</label>
                                                                                <input class="form-control h_40" id="psize" name="psize" type="text" readonly>
                                                                            </div>
                                                                        </div>
                                                                        <div class="col-lg-6 col-md-12">
                                                                            <div class="form-group mt-4">
                                                                                <label class="form-label">Product Type</label>
                                                                                <input class="form-control h_40" id="ptype" name="ptype" type="text" readonly>
                                                                            </div>
                                                                        </div>
                                                                        <div class="col-lg-12 col-md-12">
                                                                            <div class="form-group mt-4">
                                                                                <label class="form-label">Product Description</label>
                                                                                <input class="form-control h_40" id="desc" name="desc" type="text" readonly>
                                                                            </div>
                                                                        </div>
                                                                        <div class="col-lg-12 col-md-12">
                                                                            <div class="form-group mt-4">
                                                                                <label class="form-label">Location</label>
                                                                                <input class="form-control h_40" id="loc" name="loc" type="text" readonly>
                                                                            </div>
                                                                        </div>
                                                                        <div class="col-lg-12 col-md-12">
                                                                            <div class="form-group mt-4">
                                                                                <label class="form-label">Company</label>
                                                                                <input class="form-control h_40" id="comp" name="comp" type="text" readonly>
                                                                            </div>
                                                                        </div>
                                                                        <div class="col-lg-12 col-md-12">
                                                                            <div class="form-group mt-4">
                                                                                <label class="form-label">Current Stock</label>
                                                                                <input class="form-control h_40" id="cstock" name="cstock" type="text" readonly>
                                                                            </div>
                                                                        </div>
                                                                        <div class="col-lg-2 col-md-12">
                                                                            <div class="form-group mt-4">
                                                                                <label class="form-label">Quantity</label>
                                                                                <select class="form-control h_40" id="qty" name="qty" required>
                                                                                    <?php
                                                                                    for($i=1; $i<=20; $i++){
                                                                                        $selected = ($i == 1) ? "selected" : "";
                                                                                        echo "<option value='$i' $selected>$i</option>";
                                                                                    }
                                                                                    ?>
                                                                                </select>
                                                                            </div>
                                                                        </div>
                                                                        <div class="col-lg-2 col-md-12">
                                                                            <div class="form-group mt-4">
                                                                                <label class="form-label">&nbsp;</label>
                                                                                <button type="button" class="btn btn-success w-100" id="btnStockIn" name="btnStockIn">
                                                                                    <i class="fa-solid fa-arrow-down"></i> Stock In
                                                                                </button>
                                                                            </div>
                                                                        </div>
                                                                        <div class="col-lg-2 col-md-12">
                                                                            <div class="form-group mt-4">
                                                                                <label class="form-label">&nbsp;</label>
                                                                                <button type="button" class="btn btn-warning w-100" id="btnStockOut" name="btnStockOut">
                                                                                    <i class="fa-solid fa-arrow-up"></i> Stock Out
                                                                                </button>
                                                                            </div>
                                                                        </div>
                                                                        <div class="col-lg-2 col-md-12">
                                                                            <div class="form-group mt-4">
                                                                                <label class="form-label">&nbsp;</label>
                                                                                <button type="button" class="btn btn-danger w-100" id="btnStockReturn" name="btnStockReturn">
                                                                                    <i class="fa-solid fa-arrow-right-from-bracket"></i> Stock Return
                                                                                </button>
                                                                            </div>
                                                                        </div>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </form>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                </div>
            </div>
        </div>
    </div>
	
    <div class="copyright-footer">
        <?php echo $copyrights ?>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <!-- Include DataTable JS -->
    <script src="https://cdn.datatables.net/1.10.24/js/jquery.dataTables.min.js"></script>
    <script>
        // Initialize DataTable
        $(document).ready(function() {
          $('#dataTable').DataTable({
                  "ordering": false,
                  "searching": false,
                  "lengthChange": false,
                  "pageLength": 5
              });
        });
    </script>

	<!-- Body End -->
    <?php include 'pluginjs.php' ?>
    
    <script>
        document.getElementById("product").addEventListener("change", function() {
            
            let prodCode = this.value;
        
            if(prodCode !== ""){
                fetch("get-product-details.php?code=" + prodCode)
                .then(response => response.json())
                .then(data => {
                    //document.getElementById("$trandate").value = data.Stock;
                    document.getElementById("bcode").value = data.Barcode;
                    document.getElementById("pname").value = data.ProdName;
                    document.getElementById("psize").value = data.ProdSize;
                    document.getElementById("ptype").value = data.ProdType;
                    document.getElementById("desc").value = data.Description;
                    document.getElementById("loc").value = data.Location;
                    document.getElementById("comp").value = data.Company;
                    document.getElementById("cstock").value = data.Stock;
        
                });
            }
        });
    </script>
    
    <script>
        function submitStock(actionType) {
            let form = document.getElementById('stockForm');
            let formData = new FormData(form);
            formData.append('action', actionType);
        
            fetch('save-stock-new-entry.php', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                if(data.status === 'success'){
                    alert(data.message);
                    // update current stock
                    document.getElementById('cstock').value = data.newStock;
                } else {
                    alert("Error: " + data.message);
                }
            })
            .catch(error => console.error('Error:', error));
        }
        
        document.getElementById('btnStockIn').addEventListener('click', function(){ submitStock('IN'); });
        document.getElementById('btnStockOut').addEventListener('click', function(){ submitStock('OUT'); });
        document.getElementById('btnStockReturn').addEventListener('click', function(){ submitStock('RETURN'); });
    </script>

</body>
</html>
