<?php
// Calculator functions
function calculateBMR($weight, $height, $age, $gender, $unit) {
    $weightInKg = $unit === 'imperial' ? $weight * 0.453592 : $weight;
    $heightInCm = $unit === 'imperial' ? $height * 2.54 : $height;
    
    if ($gender === 'male') {
        return 88.362 + (13.397 * $weightInKg) + (4.799 * $heightInCm) - (5.677 * $age);
    }
    return 447.593 + (9.247 * $weightInKg) + (3.098 * $heightInCm) - (4.330 * $age);
}

function calculateTDEE($bmr, $activityLevel) {
    $multipliers = [
        'sedentary' => 1.2,
        'light' => 1.375,
        'moderate' => 1.55,
        'active' => 1.725,
        'very-active' => 1.9
    ];
    return $bmr * $multipliers[$activityLevel];
}

function calculateTargetCalories($tdee, $goal) {
    $adjustments = [
        'lose' => -500,
        'maintain' => 0,
        'gain' => 500
    ];
    return $tdee + $adjustments[$goal];
}

function calculateMacros($targetCalories) {
    return [
        'protein' => round(($targetCalories * 0.3) / 4),
        'carbs' => round(($targetCalories * 0.4) / 4),
        'fats' => round(($targetCalories * 0.3) / 9)
    ];
}

// Process form submission
$results = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $weight = floatval($_POST['weight']);
    $height = floatval($_POST['height']);
    $age = intval($_POST['age']);
    $gender = $_POST['gender'];
    $activityLevel = $_POST['activityLevel'];
    $goal = $_POST['goal'];
    $unit = $_POST['unit'];

    $bmr = calculateBMR($weight, $height, $age, $gender, $unit);
    $tdee = calculateTDEE($bmr, $activityLevel);
    $targetCalories = calculateTargetCalories($tdee, $goal);
    $macros = calculateMacros($targetCalories);

    $results = [
        'bmr' => round($bmr),
        'tdee' => round($tdee),
        'targetCalories' => round($targetCalories),
        'macros' => $macros
    ];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Advanced Calorie & Macro Calculator</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://unpkg.com/lucide-static/font/lucide.css">
</head>
<body class="min-h-screen bg-gradient-to-br from-blue-50 to-indigo-50 py-12 px-4 sm:px-6 lg:px-8">
    <div class="max-w-3xl mx-auto">
        <div class="text-center mb-8">
            <i class="lucide-calculator mx-auto h-12 w-12 text-indigo-600"></i>
            <h1 class="mt-4 text-3xl font-bold text-gray-900">Advanced Calorie & Macro Calculator</h1>
            <p class="mt-2 text-gray-600">Calculate your daily calorie needs with personalized adjustments</p>
        </div>

        <div class="bg-white rounded-lg shadow-xl p-6 md:p-8">
            <form method="POST" class="space-y-6">
                <div class="flex justify-center mb-6">
                    <div class="inline-flex rounded-md shadow-sm">
                        <button type="button" onclick="setUnit('metric')" class="px-4 py-2 text-sm font-medium rounded-l-md border" id="metricBtn">Metric</button>
                        <button type="button" onclick="setUnit('imperial')" class="px-4 py-2 text-sm font-medium rounded-r-md border" id="imperialBtn">Imperial</button>
                    </div>
                    <input type="hidden" name="unit" id="unitInput" value="imperial">
                </div>

                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <label class="block text-sm font-medium text-gray-700">Weight (<span id="weightUnit">lbs</span>)</label>
                        <input type="number" name="weight" required class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500">
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700">Height (<span id="heightUnit">inches</span>)</label>
                        <input type="number" name="height" required class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500">
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700">Age</label>
                        <input type="number" name="age" required class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500">
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700">Gender</label>
                        <select name="gender" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500">
                            <option value="male">Male</option>
                            <option value="female">Female</option>
                        </select>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700">Activity Level</label>
                        <select name="activityLevel" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500">
                            <option value="sedentary">Sedentary</option>
                            <option value="light">Lightly Active</option>
                            <option value="moderate">Moderately Active</option>
                            <option value="active">Very Active</option>
                            <option value="very-active">Extremely Active</option>
                        </select>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700">Goal</label>
                        <select name="goal" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500">
                            <option value="lose">Lose Weight</option>
                            <option value="maintain">Maintain Weight</option>
                            <option value="gain">Gain Weight</option>
                        </select>
                    </div>
                </div>

                <div class="flex justify-center">
                    <button type="submit" class="inline-flex items-center px-6 py-3 border border-transparent text-base font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">
                        Calculate
                    </button>
                </div>
            </form>

            <?php if ($results): ?>
            <div class="mt-8 space-y-6">
                <h2 class="text-xl font-semibold text-gray-900">Your Results</h2>
                <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                    <div class="bg-indigo-50 p-4 rounded-lg">
                        <h3 class="text-sm font-medium text-indigo-800">BMR</h3>
                        <p class="mt-2 text-2xl font-semibold text-indigo-900"><?php echo $results['bmr']; ?> kcal</p>
                    </div>
                    <div class="bg-indigo-50 p-4 rounded-lg">
                        <h3 class="text-sm font-medium text-indigo-800">TDEE</h3>
                        <p class="mt-2 text-2xl font-semibold text-indigo-900"><?php echo $results['tdee']; ?> kcal</p>
                    </div>
                    <div class="bg-indigo-50 p-4 rounded-lg">
                        <h3 class="text-sm font-medium text-indigo-800">Target Calories</h3>
                        <p class="mt-2 text-2xl font-semibold text-indigo-900"><?php echo $results['targetCalories']; ?> kcal</p>
                    </div>
                </div>

                <div class="bg-white rounded-lg border border-gray-200 p-6">
                    <h3 class="text-lg font-medium text-gray-900 mb-4">Recommended Macro Split</h3>
                    <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                        <div>
                            <h4 class="text-sm font-medium text-gray-500">Protein</h4>
                            <p class="mt-1 text-xl font-semibold text-gray-900"><?php echo $results['macros']['protein']; ?>g</p>
                        </div>
                        <div>
                            <h4 class="text-sm font-medium text-gray-500">Carbs</h4>
                            <p class="mt-1 text-xl font-semibold text-gray-900"><?php echo $results['macros']['carbs']; ?>g</p>
                        </div>
                        <div>
                            <h4 class="text-sm font-medium text-gray-500">Fats</h4>
                            <p class="mt-1 text-xl font-semibold text-gray-900"><?php echo $results['macros']['fats']; ?>g</p>
                        </div>
                    </div>
                </div>

                <div class="space-y-4">
                    <a href="https://mealplanpdf.net/cv4/2089b0ec565f96167f09a19/
" target="_blank" rel="noopener noreferrer" class="block bg-gradient-to-r from-green-500 to-emerald-600 rounded-lg p-6 shadow-lg hover:shadow-xl transition-shadow duration-300">
                        <div class="flex items-center justify-between">
                            <div>
                                <h3 class="text-xl font-bold text-white">Craving Convenience?</h3>
                                <p class="text-green-50 mt-2">Try Fresh Ingredients Paired with Simple, Gourmet Recipes</p>
                            </div>
                            <i class="lucide-chef-hat h-12 w-12 text-white"></i>
                        </div>
                    </a>

                    <a href="https://mealplanpdf.net/cv4/2089b0ec565f96167f09a19/
" target="_blank" rel="noopener noreferrer" class="block bg-gradient-to-r from-purple-500 to-indigo-600 rounded-lg p-6 shadow-lg hover:shadow-xl transition-shadow duration-300">
                        <div class="flex items-center justify-between">
                            <div>
                                <h3 class="text-xl font-bold text-white">Revolutionize Your Health</h3>
                                <p class="text-purple-50 mt-2">The Game-Changer Fitness Experts Swear By</p>
                            </div>
                            <i class="lucide-dumbbell h-12 w-12 text-white"></i>
                        </div>
                    </a>
                </div>
            </div>
            <?php endif; ?>
        </div>
    </div>

    <script>
        function setUnit(unit) {
            document.getElementById('unitInput').value = unit;
            document.getElementById('metricBtn').className = `px-4 py-2 text-sm font-medium rounded-l-md border ${unit === 'metric' ? 'bg-indigo-600 text-white border-indigo-600' : 'bg-white text-gray-700 border-gray-300 hover:bg-gray-50'}`;
            document.getElementById('imperialBtn').className = `px-4 py-2 text-sm font-medium rounded-r-md border ${unit === 'imperial' ? 'bg-indigo-600 text-white border-indigo-600' : 'bg-white text-gray-700 border-gray-300 hover:bg-gray-50'}`;
            document.getElementById('weightUnit').textContent = unit === 'metric' ? 'kg' : 'lbs';
            document.getElementById('heightUnit').textContent = unit === 'metric' ? 'cm' : 'inches';
        }

        // Set default unit to imperial
        setUnit('imperial');
    </script>
    <!--Start of Tawk.to Script-->
<script type="text/javascript">
var Tawk_API=Tawk_API||{}, Tawk_LoadStart=new Date();
(function(){
var s1=document.createElement("script"),s0=document.getElementsByTagName("script")[0];
s1.async=true;
s1.src='https://embed.tawk.to/669adf6932dca6db2cb250e0/1i36fhd5m';
s1.charset='UTF-8';
s1.setAttribute('crossorigin','*');
s0.parentNode.insertBefore(s1,s0);
})();
</script>
<!--End of Tawk.to Script-->
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-FF95D5EQTC"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-FF95D5EQTC');
</script>
<!-- Meta Pixel Code -->
<script>
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s)}(window, document,'script',
'https://connect.facebook.net/en_US/fbevents.js');
fbq('init', '8932466056863042');
fbq('track', 'PageView');
</script>
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=8932466056863042&ev=PageView&noscript=1"
/></noscript>
<!-- End Meta Pixel Code -->
</body>
</html>