<?php
session_start();

// Connect to the database
// From application/dripend/systemSetup/facility/ to application/dripend/includes/db.php
require_once __DIR__ . '/../../includes/db.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // 1. Clean and Collect Text Inputs
    $hospital_name = mysqli_real_escape_string($conn, $_POST['hospital_name']);
    $tax_id = mysqli_real_escape_string($conn, $_POST['tax_id'] ?? '');
    $primary_phone = mysqli_real_escape_string($conn, $_POST['primary_phone']);
    $secondary_phone = mysqli_real_escape_string($conn, $_POST['secondary_phone'] ?? '');
    $primary_email = mysqli_real_escape_string($conn, $_POST['primary_email']);
    $secondary_email = mysqli_real_escape_string($conn, $_POST['secondary_email'] ?? '');
    $address = mysqli_real_escape_string($conn, $_POST['address'] ?? '');
    $postal_address = mysqli_real_escape_string($conn, $_POST['postal_address'] ?? '');
    $document_alignment = mysqli_real_escape_string($conn, $_POST['document_alignment'] ?? 'center');
    $primary_hex = mysqli_real_escape_string($conn, $_POST['primary_hex'] ?? '#1E3A8A');
    $accent_hex = mysqli_real_escape_string($conn, $_POST['accent_hex'] ?? '#3B82F6');
    $id_format = mysqli_real_escape_string($conn, $_POST['id_format'] ?? 'FMT_B');
    $id_padding = (int)($_POST['id_padding'] ?? 5);
    $opd_prefix = mysqli_real_escape_string($conn, strtoupper($_POST['opd_prefix'] ?? 'RMX'));

    // Checkbox Toggles
    $enable_dual_logo = isset($_POST['enable_dual_logo']) ? 1 : 0;

    // 2. Handle File Uploads
    // From application/dripend/systemSetup/facility/ up 3 levels to application/ then down to rimaxUploads/    
$upload_dir = realpath(__DIR__ . '/../../..') . '/rimaxUploads/';

    // Auto-create the directory if it doesn't exist on the server yet
    if (!is_dir($upload_dir)) {
        mkdir($upload_dir, 0755, true);
    }

    $primary_logo_query = "";
    if (isset($_FILES['primary_logo']) && $_FILES['primary_logo']['error'] === UPLOAD_ERR_OK) {
        $ext = pathinfo($_FILES['primary_logo']['name'], PATHINFO_EXTENSION);
        $new_name = 'primary_logo_' . time() . '.' . $ext;
        $destination = $upload_dir . $new_name;

        if (move_uploaded_file($_FILES['primary_logo']['tmp_name'], $destination)) {
            // Save the path so the UI can easily display it later (relative to application folder)
            $db_path = 'rimaxUploads/' . $new_name;
            $primary_logo_query = ", primary_logo_path = '$db_path'";
        }
    }

    $secondary_logo_query = "";
    if (isset($_FILES['secondary_logo']) && $_FILES['secondary_logo']['error'] === UPLOAD_ERR_OK) {
        $ext = pathinfo($_FILES['secondary_logo']['name'], PATHINFO_EXTENSION);
        $new_name = 'secondary_logo_' . time() . '.' . $ext;
        $destination = $upload_dir . $new_name;

        if (move_uploaded_file($_FILES['secondary_logo']['tmp_name'], $destination)) {
            $db_path = 'rimaxUploads/' . $new_name;
            $secondary_logo_query = ", secondary_logo_path = '$db_path'";
        }
    }

    // 3. Update the Database Record
    $update_sql = "UPDATE facility_profile SET
        hospital_name = '$hospital_name',
        tax_id = '$tax_id',
        primary_phone = '$primary_phone',
        secondary_phone = '$secondary_phone',
        primary_email = '$primary_email',
        secondary_email = '$secondary_email',
        address = '$address',
        postal_address = '$postal_address',
        enable_dual_logo = $enable_dual_logo,
        document_alignment = '$document_alignment',
        primary_hex = '$primary_hex',
        accent_hex = '$accent_hex',
        id_format = '$id_format',
        id_padding = $id_padding,
        opd_prefix = '$opd_prefix'
        $primary_logo_query
        $secondary_logo_query
        WHERE id = 1";

    if (mysqli_query($conn, $update_sql)) {
        // Success: Redirect back to the UI 
$redirect_url = "/rimax/application/views/system_setup/systemSetup.php?cat=facility&page=facility/profile_branding.php&status=success";
header("Location: " . $redirect_url);
exit();
    } else {
        echo "Error updating record: " . mysqli_error($conn);
    }
} else {
    echo "Direct access denied. Please submit the form.";
}
?>