

<?php $__env->startSection('title', 'Create Prescription'); ?>

<?php $__env->startSection('content'); ?>
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
    <div class="mb-6 flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4">
        <h1 class="text-3xl font-bold text-gray-800">Create Prescription</h1>
        <a href="<?php echo e(auth()->user()->isDoctor() ? route('doctor.dashboard') : (auth()->user()->isAdmin() ? route('admin.dashboard') : route('pharmacist.dashboard'))); ?>" 
           class="bg-gray-600 hover:bg-gray-700 text-white px-4 py-2 rounded">
            <i class="fas fa-arrow-left mr-2"></i>Back to Dashboard
        </a>
    </div>

    <!-- Patient Information Section -->
    <div class="bg-white rounded-lg shadow p-6 mb-6">
        <h2 class="text-xl font-bold mb-4">Patient Information</h2>
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
            <div><strong>Name:</strong> <?php echo e($appointment->patient->full_name); ?></div>
            <div><strong>Patient ID:</strong> <?php echo e($appointment->patient->patient_id); ?></div>
            <div><strong>Identity Number:</strong> <?php echo e($appointment->patient->identity_number ?? 'N/A'); ?></div>
            <div><strong>Date of Birth:</strong> <?php echo e($appointment->patient->date_of_birth?->format('m-d-Y') ?? 'N/A'); ?></div>
            <div><strong>Age:</strong> <?php echo e($appointment->patient->date_of_birth ? \Carbon\Carbon::parse($appointment->patient->date_of_birth)->age : 'N/A'); ?> years</div>
            <div><strong>Gender:</strong> <?php echo e($appointment->patient->gender ?? 'N/A'); ?></div>
            <div><strong>Payment Mode:</strong> <?php echo e($appointment->patient->payment_mode ?? 'N/A'); ?></div>
            <?php if($appointment->patient->payment_mode === 'Medical Aid Payment'): ?>
                <div><strong>Medical Aid:</strong> <?php echo e($appointment->patient->medical_aid_number ?? 'N/A'); ?></div>
                <div><strong>Membership Status:</strong> <?php echo e($appointment->patient->main_member ?? 'N/A'); ?></div>
            <?php endif; ?>
        </div>
    </div>

    <!-- Vitals Section -->
    <?php if($appointment->vitals): ?>
    <div class="bg-white rounded-lg shadow p-6 mb-6">
        <h2 class="text-xl font-bold mb-4">Patient Vitals</h2>
        <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
            <div class="p-3 bg-gray-50 rounded">
                <strong>Blood Pressure:</strong> <?php echo e($appointment->vitals->blood_pressure ?? 'N/A'); ?> mmHg
            </div>
            <div class="p-3 bg-gray-50 rounded">
                <strong>Pulse Rate:</strong> <?php echo e($appointment->vitals->pulse_rate ?? 'N/A'); ?> bpm
            </div>
            <div class="p-3 bg-gray-50 rounded">
                <strong>RBS:</strong> <?php echo e($appointment->vitals->rbs ?? 'N/A'); ?> %
            </div>
            <div class="p-3 bg-gray-50 rounded">
                <strong>Resp Pattern:</strong> <?php echo e($appointment->vitals->resp_pattern ?? 'N/A'); ?>

            </div>
            <div class="p-3 bg-gray-50 rounded">
                <strong>SpO2:</strong> <?php echo e($appointment->vitals->spo2 ?? 'N/A'); ?> %
            </div>
            <div class="p-3 bg-gray-50 rounded">
                <strong>Temperature:</strong> <?php echo e($appointment->vitals->temperature ?? 'N/A'); ?> °C
            </div>
            <div class="p-3 bg-gray-50 rounded">
                <strong>Blood Glucose:</strong> <?php echo e($appointment->vitals->blood_glucose ?? 'N/A'); ?> mmol/L
            </div>
            <div class="p-3 bg-gray-50 rounded">
                <strong>BMI:</strong> <?php echo e($appointment->vitals->bmi ?? 'N/A'); ?> kg/m²
            </div>
        </div>
    </div>
    <?php endif; ?>

    <form method="POST" action="<?php echo e(route('prescriptions.store', $appointment->id)); ?>" class="bg-white rounded-lg shadow p-6">
        <?php echo csrf_field(); ?>

        <!-- Diagnosis Section -->
        <div class="mb-6">
            <label class="block text-gray-700 text-sm font-bold mb-2">Diagnosis</label>
            <textarea name="diagnosis" rows="3" class="w-full px-3 py-2 border rounded" placeholder="Enter diagnosis..."><?php echo e(old('diagnosis')); ?></textarea>
        </div>

        <!-- Prescription Items Section -->
        <div class="mb-6">
            <h3 class="text-lg font-bold mb-4">Prescription Items</h3>
            <div id="items-container">
                <div class="item-row mb-4 p-4 border rounded bg-gray-50">
                    <div class="grid grid-cols-1 md:grid-cols-6 gap-4 mb-4">
                        <div class="md:col-span-2">
                            <label class="block text-gray-700 text-sm font-bold mb-2">Prescription Items Rx *</label>
                            <input type="text" name="items[0][medicine_name]" required class="w-full px-3 py-2 border rounded medicine-autocomplete" placeholder="Type or select medicine name" list="medicine-list-0" autocomplete="off">
                            <datalist id="medicine-list-0">
                                <?php $__currentLoopData = $medicineNames; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $medicine): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($medicine); ?>"><?php echo e($medicine); ?></option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </datalist>
                        </div>
                        <div>
                            <label class="block text-gray-700 text-sm font-bold mb-2">Rx Quantity *</label>
                            <select name="items[0][quantity]" required class="w-full px-3 py-2 border rounded">
                                <option value="">Please Select</option>
                                <?php if(isset($quantityItems) && $quantityItems->count() > 0): ?>
                                    <?php $__currentLoopData = $quantityItems; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($item->name); ?>"><?php echo e($item->name); ?></option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                <?php else: ?>
                                    
                                    <option value="5ml Teaspoon">5ml Teaspoon</option>
                                    <option value="10ml">10ml</option>
                                    <option value="15ml">15ml</option>
                                    <option value="20ml">20ml</option>
                                    <?php for($i = 1; $i <= 40; $i++): ?>
                                        <option value="<?php echo e($i); ?> <?php echo e($i == 1 ? 'Tablet' : 'Tablets'); ?>"><?php echo e($i); ?> <?php echo e($i == 1 ? 'Tablet' : 'Tablets'); ?></option>
                                    <?php endfor; ?>
                                    <option value="Apply">Apply</option>
                                    <option value="Chew">Chew</option>
                                    <option value="Dab">Dab</option>
                                    <option value="Dissolve">Dissolve</option>
                                    <option value="Drip">Drip</option>
                                    <option value="Drop">Drop</option>
                                    <option value="Flush">Flush</option>
                                    <option value="Gargle">Gargle</option>
                                    <option value="Infuse">Infuse</option>
                                    <option value="Inhale">Inhale</option>
                                    <option value="Inject">Inject</option>
                                    <option value="Insert">Insert</option>
                                    <option value="Instil">Instil</option>
                                    <option value="Massage">Massage</option>
                                    <option value="Place between gum and cheek">Place between gum and cheek</option>
                                    <option value="Place under the tongue">Place under the tongue</option>
                                    <option value="Rinse">Rinse</option>
                                    <option value="Rub">Rub</option>
                                    <option value="Soak">Soak</option>
                                    <option value="Spray">Spray</option>
                                    <option value="Spread">Spread</option>
                                    <option value="Sprinkle">Sprinkle</option>
                                    <option value="Swallow">Swallow</option>
                                    <option value="Swish and spit">Swish and spit</option>
                                <?php endif; ?>
                            </select>
                        </div>
                        <div>
                            <label class="block text-gray-700 text-sm font-bold mb-2">Rx Frequency (OD/BD/TDS) *</label>
                            <select name="items[0][frequency]" required class="w-full px-3 py-2 border rounded">
                                <option value="">Please Select</option>
                                <?php if(isset($frequencyItems) && $frequencyItems->count() > 0): ?>
                                    <?php $__currentLoopData = $frequencyItems; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($item->name); ?>"><?php echo e($item->name); ?></option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                <?php else: ?>
                                    
                                    <option value="Once a day">Once a day</option>
                                    <option value="Immediately">Immediately</option>
                                    <option value="At night">At night</option>
                                    <option value="At bedtime">At bedtime</option>
                                    <option value="Twice a day">Twice a day</option>
                                    <option value="Three times a day">Three times a day</option>
                                    <option value="Four times a day">Four times a day</option>
                                    <option value="Every four hours">Every four hours</option>
                                    <option value="Every six hours">Every six hours</option>
                                    <option value="Every eight hours">Every eight hours</option>
                                    <option value="Every twelve hours">Every twelve hours</option>
                                    <option value="Every twenty-four hours">Every twenty-four hours</option>
                                    <option value="Once a week">Once a week</option>
                                    <option value="Every week">Every week</option>
                                    <option value="Once a month">Once a month</option>
                                <?php endif; ?>
                                
                                <option value="instill 1 drop">instill 1 drop</option>
                                <option value="instill 2 drops">instill 2 drops</option>
                                <option value="instill 3 drops">instill 3 drops</option>
                                <option value="instill 5 drops">instill 5 drops</option>
                                <option value="instill 10 drops">instill 10 drops</option>
                            </select>
                        </div>
                        <div>
                            <label class="block text-gray-700 text-sm font-bold mb-2">Rx Instruction</label>
                            <select name="items[0][instructions]" class="w-full px-3 py-2 border rounded">
                                <option value="">Please Select</option>
                                <?php if(isset($instructionItems) && $instructionItems->count() > 0): ?>
                                    <?php $__currentLoopData = $instructionItems; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($item->name); ?>"><?php echo e($item->name); ?></option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                <?php else: ?>
                                    
                                    <option value="When required">When required</option>
                                    <option value="Finish the Course">Finish the Course</option>
                                    <option value="Only if necessary">Only if necessary</option>
                                    <option value="Before meals">Before meals</option>
                                    <option value="After meals">After meals</option>
                                    <option value="With food">With food</option>
                                    <option value="On alternate days">On alternate days</option>
                                    <option value="At night only">At night only</option>
                                    <option value="As directed by your healthcare provider">As directed by your healthcare provider</option>
                                    <option value="Up to the maximum prescribed dose">Up to the maximum prescribed dose</option>
                                    <option value="Gradually increase or decrease the dose as instructed">Gradually increase or decrease the dose as instructed</option>
                                <?php endif; ?>
                            </select>
                        </div>
                        <div>
                            <label class="block text-gray-700 text-sm font-bold mb-2">Rx Duration *</label>
                            <select name="items[0][duration]" required class="w-full px-3 py-2 border rounded">
                                <option value="">Please Select</option>
                                <?php if(isset($durationItems) && $durationItems->count() > 0): ?>
                                    <?php $__currentLoopData = $durationItems; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($item->name); ?>"><?php echo e($item->name); ?></option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                <?php else: ?>
                                    
                                    <?php for($i = 1; $i <= 7; $i++): ?>
                                        <option value="<?php echo e($i); ?> <?php echo e($i == 1 ? 'Day' : 'Days'); ?> Treatment"><?php echo e($i); ?> <?php echo e($i == 1 ? 'Day' : 'Days'); ?> Treatment</option>
                                    <?php endfor; ?>
                                    <?php for($i = 1; $i <= 5; $i++): ?>
                                        <option value="<?php echo e($i); ?> <?php echo e($i == 1 ? 'Week' : 'Weeks'); ?> Treatment"><?php echo e($i); ?> <?php echo e($i == 1 ? 'Week' : 'Weeks'); ?> Treatment</option>
                                    <?php endfor; ?>
                                    <?php for($i = 1; $i <= 6; $i++): ?>
                                        <option value="<?php echo e($i); ?> <?php echo e($i == 1 ? 'Month' : 'Months'); ?> Treatment"><?php echo e($i); ?> <?php echo e($i == 1 ? 'Month' : 'Months'); ?> Treatment</option>
                                    <?php endfor; ?>
                                    <option value="12 Months Treatment">12 Months Treatment</option>
                                <?php endif; ?>
                            </select>
                        </div>
                    </div>
                    <div class="mb-2">
                        <label class="block text-gray-700 text-sm font-bold mb-2">Specific Instructions</label>
                        <input type="text" name="items[0][dosage]" class="w-full px-3 py-2 border rounded" placeholder="e.g., 500mg">
                    </div>
                </div>
            </div>
            <button type="button" onclick="addItem()" class="bg-gray-600 hover:bg-gray-700 text-white px-4 py-2 rounded">
                <i class="fas fa-plus"></i> Add Item
            </button>
        </div>

        <!-- Additional Prescription Details Section -->
        <div class="mb-6">
            <label class="block text-gray-700 text-sm font-bold mb-2">Additional Prescription Details</label>
            <textarea name="additional_prescription_details" rows="3" class="w-full px-3 py-2 border rounded" placeholder="Enter any additional prescription details..."><?php echo e(old('additional_prescription_details')); ?></textarea>
        </div>

        <div class="flex justify-end">
            <button type="submit" class="bg-[#ec4899] hover:bg-[#db2777] text-white px-6 py-2 rounded">
                <i class="fas fa-save mr-2"></i>Create Prescription
            </button>
        </div>
    </form>
</div>

<script>
let itemCount = 1;

// Prescription items from database
const quantityItems = <?php echo json_encode(isset($quantityItems) ? $quantityItems->pluck('name') : [], 15, 512) ?>;
const frequencyItems = <?php echo json_encode(isset($frequencyItems) ? $frequencyItems->pluck('name') : [], 15, 512) ?>;
const instructionItems = <?php echo json_encode(isset($instructionItems) ? $instructionItems->pluck('name') : [], 15, 512) ?>;
const durationItems = <?php echo json_encode(isset($durationItems) ? $durationItems->pluck('name') : [], 15, 512) ?>;
const medicineNames = <?php echo json_encode(isset($medicineNames) ? $medicineNames : [], 15, 512) ?>;

// Fallback options if database is empty
const fallbackQuantity = ['5ml Teaspoon', '10ml', '15ml', '20ml', ...Array.from({length: 40}, (_, i) => `${i+1} ${i === 0 ? 'Tablet' : 'Tablets'}`), 'Apply', 'Chew', 'Dab', 'Dissolve', 'Drip', 'Drop', 'Flush', 'Gargle', 'Infuse', 'Inhale', 'Inject', 'Insert', 'Instil', 'Massage', 'Place between gum and cheek', 'Place under the tongue', 'Rinse', 'Rub', 'Soak', 'Spray', 'Spread', 'Sprinkle', 'Swallow', 'Swish and spit'];
const fallbackFrequency = ['Once a day', 'Immediately', 'At night', 'At bedtime', 'Twice a day', 'Three times a day', 'Four times a day', 'Every four hours', 'Every six hours', 'Every eight hours', 'Every twelve hours', 'Every twenty-four hours', 'Once a week', 'Every week', 'Once a month'];
const fallbackInstruction = ['When required', 'Finish the Course', 'Only if necessary', 'Before meals', 'After meals', 'With food', 'On alternate days', 'At night only', 'As directed by your healthcare provider', 'Up to the maximum prescribed dose', 'Gradually increase or decrease the dose as instructed'];
const fallbackDuration = [...Array.from({length: 7}, (_, i) => `${i+1} ${i === 0 ? 'Day' : 'Days'} Treatment`), ...Array.from({length: 5}, (_, i) => `${i+1} ${i === 0 ? 'Week' : 'Weeks'} Treatment`), ...Array.from({length: 6}, (_, i) => `${i+1} ${i === 0 ? 'Month' : 'Months'} Treatment`), '12 Months Treatment'];

// New instill frequency options
const instillOptions = ['instill 1 drop', 'instill 2 drops', 'instill 3 drops', 'instill 5 drops', 'instill 10 drops'];

function generateQuantityOptions() {
    const items = quantityItems.length > 0 ? quantityItems : fallbackQuantity;
    return items.map(item => `<option value="${item}">${item}</option>`).join('');
}

function generateFrequencyOptions() {
    const items = frequencyItems.length > 0 ? frequencyItems : fallbackFrequency;
    const allItems = [...items, ...instillOptions];
    return allItems.map(item => `<option value="${item}">${item}</option>`).join('');
}

function generateInstructionOptions() {
    const items = instructionItems.length > 0 ? instructionItems : fallbackInstruction;
    return items.map(item => `<option value="${item}">${item}</option>`).join('');
}

function generateDurationOptions() {
    const items = durationItems.length > 0 ? durationItems : fallbackDuration;
    return items.map(item => `<option value="${item}">${item}</option>`).join('');
}

function addItem() {
    const container = document.getElementById('items-container');
    const newItem = document.createElement('div');
    newItem.className = 'item-row mb-4 p-4 border rounded bg-gray-50';
    newItem.innerHTML = `
        <div class="grid grid-cols-1 md:grid-cols-6 gap-4 mb-4">
            <div class="md:col-span-2">
                <label class="block text-gray-700 text-sm font-bold mb-2">Prescription Items Rx *</label>
                <input type="text" name="items[${itemCount}][medicine_name]" required class="w-full px-3 py-2 border rounded medicine-autocomplete" placeholder="Type or select medicine name" list="medicine-list-${itemCount}" autocomplete="off">
                <datalist id="medicine-list-${itemCount}">
                    ${medicineNames.map(medicine => `<option value="${medicine}">${medicine}</option>`).join('')}
                </datalist>
            </div>
            <div>
                <label class="block text-gray-700 text-sm font-bold mb-2">Rx Quantity *</label>
                <select name="items[${itemCount}][quantity]" required class="w-full px-3 py-2 border rounded">
                    <option value="">Please Select</option>
                    ${generateQuantityOptions()}
                </select>
            </div>
            <div>
                <label class="block text-gray-700 text-sm font-bold mb-2">Rx Frequency (OD/BD/TDS) *</label>
                <select name="items[${itemCount}][frequency]" required class="w-full px-3 py-2 border rounded">
                    <option value="">Please Select</option>
                    ${generateFrequencyOptions()}
                </select>
            </div>
            <div>
                <label class="block text-gray-700 text-sm font-bold mb-2">Rx Instruction</label>
                <select name="items[${itemCount}][instructions]" class="w-full px-3 py-2 border rounded">
                    <option value="">Please Select</option>
                    ${generateInstructionOptions()}
                </select>
            </div>
            <div>
                <label class="block text-gray-700 text-sm font-bold mb-2">Rx Duration *</label>
                <select name="items[${itemCount}][duration]" required class="w-full px-3 py-2 border rounded">
                    <option value="">Please Select</option>
                    ${generateDurationOptions()}
                </select>
            </div>
        </div>
        <div class="mb-2">
            <label class="block text-gray-700 text-sm font-bold mb-2">Specific Instructions</label>
            <input type="text" name="items[${itemCount}][dosage]" class="w-full px-3 py-2 border rounded" placeholder="e.g., 500mg">
        </div>
        <button type="button" onclick="removeItem(this)" class="bg-red-600 hover:bg-red-700 text-white px-4 py-2 rounded">
            <i class="fas fa-trash"></i> Remove
        </button>
    `;
    container.appendChild(newItem);
    itemCount++;
}


function removeItem(button) {
    button.closest('.item-row').remove();
}
</script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\Users\BrainTech\Documents\BEAUTEZ SYSTEM\BEAUTEZ SYSTEM\resources\views/prescriptions/create.blade.php ENDPATH**/ ?>