<?php 
include("../connection.php");
include("../user.php");
?>
<link rel="stylesheet" href="../css/sweet-alert.css" /> 
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $cur_date = date('Y-m-d H:i:s');
    $user = $_SESSION['user'];
    $branch = $_SESSION['division_name'];
    
    // Retrieve the form input values
    $branch_name = !empty($_POST['branch_name']) ? "'" . mysqli_real_escape_string($con, $_POST['branch_name']) . "'" : "NULL";
    $division = !empty($_POST['division']) ? "'" . mysqli_real_escape_string($con, $_POST['division']) . "'" : "NULL";
    $tower = !empty($_POST['tower']) ? "'" . mysqli_real_escape_string($con, $_POST['tower']) . "'" : "NULL";

    // $room_name = mysqli_real_escape_string($con, $_POST['room_name']);
    // $shelf_name = mysqli_real_escape_string($con, $_POST['shelf_name']);
    // $rack_name = mysqli_real_escape_string($con, $_POST['rack_name']);
    // $item_name = mysqli_real_escape_string($con, $_POST['item_name']);
    // $measurement_type = mysqli_real_escape_string($con, $_POST['measurement_type']);
    // Optional fields - ensure they are set before using
    // $measurement = mysqli_real_escape_string($con, $_POST['measurement']);
    // $kg = isset($_POST['kg']) ? mysqli_real_escape_string($con, $_POST['kg']) : NULL;
    // $meter = isset($_POST['meter']) ? mysqli_real_escape_string($con, $_POST['meter']) : NULL;
    // $unit = isset($_POST['unit']) ? mysqli_real_escape_string($con, $_POST['unit']) : NULL;
    
    $branch_addr = "Main"; // Set the branch address manually as per the example
    
    // Insert the data into the `branch_master` table
    // $create_branch = mysqli_query($con, "INSERT INTO `branch_master`(`branch_name`, `branch_address`, `room_name`, `shelf_name`, `rack_name`, `item_name`, `measurement_type`, `measurement`, `status`, `created_by`, `created_on`) 
    // VALUES ('$branch_name', '$branch_addr', '$room_name', '$shelf_name', '$rack_name', '$item_name', '$measurement_type', '$measurement', '0', '$user', '$cur_date')");
    $create_branch = mysqli_query($con, "INSERT INTO `branch_master`(`branch_name`, `division`, `tower`, `status`, `created_by`, `created_on`) 
    VALUES ('$branch_name', '$division', '$tower', '0', '$user', '$cur_date')");
    
    
    if ($create_branch) { ?>
        <script>
        setTimeout(function() {
            swal({
                title: "Success",
                text: "Branch and related info inserted successfully!",
                type: "success"
            }, function() {
                window.location = "branch_master_view.php"; // Redirect after success
            });
        }, 100);
        </script>;
    <?php } 
    else { ?>
        <script>
        setTimeout(function() {
            swal({
                title: "Error",
                text: "Failed to insert branch data!",
                type: "error"
            }, function() {
                window.location = "branch_master_view.php"; // Redirect after error
            });
        }, 100);
        </script>;
    <?php }
 }
?>
<script src="../js/sweet-alert.js"></script>
