
// Add custom fields (File Upload, Class Type, School Name, and Location) to EDD checkout form
function custom_edd_checkout_fields() {
    ?>
    <p>
        <label for="class_type">Class Type:</label><br>
        <input type="radio" name="class_type" value="JSS 1" required> JSS 1
        <input type="radio" name="class_type" value="JSS 2" required> JSS 2
        <input type="radio" name="class_type" value="JSS 3" required> JSS 3
        <input type="radio" name="class_type" value="SSS 1" required> SSS 1
        <input type="radio" name="class_type" value="SSS 2" required> SSS 2
        <input type="radio" name="class_type" value="SSS 3" required> SSS 3
    </p>
    
    <p>
        <label for="school_name">School Name:</label>
        <input type="text" id="school_name" name="school_name" required>
    </p>

    <p>
        <label for="location">Location (State in Nigeria):</label>
        <select id="location" name="location" required>
            <option value="">Select State</option>
            <option value="Abia">Abia</option>
            <option value="Adamawa">Adamawa</option>
            <option value="Akwa Ibom">Akwa Ibom</option>
            <option value="Anambra">Anambra</option>
            <option value="Bauchi">Bauchi</option>
            <option value="Bayelsa">Bayelsa</option>
            <option value="Benue">Benue</option>
            <option value="Borno">Borno</option>
            <option value="Cross River">Cross River</option>
            <option value="Delta">Delta</option>
            <option value="Ebonyi">Ebonyi</option>
            <option value="Edo">Edo</option>
            <option value="Ekiti">Ekiti</option>
            <option value="Enugu">Enugu</option>
            <option value="Gombe">Gombe</option>
            <option value="Imo">Imo</option>
            <option value="Jigawa">Jigawa</option>
            <option value="Kaduna">Kaduna</option>
            <option value="Kano">Kano</option>
            <option value="Katsina">Katsina</option>
            <option value="Kebbi">Kebbi</option>
            <option value="Kogi">Kogi</option>
            <option value="Kwara">Kwara</option>
            <option value="Lagos">Lagos</option>
            <option value="Nasarawa">Nasarawa</option>
            <option value="Niger">Niger</option>
            <option value="Ogun">Ogun</option>
            <option value="Ondo">Ondo</option>
            <option value="Osun">Osun</option>
            <option value="Oyo">Oyo</option>
            <option value="Plateau">Plateau</option>
            <option value="Rivers">Rivers</option>
            <option value="Sokoto">Sokoto</option>
            <option value="Taraba">Taraba</option>
            <option value="Yobe">Yobe</option>
            <option value="Zamfara">Zamfara</option>
            <option value="FCT Abuja">FCT Abuja</option>
        </select>
    </p>

    <p>
        <label for="essay_upload">Upload Your Essay (PDF Only):</label>
        <input type="file" id="essay_upload" name="essay_upload" accept=".pdf" required>
    </p>
    <?php
}
add_action('edd_purchase_form_after_user_info', 'custom_edd_checkout_fields');

// Process & Save Custom Fields
function custom_edd_save_checkout_fields($payment_meta) {
    if (!empty($_POST['class_type'])) {
        $payment_meta['class_type'] = sanitize_text_field($_POST['class_type']);
    }
    if (!empty($_POST['school_name'])) {
        $payment_meta['school_name'] = sanitize_text_field($_POST['school_name']);
    }
    if (!empty($_POST['location'])) {
        $payment_meta['location'] = sanitize_text_field($_POST['location']);
    }
    
    // File Upload Processing
    if (!empty($_FILES['essay_upload']['name'])) {
        $upload = wp_upload_bits($_FILES['essay_upload']['name'], null, file_get_contents($_FILES['essay_upload']['tmp_name']));
        
        if (!$upload['error']) {
            $payment_meta['essay_upload'] = $upload['url'];
        } else {
            edd_set_error('file_upload_error', 'Error uploading file. Please try again.');
        }
    }
    return $payment_meta;
}
add_filter('edd_payment_meta', 'custom_edd_save_checkout_fields');

// Display Custom Fields in Admin Order Details
function custom_edd_display_custom_fields($payment_meta, $user_info) {
    if (!empty($payment_meta['class_type'])) {
        echo '<p><strong>Class Type:</strong> ' . esc_html($payment_meta['class_type']) . '</p>';
    }
    if (!empty($payment_meta['school_name'])) {
        echo '<p><strong>School Name:</strong> ' . esc_html($payment_meta['school_name']) . '</p>';
    }
    if (!empty($payment_meta['location'])) {
        echo '<p><strong>Location:</strong> ' . esc_html($payment_meta['location']) . '</p>';
    }
    if (!empty($payment_meta['essay_upload'])) {
        echo '<p><strong>Uploaded Essay:</strong> <a href="' . esc_url($payment_meta['essay_upload']) . '" target="_blank">Download</a></p>';
    }
}
add_action('edd_payment_view_details', 'custom_edd_display_custom_fields', 10, 2);

// Include Custom Fields in Admin Email
function custom_edd_admin_email_tags($email_tags) {
    $email_tags[] = array(
        'tag'         => 'class_type',
        'description' => 'Class Type selected',
        'function'    => function($payment_id) {
            $payment_meta = edd_get_payment_meta($payment_id);
            return !empty($payment_meta['class_type']) ? $payment_meta['class_type'] : 'Not provided';
        }
    );

    $email_tags[] = array(
        'tag'         => 'school_name',
        'description' => 'School Name entered',
        'function'    => function($payment_id) {
            $payment_meta = edd_get_payment_meta($payment_id);
            return !empty($payment_meta['school_name']) ? $payment_meta['school_name'] : 'Not provided';
        }
    );

    $email_tags[] = array(
        'tag'         => 'location',
        'description' => 'Selected location (state)',
        'function'    => function($payment_id) {
            $payment_meta = edd_get_payment_meta($payment_id);
            return !empty($payment_meta['location']) ? $payment_meta['location'] : 'Not provided';
        }
    );

    $email_tags[] = array(
        'tag'         => 'essay_upload',
        'description' => 'Uploaded essay file URL',
        'function'    => function($payment_id) {
            $payment_meta = edd_get_payment_meta($payment_id);
            return !empty($payment_meta['essay_upload']) ? $payment_meta['essay_upload'] : 'No file uploaded';
        }
    );

    return $email_tags;
}
add_filter('edd_email_tags', 'custom_edd_admin_email_tags');
