<?php 

$allowed_html = array(
  'br' => array(),
  'strong' => array(),
  'p' => array(),
  'span' => array(),
);

$accepted_languages = array('nl', 'de', 'en', 'fr', 'es');
$lang_data      = eurostocks_get_language_details();
$languages      = $lang_data['languages'];
$language_codes = $lang_data['language_codes'];
$default_lang   = $lang_data['default_lang'];
$current_lang   = $lang_data['current_lang'];
$diflang        = $lang_data['diflang'];
$show_lang      = $lang_data['show_lang'];


if (isset($data['vehicles']) && $data != '[]' && !isset($data['Status'])) {
  $vehicle_results = $data['vehicles'];
  $vehicles = $vehicle_results['vehicle'];
  $total = $vehicle_results['totalResults'];
  $total_pages = $vehicle_results['totalPages'];
} else {
  $vehicle_results = array();
  $vehicles = array();
  $total = 0;
  $total_pages = 0;
}

$options = get_option($this->plugin_name);
if (!is_array($options)) {
  $options = array();
}

$options = array_merge(
  array(
    'txt_srch'            => __('Import your new car from Germany?', 'eurostocks'),
    'txt_search_help'     => __('There are different ways in which you can pay BPM. Via the fixed table, with the help of price lists or with a valuation report. We specialize in calculating the lowest possible BPM. Therefore, always request a quote without obligation.', 'eurostocks'),
    'txt_search_help_show'=> 0,
  ),
  $options
);

$car_options = get_option($this->plugin_name . "_cars");

$makelist = get_option('eurostocks_makes');

if (isset($car_options['select_available_make'])) {
  $makes = $car_options['select_available_make'];
} elseif (isset($options['select_available_make'])) {
  $makes = $options['select_available_make'];
} else {
  $makes = array();
}
if (isset($makes) && is_array($makes)) {
  sort($makes);
}

if (!isset($car_options['live_models']) || (isset($car_options['live_models']) && $car_options['live_models'] == '0')) {
  if (! wp_next_scheduled('eurostocks_update_model_scheduler', [])) {
    if (isset($car_options['select_available_model'])) {
      $models_a = $car_options['select_available_model'];
    } else {
      $models_a = array();
    }
    if (isset($car_options['select_available_model_b'])) {
      $models_b = $car_options['select_available_model_b'];
    } else {
      $models_b = array();
    }
    $models = array_merge($models_a, $models_b);
  } else {
    $models = array();
    foreach ($makes as $make) {
      $carmodels = get_option('eurostocks_models_' . sanitize_text_field($make));
      if (isset($carmodels) && is_array($carmodels)) {
        $models = array_merge($models, $carmodels);
      }
    }
  }
  
  if (isset($car_options['excluded_models']) && $car_options['excluded_models'] != '') {
    $filtered_models = [];
    $excluded_models = explode(',', $car_options['excluded_models']);
    foreach ($models as $model) {
      list($model_name, $make) = explode('|', $model);
      $exclude = false;
      foreach ($excluded_models as $excluded_model) {
        if (strpos($model_name, $excluded_model) !== false) {
          $exclude = true;
          break;
        }
      }
      if (!$exclude) {
        $filtered_models[] = $model;
      }
    }
    $models = $filtered_models;
  }

  sort($models);
} else {
  $models = array();
}

if ((isset($options['enable_fl']) && $options['enable_fl'] == 1) || (isset($options['only_fl']) && $options['only_fl'] == 1)) {
  $eurostocks_fl = $options['enable_fl'];
  $eurostocks_only_fl = $options['only_fl'];
  $eurostocks_fl_interest = $options['fl_percentage'];
  $eurostocks_fl_duration = $options['fl_duration'];
  $eurostocks_fl_rest = $options['fl_restschuld'];
} else {
  $eurostocks_fl = 0;
  $eurostocks_only_fl = 0;
}
$caroptions = array(
  "AIR_SUSPENSION" => __("Air suspension", "eurostocks"),
  "ALARM_SYSTEM" => __("Alarm system", "eurostocks"),
  "ALLOY_WHEELS" => __("Alloy wheels", "eurostocks"),
  "ALL_SEASON_TIRES" => __("All season tires", "eurostocks"),
  "AMBIENT_LIGHTING" => __("Ambient lighting", "eurostocks"),
  "ANDROID_AUTO" => __("Android Auto", "eurostocks"),
  "ARM_REST" => __("Armrest", "eurostocks"),
  "AUTOMATIC_RAIN_SENSOR" => __("Automatic rain sensor", "eurostocks"),
  "AUXILIARY_HEATING" => __("Auxiliary heating", "eurostocks"),
  "BIODIESEL_SUITABLE" => __("Biodiesel suitable", "eurostocks"),
  "BLIND_SPOT_MONITOR" => __("Blind spot monitor", "eurostocks"),
  "BLUETOOTH" => __("Bluetooth", "eurostocks"),
  "CARGO_BARRIER" => __("Cargo barrier", "eurostocks"),
  "CARPLAY" => __("Apple CarPlay", "eurostocks"),
  "CD_MULTICHANGER" => __("CD multichanger", "eurostocks"),
  "CD_PLAYER" => __("CD player", "eurostocks"),
  "CENTRAL_LOCKING" => __("Central locking", "eurostocks"),
  "COLLISION_AVOIDANCE" => __("Pre-collision system", "eurostocks"),
  "DIGITAL_COCKPIT" => __("Digital cockpit", "eurostocks"),
  "DIMMING_INTERIOR_MIRROR" => __("Dimming interior mirror", "eurostocks"),
  "DISABLED_ACCESSIBLE" => __("Disabled accessible", "eurostocks"),
  "DISTANCE_WARNING_SYSTEM" => __("Distance warning system", "eurostocks"),
  "E10_ENABLED" => __("E10 enabled", "eurostocks"),
  "ELECTRIC_ADJUSTABLE_SEATS" => __("Electric adjustable seats", "eurostocks"),
  "ELECTRIC_BACKSEAT_ADJUSTMENT" => __("Electric backseat adjustment", "eurostocks"),
  "ELECTRIC_EXTERIOR_MIRRORS" => __("Electric exterior mirrors", "eurostocks"),
  "ELECTRIC_HEATED_REAR_SEATS" => __("Electric heated rear seats", "eurostocks"),
  "ELECTRIC_HEATED_SEATS" => __("Electric heated seats", "eurostocks"),
  "ELECTRIC_TAILGATE" => __("Electric tailgate", "eurostocks"),
  "ELECTRIC_WINDOWS" => __("Electric windows", "eurostocks"),
  "EMERGENCY_CALL_SYSTEM" => __("eCall / SOS system", "eurostocks"),
  "ESP" => __("ESP", "eurostocks"),
  "FATIGUE_WARNING_SYSTEM" => __("Fatigue warning system", "eurostocks"),
  "FOLD_FLAT_PASSENGER_SEAT" => __("Fold flat passenger seat", "eurostocks"),
  "FOUR_WHEEL_DRIVE" => __("Four wheel drive", "eurostocks"),
  "FRONT_FOG_LIGHTS" => __("Front fog lights", "eurostocks"),
  "FULL_SERVICE_HISTORY" => __("Full service history", "eurostocks"),
  "GLARE_FREE_HIGH_BEAM" => __("Glare-free high beam", "eurostocks"),
  "HANDS_FREE_PHONE_SYSTEM" => __("Hands-free phone system", "eurostocks"),
  "HEADLIGHT_WASHER_SYSTEM" => __("Headlight washer system", "eurostocks"),
  "HEAD_UP_DISPLAY" => __("Head-up display", "eurostocks"),
  "HEATED_STEERING_WHEEL" => __("Heated steering wheel", "eurostocks"),
  "HEATED_WINDSHIELD" => __("Heated windshield", "eurostocks"),
  "HIGH_BEAM_ASSIST" => __("High beam assist", "eurostocks"),
  "HILL_START_ASSIST" => __("Hill start assist", "eurostocks"),
  "HYBRID_PLUGIN" => __("Hybrid plugin", "eurostocks"),
  "IMMOBILIZER" => __("Immobilizer", "eurostocks"),
  "ISOFIX" => __("Isofix", "eurostocks"),
  "KEYLESS_ENTRY" => __("Keyless entry", "eurostocks"),
  "LANE_DEPARTURE_WARNING" => __("Lane departure warning", "eurostocks"),
  "LIGHT_SENSOR" => __("Light sensor", "eurostocks"),
  "NAVIGATION_SYSTEM" => __("Navigation system", "eurostocks"),
  "NONSMOKER_VEHICLE" => __("Non-smoker vehicle", "eurostocks"),
  "POWER_ASSISTED_STEERING" => __("Power assisted steering", "eurostocks"),
  "SOUND_SYSTEM" => __("Sound system", "eurostocks"),
  "SUNROOF" => __("Sunroof", "eurostocks"),
  "SPEED_LIMITER" => __("Speed limiter", "eurostocks"),
  "START_STOP_SYSTEM" => __("Start stop system", "eurostocks"),
  "TIRE_PRESSURE_MONITORING" => __("Tire pressure monitoring", "eurostocks"),
  "TRAFFIC_SIGN_RECOGNITION" => __("Traffic sign recognition", "eurostocks"),
  "TV" => __("TV", "eurostocks"),
  "VEGETABLEOILFUEL_SUITABLE" => __("Vegetable oil fuel suitable", "eurostocks"),
  "WARANTY" => __("Warranty", "eurostocks"),
  "WINTER_TIRES" => __("Winter tires", "eurostocks"),
  "SPORT_PACKAGE" => __("Sport package", "eurostocks"),
  "SPORT_SEATS" => __("Sport seats", "eurostocks"),
  "PERFORMANCE_HANDLING_SYSTEM" => __("Performance handling system", "eurostocks"),
  "PANORAMIC_GLASS_ROOF" => __("Panoramic glass roof", "eurostocks"),
  "DAB_RADIO" => __("DAB radio", "eurostocks"),
  "TUNER_RADIO" => __("Tuner / Radio", "eurostocks"),
  "TRAILER_COUPLING" => __("Coupling", "eurostocks"),
);

asort($caroptions);

$parkingsensors = array(
  "FRONT_SENSORS" => __("Front sensors", "eurostocks"),
  "REAR_SENSORS" => __("Rear sensors", "eurostocks"),
  "REAR_VIEW_CAM" => __("Rear view camera", "eurostocks"),
  "AUTOMATIC_PARKING" => __("Automatic parking", "eurostocks"),
  "CAM_360_DEGREES" => __("360°-camera", "eurostocks"),
);


if (count($language_codes) > 1) {
  $eurostocks_categories = get_option('eurostocks_categories_' . $current_lang);
  if (!isset($eurostocks_categories) || !is_array($eurostocks_categories)) {
    $eurostocks_categories = get_option('eurostocks_categories');
  }

  $eurostocks_headlight_types = get_option('eurostocks_headlight_types_' . $current_lang);
  if (!isset($eurostocks_headlight_types) || !is_array($eurostocks_headlight_types)) {
    $eurostocks_headlight_types = get_option('eurostocks_headlight_types');
  }

  $eurostocks_door_counts = get_option('eurostocks_door_counts_' . $current_lang);
  if (!isset($eurostocks_door_counts) || !is_array($eurostocks_door_counts)) {
    $eurostocks_door_counts = get_option('eurostocks_door_counts');
  }

  $eurostocks_exterior_colors = get_option('eurostocks_exterior_colors_' . $current_lang);
  if (!isset($eurostocks_exterior_colors) || !is_array($eurostocks_exterior_colors)) {
    $eurostocks_exterior_colors = get_option('eurostocks_exterior_colors');
  }

  $eurostocks_climatisations = get_option('eurostocks_climatisations_' . $current_lang);
  if (!isset($eurostocks_climatisations) || !is_array($eurostocks_climatisations)) {
    $eurostocks_climatisations = get_option('eurostocks_climatisations');
  }

  $eurostocks_interior_types = get_option('eurostocks_interior_types_' . $current_lang);
  if (!isset($eurostocks_interior_types) || !is_array($eurostocks_interior_types)) {
    $eurostocks_interior_types = get_option('eurostocks_interior_types');
  }

  $eurostocks_speed_controls = get_option('eurostocks_speed_controls_' . $current_lang);
  if (!isset($eurostocks_speed_controls) || !is_array($eurostocks_speed_controls)) {
    $eurostocks_speed_controls = get_option('eurostocks_speed_controls');
  }

  $eurostocks_transmissions = get_option('eurostocks_transmissions_' . $current_lang);
  if (!isset($eurostocks_transmissions) || !is_array($eurostocks_transmissions)) {
    $eurostocks_transmissions = get_option('eurostocks_transmissions');
  }

  $eurostocks_fuel_types = get_option('eurostocks_fuel_types_' . $current_lang);
  if (!isset($eurostocks_fuel_types) || !is_array($eurostocks_fuel_types)) {
    $eurostocks_fuel_types = get_option('eurostocks_fuel_types');
  }
} else {
  $eurostocks_categories = get_option('eurostocks_categories');
  $eurostocks_headlight_types = get_option('eurostocks_headlight_types');
  $eurostocks_door_counts = get_option('eurostocks_door_counts');
  $eurostocks_exterior_colors = get_option('eurostocks_exterior_colors');
  $eurostocks_climatisations = get_option('eurostocks_climatisations');
  $eurostocks_interior_types = get_option('eurostocks_interior_types');
  $eurostocks_speed_controls = get_option('eurostocks_speed_controls');
  $eurostocks_transmissions = get_option('eurostocks_transmissions');
  $eurostocks_fuel_types = get_option('eurostocks_fuel_types');
}

if (isset($options['enable_fl']) && $options['enable_fl'] == 1) {
  $eurostocks_fl = $options['enable_fl'];
  $eurostocks_fl_interest = $options['fl_percentage'];
  $eurostocks_fl_duration = $options['fl_duration'];
  $eurostocks_fl_rest = $options['fl_restschuld'];
} else {
  $eurostocks_fl = 0;
}
if($diflang && isset($options[esc_html($current_lang)]) && isset($options[esc_html($current_lang)]['txt_select_make']) && $options[esc_html($current_lang)]['txt_select_make'] != ""  ){ 
  $select_make = esc_html($options[esc_html($current_lang)]['txt_select_make']); 
}else{ 
if (isset($options['txt_select_make']) && $options['txt_select_make'] != "") {
   $select_make = esc_html($options['txt_select_make']);
 } else {
   $select_make = __("Select make", 'eurostocks');
 } 
}

if($diflang && isset($options[esc_html($current_lang)]) && isset($options[esc_html($current_lang)]['txt_select_model']) && $options[esc_html($current_lang)]['txt_select_model'] != ""  ){ 
 $select_model = esc_html($options[esc_html($current_lang)]['txt_select_model']); 
}else{ 
if (isset($options['txt_select_model']) && $options['txt_select_model'] != "") {
  $select_model = esc_html($options['txt_select_model']);
} else {
  $select_model = __("Select model", 'eurostocks');
} 
}
?>
<script>
  var EUROSTOCKS_TRANSLATIONS = {
    "ERROR_GENERIC": "<?php esc_html_e("An error occurred, please try again later.", "eurostocks"); ?>",
    "LIVE_MODELS_LOAD": "<?php esc_html_e("Loading...", "eurostocks"); ?>",
    "MODELS_NO_PREFERENCE": "<?php esc_html_e("No preference", "eurostocks"); ?>",
    "MODELS_SELECT": "<?php echo esc_html($select_model); ?>",
    "MAKES_SELECT": "<?php echo esc_html($select_make); ?>",
    "ALL": "<?php echo esc_html("All", "eurostocks"); ?>",
    <?php if(isset($atts['model_group']) && $atts['model_group'] != ''){ ?>
    "MODEL": "<?php echo esc_html($atts['model_group']); ?>|GROUP",
    <?php }elseif(isset($atts['model']) && $atts['model'] != ''){ ?>
    "MODEL": "<?php echo esc_html($atts['model']); ?>"
    <?php } ?>
  };

  <?php 
  if (isset($car_options['excluded_models']) && $car_options['excluded_models'] != '') {

    $excluded_models = explode(',', $car_options['excluded_models']);
    ?>
    var EXCLUDED_MODELS = <?php echo json_encode($excluded_models); ?>;
  <?php }else{ ?>
    var EXCLUDED_MODELS = [];
  <?php } 
  ?>
</script>
