<?php
session_start();
require_once "config.php";

// Function to sanitize input
function sanitizeInput($input) {
    return htmlspecialchars(strip_tags(trim($input)));
}

// Function to handle file upload
function handleFileUpload($file, $targetDir = "uploads/") {
    $targetFile = $targetDir . basename($file["name"]);
    if (move_uploaded_file($file["tmp_name"], $targetFile)) {
        return $targetFile;
    } else {
        return "";
    }
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    
    $branch_name = sanitizeInput($_POST['branch_name']);
    $branch_address = sanitizeInput($_POST['branch_address']);
   
    if (isset($_GET['action'])) {
        $action = $_GET['action'];

        if ($action == 'add') {
            
            $sql = "INSERT INTO branches (branch_name, branch_address, created_at) VALUES (?, ?, ?)";
            
            if ($stmt = mysqli_prepare($conn, $sql)) {
                mysqli_stmt_bind_param($stmt, "sss", $branch_name, $branch_address, $date);
                
                if (mysqli_stmt_execute($stmt)) {
                    mysqli_stmt_close($stmt);
                    mysqli_close($conn);
                    $_SESSION['success_message'] = "Employee added successfully.";
                    header("location: branch.php");
                    exit();
                } else {
                    echo "Error: " . mysqli_error($conn);
                }
            } else {
                echo "Error: " . mysqli_error($conn);
            }
        } 
        
        if ($action == 'edit') {
            $branch_id = sanitizeInput($_POST['branch_id']);
            $sql = "UPDATE branches SET branch_name=?, branch_address=? WHERE branch_id=?";
            
            if ($stmt = mysqli_prepare($conn, $sql)) {
                mysqli_stmt_bind_param($stmt, "ssi", $branch_name, $branch_address, $branch_id);
                
                if (mysqli_stmt_execute($stmt)) {
                    mysqli_stmt_close($stmt);
                    mysqli_close($conn);
                    $_SESSION['success_message'] = "Branch updated successfully.";
                    header("location: branch.php");
                    exit();
                } else {
                    echo "Error: " . mysqli_error($conn);
                }
            } else {
                echo "Error: " . mysqli_error($conn);
            }
        }
        
        if ($action == 'delete') {
            $branch_id = sanitizeInput($_POST['delete_id']); 
            $sql = "DELETE FROM branches WHERE branch_id=?";
            
            if ($stmt = mysqli_prepare($conn, $sql)) {
                mysqli_stmt_bind_param($stmt, "i", $branch_id);
                if (mysqli_stmt_execute($stmt)) {
                    mysqli_stmt_close($stmt);
                    mysqli_close($conn);
                    header("Location: branch.php");
                    exit();
                } else {
                    echo "Error: " . mysqli_error($conn);
                }
            }
        }
    }
    else{
        
    }
}
?>