<?php
/**
 * Driver profile fields in WP Dashboard (admin)
 * @since 1.0.0
 */

// Enqueue media uploader and custom JS for driver profile fields
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

add_action('admin_enqueue_scripts', function($hook) {
    if ($hook === 'profile.php' || $hook === 'user-edit.php') {
        wp_enqueue_media();
        wp_enqueue_script(
            'oddw-driver-profile',
            plugin_dir_url(dirname(dirname(__FILE__))) . 'admin/js/oddw-driver-profile.js',
            array('jquery'),
            defined('ODDW_VERSION') ? ODDW_VERSION : '1.0.0',
            true
        );
    }
});

// Show extra fields for driver role in user profile (admin)
add_action('show_user_profile', 'oddw_driver_profile_fields');
add_action('edit_user_profile', 'oddw_driver_profile_fields');
function oddw_driver_profile_fields($user) {
    if (!in_array('driver', (array) $user->roles, true)) return;
    ?>
    <h2><?php esc_html_e('Driver Details', 'orders-delivery-drivers-for-woocommerce'); ?></h2>
    <?php wp_nonce_field( 'oddw_driver_profile_update', 'oddw_driver_profile_nonce' ); ?>
    <table class="form-table">
        <tr>
            <th><label for="oddw_driver_image">Driver Image</label></th>
            <td>
                <?php $driver_image_id = get_user_meta($user->ID, 'oddw_driver_image', true); ?>
                <div class="oddw-image-upload-wrap">
                    <img id="oddw_driver_image_preview" src="<?php echo $driver_image_id ? esc_url(wp_get_attachment_url($driver_image_id)) : ''; ?>" style="max-width:100px;max-height:100px;display:<?php echo $driver_image_id ? 'block' : 'none'; ?>;margin-bottom:5px;" />
                    <input type="hidden" name="oddw_driver_image" id="oddw_driver_image" value="<?php echo esc_attr($driver_image_id); ?>" />
                    <button type="button" class="button oddw-upload-btn" data-target="oddw_driver_image">Upload/Select Image</button>
                    <button type="button" class="button oddw-remove-btn" data-target="oddw_driver_image" style="display:<?php echo $driver_image_id ? 'inline-block' : 'none'; ?>;">Remove</button>
                    <br><span class="description">Select or upload a driver profile image.</span>
                </div>
            </td>
        </tr>
        <tr>
            <th><label for="oddw_driver_car_image">Car Image</label></th>
            <td>
                <?php $car_image_id = get_user_meta($user->ID, 'oddw_driver_car_image', true); ?>
                <div class="oddw-image-upload-wrap">
                    <img id="oddw_driver_car_image_preview" src="<?php echo $car_image_id ? esc_url(wp_get_attachment_url($car_image_id)) : ''; ?>" style="max-width:100px;max-height:100px;display:<?php echo $car_image_id ? 'block' : 'none'; ?>;margin-bottom:5px;" />
                    <input type="hidden" name="oddw_driver_car_image" id="oddw_driver_car_image" value="<?php echo esc_attr($car_image_id); ?>" />
                    <button type="button" class="button oddw-upload-btn" data-target="oddw_driver_car_image">Upload/Select Image</button>
                    <button type="button" class="button oddw-remove-btn" data-target="oddw_driver_car_image" style="display:<?php echo $car_image_id ? 'inline-block' : 'none'; ?>;">Remove</button>
                    <br><span class="description">Select or upload a car image.</span>
                </div>
            </td>
        </tr>
        <tr>
            <th><label for="oddw_driver_car_type">Car Type</label></th>
            <td>
                <?php $car_type = get_user_meta($user->ID, 'oddw_driver_car_type', true); ?>
                <input type="text" name="oddw_driver_car_type" id="oddw_driver_car_type" value="<?php echo esc_attr($car_type); ?>" class="regular-text" />
                <br><span class="description">Enter car type (e.g., Sedan, Van, Truck).</span>
            </td>
        </tr>
        <tr>
            <th><label for="oddw_driver_phone">Phone Number</label></th>
            <td>
                <?php $phone = get_user_meta($user->ID, 'oddw_driver_phone', true); ?>
                <input type="tel" name="oddw_driver_phone" id="oddw_driver_phone" value="<?php echo esc_attr($phone); ?>" class="regular-text" />
                <br><span class="description">Enter driver's phone number.</span>
            </td>
        </tr>
        <tr>
            <th><label for="oddw_driver_license_plate">License Plate Number</label></th>
            <td>
                <?php $license_plate = get_user_meta($user->ID, 'oddw_driver_license_plate', true); ?>
                <input type="text" name="oddw_driver_license_plate" id="oddw_driver_license_plate" value="<?php echo esc_attr($license_plate); ?>" class="regular-text" />
                <br><span class="description">Enter driver's license plate number.</span>
            </td>
        </tr>
        <tr>
            <th><label for="oddw_driver_transportation_type">Transportation Type</label></th>
            <td>
                <?php $transportation_type = get_user_meta($user->ID, 'oddw_driver_transportation_type', true); ?>
                <select name="oddw_driver_transportation_type" id="oddw_driver_transportation_type" class="regular-text">
                    <option value=""><?php esc_html_e('--', 'orders-delivery-drivers-for-woocommerce'); ?></option>
                    <option value="Bicycle" <?php selected($transportation_type, 'Bicycle'); ?>><?php esc_html_e('Bicycle', 'orders-delivery-drivers-for-woocommerce'); ?></option>
                    <option value="Car" <?php selected($transportation_type, 'Car'); ?>><?php esc_html_e('Car', 'orders-delivery-drivers-for-woocommerce'); ?></option>
                    <option value="SUV" <?php selected($transportation_type, 'SUV'); ?>><?php esc_html_e('SUV', 'orders-delivery-drivers-for-woocommerce'); ?></option>
                    <option value="Truck" <?php selected($transportation_type, 'Truck'); ?>><?php esc_html_e('Truck', 'orders-delivery-drivers-for-woocommerce'); ?></option>
                </select>
                <br><span class="description">Select transportation type.</span>
            </td>
        </tr>
        <tr>
            <th><label for="oddw_driver_vehicle_model">Vehicle Model</label></th>
            <td>
                <?php $vehicle_model = get_user_meta($user->ID, 'oddw_driver_vehicle_model', true); ?>
                <input type="text" name="oddw_driver_vehicle_model" id="oddw_driver_vehicle_model" value="<?php echo esc_attr($vehicle_model); ?>" class="regular-text" />
                <br><span class="description">Enter vehicle model.</span>
            </td>
        </tr>
        <tr>
            <th><label for="oddw_driver_vehicle_color">Vehicle Color</label></th>
            <td>
                <?php $vehicle_color = get_user_meta($user->ID, 'oddw_driver_vehicle_color', true); ?>
                <input type="text" name="oddw_driver_vehicle_color" id="oddw_driver_vehicle_color" value="<?php echo esc_attr($vehicle_color); ?>" class="regular-text" />
                <br><span class="description">Enter vehicle color.</span>
            </td>
        </tr>
        <tr>
            <th><label for="oddw_driver_availability">Driver Availability</label></th>
            <td>
                <?php $availability = get_user_meta($user->ID, 'oddw_driver_availability', true); ?>
                <select name="oddw_driver_availability" id="oddw_driver_availability" class="regular-text">
                    <option value="" <?php selected($availability, ''); ?>><?php esc_html_e('Unavailable', 'orders-delivery-drivers-for-woocommerce'); ?></option>
                    <option value="on" <?php selected($availability, 'on'); ?>><?php esc_html_e('Available', 'orders-delivery-drivers-for-woocommerce'); ?></option>
                </select>
                <br><span class="description">Set driver availability.</span>
            </td>
        </tr>
    </table>
    <?php
}

// Save extra fields for driver role in user profile (admin)
add_action('personal_options_update', 'oddw_save_driver_profile_fields');
add_action('edit_user_profile_update', 'oddw_save_driver_profile_fields');
function oddw_save_driver_profile_fields($user_id) {
    $user = get_userdata($user_id);
    if (!in_array('driver', (array) $user->roles, true)) return;
    
    // Verify nonce for security
    if ( ! isset( $_POST['oddw_driver_profile_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['oddw_driver_profile_nonce'] ) ), 'oddw_driver_profile_update' ) ) {
        return;
    }
    
    if (isset($_POST['oddw_driver_image'])) {
        update_user_meta($user_id, 'oddw_driver_image', sanitize_text_field( wp_unslash( $_POST['oddw_driver_image'] ) ) );
    }
    if (isset($_POST['oddw_driver_car_image'])) {
        update_user_meta($user_id, 'oddw_driver_car_image', sanitize_text_field( wp_unslash( $_POST['oddw_driver_car_image'] ) ) );
    }
    if (isset($_POST['oddw_driver_car_type'])) {
        update_user_meta($user_id, 'oddw_driver_car_type', sanitize_text_field( wp_unslash( $_POST['oddw_driver_car_type'] ) ) );
    }
    if (isset($_POST['oddw_driver_phone'])) {
        update_user_meta($user_id, 'oddw_driver_phone', sanitize_text_field( wp_unslash( $_POST['oddw_driver_phone'] ) ) );
    }
    if (isset($_POST['oddw_driver_license_plate'])) {
        update_user_meta($user_id, 'oddw_driver_license_plate', sanitize_text_field( wp_unslash( $_POST['oddw_driver_license_plate'] ) ) );
    }
    if (isset($_POST['oddw_driver_transportation_type'])) {
        update_user_meta($user_id, 'oddw_driver_transportation_type', sanitize_text_field( wp_unslash( $_POST['oddw_driver_transportation_type'] ) ) );
    }
    if (isset($_POST['oddw_driver_vehicle_model'])) {
        update_user_meta($user_id, 'oddw_driver_vehicle_model', sanitize_text_field( wp_unslash( $_POST['oddw_driver_vehicle_model'] ) ) );
    }
    if (isset($_POST['oddw_driver_vehicle_color'])) {
        update_user_meta($user_id, 'oddw_driver_vehicle_color', sanitize_text_field( wp_unslash( $_POST['oddw_driver_vehicle_color'] ) ) );
    }
    if (isset($_POST['oddw_driver_availability'])) {
        update_user_meta($user_id, 'oddw_driver_availability', sanitize_text_field( wp_unslash( $_POST['oddw_driver_availability'] ) ) );
    }
}

