<?php 
global $post;
$lang = 1;
$gocars_display = get_post_meta( $post->ID, 'gocars_display', true );
$fleet_number = get_post_meta( $post->ID, 'fleet_number', true ); 
$fleet_sorting = get_post_meta( $post->ID, 'fleet_sorting', true );

$car_info_display = get_post_meta($post->ID, 'car_info_display', true);
$car_info_display = !empty($car_info_display) ? explode(',', $car_info_display) : array();

$car_features = get_post_meta($post->ID, 'car_features', true);
$car_features = !empty($car_features) ? explode(',', $car_features) : array();

$selected_vehicles = get_post_meta($post->ID, 'selected_vehicles', true);
$selected_vehicles = !empty($selected_vehicles) ? explode(',', $selected_vehicles) : array();

$display_selected_vehicles = get_post_meta($post->ID, 'display_selected_vehicles', true);
$cached_opts = get_post_meta( $post->ID, 'options_data', true );
$option_arr = null;
if ( $cached_opts !== '' && $cached_opts !== false ) {
    $option_arr = json_decode( wp_unslash( $cached_opts ) );
}
if ( empty( $option_arr ) || ! is_object( $option_arr ) ) {
    $option_arr = getGcOptions($post->ID);
}

$vehicle_types = null;
$vehicle_meta_raw = get_post_meta( $post->ID, 'vehicle_data', true );
if ( $vehicle_meta_raw !== '' && $vehicle_meta_raw !== false ) {
    $decoded_vt = json_decode( wp_unslash( $vehicle_meta_raw ), true );
    if ( is_array( $decoded_vt ) ) {
        $vehicle_types = $decoded_vt;
    }
}
if ( $vehicle_types === null ) {
    $vehicle_types = getGcVehicleTypes( $post->ID, $option_arr, null );
}

$cached_models = gocars_admin_vehicle_models_branch_from_meta( $post->ID, $lang, 'all_vehicles_data' );
if ( $cached_models !== null && count( $cached_models ) > 0 ) {
    $type_names = gocars_models_dedupe_first_per_type_name( $cached_models );
    $vehicles   = $cached_models;
} else {
    $type_names = getGcCarModels($lang, $post->ID);
    $vehicles   = getAllGcCarModels($lang, $post->ID);
}
$vehicles_arr = array();
$fleet_types  = array();
$vehicles     = is_array($vehicles) ? $vehicles : array();
$type_names   = is_array($type_names) ? $type_names : array();

foreach ($vehicles as $key => $value) {
    if (
        is_object($value) &&
        isset($value->id) &&
        isset($value->make_name) &&
        isset($value->model_name)
    ) {
        $vehicles_arr[$value->id] = $value->make_name . ' ' . $value->model_name;
    }
}
foreach ($type_names as $key => $value) {
    if (is_object($value) && property_exists($value, 'type_name') && $value->type_name !== '') {
        $fleet_types[] = $value->type_name;
    }
}

$fleet_car_one = '';
$fleet_type = get_post_meta( $post->ID, 'fleet_type', true );
$vehicle_type = get_post_meta( $post->ID, 'vehicle_type', true );
$button_text = get_post_meta($post->ID, 'button_text', true);
$price_text = get_post_meta($post->ID, 'price_text', true);
$button_classes = get_post_meta($post->ID, 'button_classes', true);
$fleet_wrap_classes = get_post_meta($post->ID, 'fleet_wrap_classes', true);
$single_fleet_classes = get_post_meta($post->ID, 'single_fleet_classes', true);
$basic_style = get_post_meta($post->ID, 'basic_style', true);

$display_elements_wrapper = get_post_meta($post->ID, 'display_elements_wrapper', true);
$elements_wrapper_data = get_post_meta($post->ID, 'elements_wrapper_data', true);
$elements_wrapper_data_arr = json_decode($elements_wrapper_data);

$car_info = array(
    'image' =>  __( 'Image', 'gocars_admin' ),
    'type' =>  __( 'Type', 'gocars_admin' ),
    'make' =>  __( 'Make', 'gocars_admin' ),
    'model' =>  __( 'Model', 'gocars_admin' ),
    'description' =>  __( 'Description', 'gocars_admin' ),
    'features' =>  __( 'Features', 'gocars_admin' ),
    'price' =>  __( 'Price', 'gocars_admin' ),
    'button' =>  __( 'Button', 'gocars_admin' ),
);

$car_features_info = array(
    'doors' =>  __( 'Doors', 'gocars_admin' ),
    'passengers' =>  __( 'Passengers', 'gocars_admin' ),
    'luggages' =>  __( 'Luggages', 'gocars_admin' ),
    'handbags' =>  __( 'Handbags', 'gocars_admin' ),
    'transmission' =>  __( 'Transmission', 'gocars_admin' ),
    'air_condition' =>  __( 'Air Condition', 'gocars_admin' ),
    'fuel' =>  __( 'Fuel', 'gocars_admin' ),
    'abs' =>  __( 'ABS', 'gocars_admin' ),
    'bluetooth' =>  __( 'Bluetooth', 'gocars_admin' ),
    'navigation' =>  __( 'Navigation', 'gocars_admin' ),
    'usb' =>  __( 'USB', 'gocars_admin' ),
    'keyless' =>  __( 'Keyless', 'gocars_admin' ),
    'rear_trunk' =>  __( 'Rear Trunk', 'gocars_admin' ),
    'cc' =>  __( 'CC', 'gocars_admin' ),
    'under_seat_space' =>  __( 'Under Seat Space', 'gocars_admin' ),
    'antispeed' =>  __( 'Anti-Speed', 'gocars_admin' ),
    'apple_carplay' =>  __( 'Apple Car Play', 'gocars_admin' ),
    'android_auto' =>  __( 'Android Auto', 'gocars_admin' ),
);

?>
   
<table class="form-table shortcode-table fleet_fields" id="fleet_fields" role="presentation"<?php echo ($gocars_display != 'fleet') ? ' style="display: none;"' : '' ?>>
    <tbody>
        <tr>
            <th scope="row">
                <label for="fleet_number"><?php esc_html_e( 'Number of Fleet','gocars_admin' ); ?></label>
            </th>
            <td>
                <fieldset>
                    <legend class="screen-reader-text">
                        <span>
                            <?php esc_html_e( 'Number of Fleet','gocars_admin' ); ?>
                        </span>
                    </legend>
                    <label>
                        <select name="fleet_number" id="fleet_number">
                            <option value=""> <?php echo __( 'Show All Fleet', 'gocars_admin' ); ?> </option>
                            <?php
                            for( $i=1; $i<=10; $i++ ) {
                                $selected = ($fleet_number == $i) ? ' selected' : '';
                                ?>
                                <option value="<?php echo $i; ?>"<?php echo $selected; ?>><?php echo $i; ?></option>
                                <?php 
                            }
                            ?>
                        </select>
                    </label>
                    
                </fieldset>
            </td>
        </tr>
        <tr>
            <th scope="row">
                <label for="display_selected_vehicles"><?php esc_html_e( 'Display Selected Vehicles','gocars_admin' ); ?></label>
            </th>
            <td>
                <fieldset>
                    <legend class="screen-reader-text">
                        <span>
                            <?php esc_html_e( 'Display Selected Vehicles','gocars_admin' ); ?>
                        </span>
                    </legend>
                    <label>
                        <select name="display_selected_vehicles" id="display_selected_vehicles">
                            <option value="no"<?php echo ($display_selected_vehicles == 'no') ? ' selected' : ''; ?>> <?php echo __( 'No', 'gocars_admin' ); ?> </option>
                            <option value="yes"<?php echo ($display_selected_vehicles == 'yes') ? ' selected' : ''; ?>> <?php echo __( 'Yes', 'gocars_admin' ); ?> </option>
                        </select>
                    </label>
                </fieldset>
            </td>
        </tr>
        <tr class="selected-vehicles-row"<?php echo $display_selected_vehicles == 'no' || $display_selected_vehicles == '' ? ' style="display: none;"' : ''; ?>>
            <th scope="row">
                <label for="selected_vehicles"><?php esc_html_e( 'Selected Vehicles','gocars_admin' ); ?></label>
            </th>
            <td>
                <fieldset>
                    <legend class="screen-reader-text">
                        <span>
                            <?php esc_html_e( 'Selected Vehicles','gocars_admin' ); ?>
                        </span>
                    </legend>
                    
                    <label class="select-2-label">
                        <select class="select-2-item selected_vehicles" multiple name="selected_vehicles[]" id="selected_vehicles"<?php echo ($selected_vehicles != '') ? " data-value='" . json_encode($selected_vehicles) . "'" : ''; ?>>
                            <?php 
                            if ( count($selected_vehicles) != 0 ) : 
                                // Sort the $vehicles_arr array based on the desired order
                                uksort($vehicles_arr, function($a, $b) use ($selected_vehicles) {
                                    return array_search($a, $selected_vehicles) - array_search($b, $selected_vehicles);
                                });
                            endif; 
                            foreach ($vehicles_arr as $key => $value) {
                                ?>
                                <option value="<?php echo $key; ?>"<?php echo (in_array($key, $selected_vehicles) || count($selected_vehicles) === 0 ) ? ' selected' : ''; ?> title="<?php echo __( $value, 'gocars_admin' ); ?>"><?php echo __( $value, 'gocars_admin' ); ?></option>
                                <?php
                            }
                            ?>
                        </select>
                        <button class="select-2-all button"><?php esc_html_e( 'Select All','gocars_admin' ); ?></button>
                    </label>
                    <input type="hidden" name="vehicles_arr" value='<?php echo json_encode($vehicles_arr); ?>'>
                    
                </fieldset>
            </td>
        </tr>
        <tr>
            <th scope="row">
                <label for="fleet_type"><?php esc_html_e( 'Fleet Type','gocars_admin' ); ?></label>
            </th>
            <td>
                <fieldset>
                    <legend class="screen-reader-text">
                        <span>
                            <?php esc_html_e( 'Fleet Type','gocars_admin' ); ?>
                        </span>
                    </legend>
                    <label>
                        <select name="fleet_type" id="fleet_type">
                            <option value=""> <?php echo __( 'Show All Fleet Types', 'gocars_admin' ); ?> </option>
                            <?php
                            foreach ($fleet_types as $key => $value) {
                                ?>
                                <option value="<?php echo $value; ?>"<?php echo ($fleet_type == $value) ? ' selected' : ''; ?>> <?php echo ucfirst($value); ?> </option>
                                <?php
                            }
                            ?>
                        </select>
                    </label>
                    
                </fieldset>
            </td>
        </tr>
        <?php if ( count($vehicle_types) > 0 ) : ?>
            <tr>
                <th scope="row">
                    <label for="vehicle_type"><?php esc_html_e( 'Vehicle Type','gocars_admin' ); ?></label>
                </th>
                <td>
                    <fieldset>
                        <legend class="screen-reader-text">
                            <span>
                                <?php esc_html_e( 'Vehicle Type','gocars_admin' ); ?>
                            </span>
                        </legend>
                        <label>
                            <select name="vehicle_type" id="vehicle_type">
                                <option value=""> <?php echo __( 'Show All Vehicle Types', 'gocars_admin' ); ?> </option>
                                <?php
                                foreach ($vehicle_types as $key => $value) {
                                    ?>
                                    <option value="<?php echo $value; ?>"<?php echo ($vehicle_type == $value) ? ' selected' : ''; ?>> <?php echo ucfirst($value); ?> </option>
                                    <?php 
                                }
                                ?>
                            </select>
                        </label>
                        
                    </fieldset>
                </td>
            </tr>
        <?php endif; ?>
        <tr>
            <th scope="row">
                <label for="fleet_sorting"><?php esc_html_e( 'Sorting Fleet','gocars_admin' ); ?></label>
            </th>
            <td>
                <fieldset>
                    <legend class="screen-reader-text">
                        <span>
                            <?php esc_html_e( 'Sorting Fleet','gocars_admin' ); ?>
                        </span>
                    </legend>
                    <label>
                        <select name="fleet_sorting" id="fleet_sorting">
                            <option value="vehicle_order"<?php echo ($fleet_sorting == 'vehicle_order') ? ' selected' : ''; ?>><?php echo __( 'Vehicle Order', 'gocars_admin' ); ?></option>
                            <option value="price_asc"<?php echo ($fleet_sorting == 'price_asc') ? ' selected' : ''; ?>><?php echo __( 'Price Ascending', 'gocars_admin' ); ?></option>
                            <option value="price_desc"<?php echo ($fleet_sorting == 'price_desc') ? ' selected' : ''; ?>><?php echo __( 'Price Descending', 'gocars_admin' ); ?></option>
                        </select>
                    </label>
                    <?php 
                    $sort_type = (isset($option_arr->o_sort_type) && $option_arr->o_sort_type == 'price_asc') ? 'Price Ascending' : 'Vehicle Order';
                    $sort_type = (isset($option_arr->o_sort_type) && $option_arr->o_sort_type == 'price_desc') ? 'Price Descending' : $sort_type;
                    ?>
                    <p class="small">The default value of the Booking System is <?php echo $sort_type; ?></p>
                    
                </fieldset>
            </td>
        </tr>     
        <tr>
            <th scope="row">
                <label for="car_info_display"><?php esc_html_e( 'Car Info Display','gocars_admin' ); ?></label>
            </th>
            <td>
                <fieldset>
                    <legend class="screen-reader-text">
                        <span>
                            <?php esc_html_e( 'Car Info Display','gocars_admin' ); ?>
                        </span>
                    </legend>
                    <label class="select-2-label">
                        <select class="select-2-item car-info" multiple name="car_info_display[]" id="car_info_display"<?php echo ($car_info_display != '') ? " data-value='" . json_encode($car_info_display) . "'" : ''; ?>>
                            <?php 
                            if ( count($car_info_display) != 0 && is_array($car_info)) : 
                                // Sort the $car_info array based on the desired order
                                uksort($car_info, function($a, $b) use ($car_info_display) {
                                    return array_search($a, $car_info_display) - array_search($b, $car_info_display);
                                });
                            endif; 
                            foreach ($car_info as $key => $value) {
                                ?>
                                <option value="<?php echo $key; ?>"<?php echo (in_array($key, $car_info_display) || count($car_info_display) === 0 ) ? ' selected' : ''; ?> title="<?php echo __( $value, 'gocars_admin' ); ?>"><?php echo __( $value, 'gocars_admin' ); ?></option>
                                <?php
                            }
                            ?>
                        </select>
                        <button class="select-2-all button"><?php esc_html_e( 'Select All','gocars_admin' ); ?></button>
                    </label>
                    <input type="hidden" name="car_info" value='<?php echo json_encode($car_info); ?>'>
                </fieldset>
            </td>
        </tr>
        <tr class="features-row">
            <th scope="row">
                <label for="car_features"><?php esc_html_e( 'Features','gocars_admin' ); ?></label>
            </th>
            <td>
                <fieldset>
                    <legend class="screen-reader-text">
                        <span>
                            <?php esc_html_e( 'Features','gocars_admin' ); ?>
                        </span>
                    </legend>
                    <label class="select-2-label">
                        <select class="select-2-item car-features" multiple name="car_features[]" id="car_features"<?php echo ($car_features != '') ? " data-value='" . json_encode($car_features) . "'" : ''; ?>>
                            <?php 
                            if ( count($car_features) != 0 ) : 
                                // Sort the $car_features_info array based on the desired order
                                uksort($car_features_info, function($a, $b) use ($car_features) {
                                    return array_search($a, $car_features) - array_search($b, $car_features);
                                });
                            endif; 
                            foreach ($car_features_info as $key => $value) {
                                ?>
                                <option value="<?php echo $key; ?>"<?php echo (in_array($key, $car_features) || count($car_features) === 0 ) ? ' selected' : ''; ?> title="<?php echo __( $value, 'gocars_admin' ); ?>"><?php echo __( $value, 'gocars_admin' ); ?></option>
                                <?php
                            }
                            ?>
                        </select>
                        <button class="select-2-all button"><?php esc_html_e( 'Select All','gocars_admin' ); ?></button>
                    </label>
                    <input type="hidden" name="car_features_info" value='<?php echo json_encode($car_features_info); ?>'>
                    
                </fieldset>
            </td>
        </tr>
        <tr>
            <th scope="row">
                <label for="fleet_wrap_classes"><?php esc_html_e( 'Fleet Wrap Classes','gocars_admin' ); ?></label>
            </th>
            <td>
                <fieldset>
                    <legend class="screen-reader-text">
                        <span>
                            <?php esc_html_e( 'Fleet Wrap Classes','Classes' ); ?>
                        </span>
                    </legend>
                    <label>
                        <input name="fleet_wrap_classes" placeholder="<?php _e('Fleet Wrap Custom Classes','gocars_admin'); ?>" type="text" id="fleet_wrap_classes" value="<?php echo esc_attr( $fleet_wrap_classes ) ; ?>" class="regular-text">
                    </label>
                    
                </fieldset>
            </td>
        </tr>
        <tr>
            <th scope="row">
                <label for="single_fleet_classes"><?php esc_html_e( 'Single Car Classes','gocars_admin' ); ?></label>
            </th>
            <td>
                <fieldset>
                    <legend class="screen-reader-text">
                        <span>
                            <?php esc_html_e( 'Single Car Classes','Classes' ); ?>
                        </span>
                    </legend>
                    <label>
                        <input name="single_fleet_classes" placeholder="<?php _e('Single Car Custom Classes','gocars_admin'); ?>" type="text" id="single_fleet_classes" value="<?php echo esc_attr( $single_fleet_classes ) ; ?>" class="regular-text">
                    </label>
                    
                </fieldset>
            </td>
        </tr>
        <tr class="button-row">
            <th scope="row">
                <label for="button_classes"><?php esc_html_e( 'Button Classes','gocars_admin' ); ?></label>
            </th>
            <td>
                <fieldset>
                    <legend class="screen-reader-text">
                        <span>
                            <?php esc_html_e( 'Button Classes','Classes' ); ?>
                        </span>
                    </legend>
                    <label>
                        <input name="button_classes" placeholder="<?php _e('Button Classes','gocars_admin'); ?>" type="text" id="button_classes" value="<?php echo esc_attr( $button_classes ); ?>" class="regular-text">
                    </label>
                    
                </fieldset>
            </td>
        </tr>
        <tr class="button-row">
            <th scope="row">
                <label for="button_text"><?php esc_html_e( 'Button Text','gocars_admin' ); ?></label>
            </th>
            <td>
                <fieldset>
                    <legend class="screen-reader-text">
                        <span>
                            <?php esc_html_e( 'Button Text','Classes' ); ?>
                        </span>
                    </legend>
                    <label>
                        <input name="button_text" placeholder="<?php _e('Button Text','gocars_admin'); ?>" type="text" maxlength="25" id="button_text" value="<?php echo esc_attr( $button_text ) ; ?>" class="regular-text">
                    </label>
                    
                </fieldset>
            </td>
        </tr>
        <tr>
            <th scope="row">
                <label for="price_text"><?php esc_html_e( 'Price Text','gocars_admin' ); ?></label>
            </th>
            <td>
                <fieldset>
                    <legend class="screen-reader-text">
                        <span>
                            <?php esc_html_e( 'Price Text','Classes' ); ?>
                        </span>
                    </legend>
                    <label>
                        <input name="price_text" placeholder="<?php _e('Price Text','gocars_admin'); ?>" type="text" id="price_text" value="<?php echo esc_attr( $price_text ) ; ?>" class="regular-text">
                    </label>
                    <p class="small">The price should be always in curly brackets,<br>Example: from {price}€ per day</p>
                    
                </fieldset>
            </td>
        </tr>
        <tr>
            <th scope="row">
                <label for="basic_style"><?php esc_html_e( 'Basic Style','gocars_admin' ); ?></label>
            </th>
            <td>
                <fieldset>
                    <legend class="screen-reader-text">
                        <span>
                            <?php esc_html_e( 'Basic Style','gocars_admin' ); ?>
                        </span>
                    </legend>
                    <label>
                        <select name="basic_style" id="basic_style">
                            <option value="no"<?php echo ($basic_style == 'no') ? ' selected' : ''; ?>> <?php echo __( 'No', 'gocars_admin' ); ?> </option>
                            <option value="yes"<?php echo ($basic_style == 'yes') ? ' selected' : ''; ?>> <?php echo __( 'Yes', 'gocars_admin' ); ?> </option>
                        </select>
                    </label>
                </fieldset>
            </td>
        </tr>

        <tr>
            <th scope="row">
                <label for="display_elements_wrapper"><?php esc_html_e( 'Elements Wrapper','gocars_admin' ); ?></label>
            </th>
            <td>
                <fieldset>
                    <legend class="screen-reader-text">
                        <span>
                            <?php esc_html_e( 'Elements Wrapper','gocars_admin' ); ?>
                        </span>
                    </legend>
                    <label>
                        <select name="display_elements_wrapper" id="display_elements_wrapper">
                            <option value="no"<?php echo ($display_elements_wrapper == 'no') ? ' selected' : ''; ?>> <?php echo __( 'No', 'gocars_admin' ); ?> </option>
                            <option value="yes"<?php echo ($display_elements_wrapper == 'yes') ? ' selected' : ''; ?>> <?php echo __( 'Yes', 'gocars_admin' ); ?> </option>
                        </select>
                    </label>
                </fieldset>
            </td>
        </tr>
        <tr class="elements-wrapper-row"<?php echo $display_elements_wrapper == 'no' || $display_elements_wrapper == '' ? ' style="display: none;"' : ''; ?>>
            <th scope="row">
               
            </th>
            <td>
                <fieldset>
                    <p class="small">Αν επιλέξουμε Elements Wrapper το Car Info Display θα σταματήσει να λειτουργεί</p>
                    <legend class="screen-reader-text">
                        <span>
                            <?php esc_html_e( 'Elements Wrapper','gocars_admin' ); ?>
                        </span>
                    </legend>
                    <div id="repeater-container">
                        <?php 
                        if ( $elements_wrapper_data_arr ) : 
                            foreach ($elements_wrapper_data_arr as $data => $element) {
                                ?>
                                <div class="field-group">
                                    <label class="repeater-field select-2-label">
                                        <select class="select-2-item elements_wrapper" multiple name="elements_wrapper[]" id="elements_wrapper-<?php echo $data; ?>"<?php echo ($elements_wrapper_data_arr) ? " data-value='" . json_encode($element) . "'" : ''; ?>>
                                            <?php 
                                            foreach ($car_info as $key => $value) {
                                                ?>
                                                <option value="<?php echo $key; ?>"<?php echo (in_array($key, $element)  ) ? ' selected' : ''; ?> title="<?php echo __( $value, 'gocars_admin' ); ?>"><?php echo __( $value, 'gocars_admin' ); ?></option>
                                                <?php
                                            }
                                            ?>
                                        </select>
                                    </label>   
                                    <button class="remove-field button"><?php esc_html_e( 'Remove','gocars_admin' ); ?></button>                             
                                </div>
                                <?php
                            }
                        endif; 
                        ?>
                        
                    </div>           
                    <input type="hidden" name="elements_wrapper_data" id="elements_wrapper_data" value="<?php echo esc_attr( $elements_wrapper_data ) ; ?>">         
                </fieldset>
                <fieldset>
                    <label>
                        <button id="add-field" class="button add-field"><?php esc_html_e( 'Add Row','gocars_admin' ); ?></button>
                    </label>
                </fieldset>
            </td>
        </tr>

    </tbody>
</table>   
<div class="hidden" data-id="fleet_fields">
    <label class="repeater-field select-2-label">
        <select class="select-2-item elements_wrapper" multiple name="elements_wrapper[]" id="elements_wrapper-{INDEX}" data-value="">
            <?php 
            foreach ($car_info as $key => $value) {
                ?>
                <option value="<?php echo $key; ?>" title="<?php echo __( $value, 'gocars_admin' ); ?>"><?php echo __( $value, 'gocars_admin' ); ?></option>
                <?php
            }
            ?>
        </select>
    </label>
</div>


