<?php
if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Provide a admin area view for the plugin
*
* This file is used to markup the admin-facing aspects of the plugin.
*
* @link       https://www.eurostocks.com
* @since      1.0.0
*
* @package    Eurostocks
* @subpackage Eurostocks/admin/partials
*/
?>
<script>
  var EUROSTOCKS_TRANSLATIONS = {
    "GET_MODELS_SUCCESS": "<?php esc_html_e("The models are scheduled to be retrieved in 30 seconds via a cron job."); ?>",
    "GET_MODELS_ERROR": "<?php esc_html_e("Something went wrong when retrieving the models, check whether the data is correct on the settings page."); ?>",
  };
</script>
<div class="eurostocks-wrap">
  <br><br>
  <?php
  $options = get_option($this->plugin_name);
  $cars = get_option('eurostocks_cars');
  $makes = eurostocks_normalize_makes_list(get_option('eurostocks_makes'));
  $models = get_option('eurostocks_models');


  if(isset($cars['select_available_make'])){
    $selected_makes = $cars['select_available_make'];
  }else if(isset($options['select_available_make'])){
    $selected_makes = $options['select_available_make'];
  }else{
    $selected_makes = array();
  }

  if(isset($cars['select_available_model'])){
    $selected_models = $cars['select_available_model'];
    if(isset($cars['select_available_model_b'])){
      $selected_models_b = $cars['select_available_model_b'];
    }else{
      $selected_models_b = array();
    }
  }else{
    $selected_models = array();
    $selected_models_b = array();
  }

  if(isset($cars['select_makes'])){
    if($cars['select_makes'] == "1"){
      $select_makes = 1;
    } else {
      $select_makes = 0;
    }
  }else {
    if(isset($options['select_makes']) && $options['select_makes'] == "1"){
      $select_makes = 1;
    } else {
      $select_makes = 0;
    }
  }

  if (!isset($cars['live_models']) || $cars['live_models'] != 1) {
    $live_models = 0;
  } else {
    $live_models = $cars['live_models'];
  }

  if (!isset($cars['make_only']) || ($cars['make_only'] != 1 && $cars['make_only'] != 2)) {
    $make_only = 0;
  } else {
    $make_only = $cars['make_only'];
  }

  if(isset($cars['excluded_models'])){
    $excluded_models = $cars['excluded_models'];
  }else{
    $excluded_models = "";
  }
  ?>

  <form method="post" id="eurostocks-settings" name="eurostocks_options" action="options.php">
    <div id="general">
      <?php

      settings_fields('eurostocks_cars');
      do_settings_sections('eurostocks_cars');
      wp_nonce_field( 'eurostocks_cars', 'eurostocks_cars_nonce' );
      
      ?>
      <div class="postbox-container">

        <div class="meta-box-holder">
          <div class="euro-postbox">
          <div class="eurostocks-admin-header">
              <h2><?php esc_html_e('Makes', 'eurostocks'); ?></h2>
            </div>
            <div class="euro-inside">
              <p><?php esc_html_e('Select the car makes to display', 'eurostocks'); ?></p>

              <?php if(!empty($makes)){ ?>

                <p>
                  <label>
                    <input class="select_makes" type="radio" name="eurostocks_cars[select_makes]" value="0" <?php checked($select_makes, 0); ?>> <?php esc_html_e('Display all', 'eurostocks'); ?>
                  </label>
                  <label>
                    <input class="select_makes" type="radio" name="eurostocks_cars[select_makes]" value="1" <?php checked($select_makes, 1); ?>> <?php esc_html_e('Select makes', 'eurostocks'); ?>
                  </label>
                </p>
                <p class="select_models_rn <?php if($select_makes == 0){ echo "eurostocks-none";} ?>">
                  <label for="eurostocks-select-available-make"><?php esc_html_e('Select the makes to display', 'eurostocks'); ?></label>
                 
                  <select id="eurostocks-select-available-make" name="eurostocks_cars[select_available_make][]" multiple="multiple" class="euroselect">

                    <?php
                    foreach($makes as $make){
                      ?>
                      <option value="<?php echo esc_html($make['key']); ?>" <?php echo (isset($selected_makes) && in_array($make['key'], $selected_makes)?'selected':''); ?>><?php echo esc_html($make['description']); ?></option>
                      <?php
                    }
                    ?>
                  </select>
                  <br>
                   <a class="deselect-all"><?php esc_html_e('Deselect all', 'eurostocks'); ?></a>
                </p>
              <?php } else { ?>
                <p><?php esc_html_e('No makes available yet. Go to Settings and click "Update makes" to load the latest list.', 'eurostocks'); ?></p>
              <?php } ?>

              

                    <br>
              <span for="eurostocks-cars-live-models">
                    <input id="eurostocks-cars-live-models" value="1" type="checkbox" name="eurostocks_cars[live_models]" <?php checked($live_models, 1); ?>>
                    <?php esc_html_e('Request models live', 'eurostocks'); ?>
                  </span>
                  <br>
                    <br>
                  <span>
                    <p><?php esc_html_e('Select search requirements', 'eurostocks'); ?></p>

                    <label>
                      <input type="radio" name="eurostocks_cars[make_only]" value="0" <?php checked($make_only, 0); ?> />
                      <?php esc_html_e('Make and Model needed', 'eurostocks'); ?>
                    </label>
                    <br>
                    <label>
                      <input type="radio" name="eurostocks_cars[make_only]" value="1" <?php checked($make_only, 1); ?> />
                      <?php esc_html_e('Only Make needed', 'eurostocks'); ?>
                    </label>
                    <br>
                    <label>
                      <input type="radio" name="eurostocks_cars[make_only]" value="2" <?php checked($make_only, 2); ?> />
                      <?php esc_html_e('No Make or Model needed', 'eurostocks'); ?>
                    </label>
                  </span>
                  <br>

                  <br>

                  <div class="eurostocks-excluded-models">
                    <label for="eurostocks-excluded-models"><?php esc_html_e('Excluded models', 'eurostocks'); ?></label>
                    <input type="text" name="eurostocks_cars[excluded_models]" value="<?php echo esc_html($excluded_models); ?>">
                    <p><?php esc_html_e('Enter the models you want to exclude from the search results, separated by a comma.', 'eurostocks'); ?></p>
                  </div>


            <?php  submit_button(__('Save settings', 'eurostocks'), 'primary','submit', TRUE); ?>

            <br><br>
            <button class="update_modals button button-secondary"><?php esc_html_e('Force update models', 'eurostocks'); ?>
                <span class="eurostocks-spinner eurostocks-none">
                  <svg class="inner" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M495.9 166.6c3.2 8.7 .5 18.4-6.4 24.6l-43.3 39.4c1.1 8.3 1.7 16.8 1.7 25.4s-.6 17.1-1.7 25.4l43.3 39.4c6.9 6.2 9.6 15.9 6.4 24.6c-4.4 11.9-9.7 23.3-15.8 34.3l-4.7 8.1c-6.6 11-14 21.4-22.1 31.2c-5.9 7.2-15.7 9.6-24.5 6.8l-55.7-17.7c-13.4 10.3-28.2 18.9-44 25.4l-12.5 57.1c-2 9.1-9 16.3-18.2 17.8c-13.8 2.3-28 3.5-42.5 3.5s-28.7-1.2-42.5-3.5c-9.2-1.5-16.2-8.7-18.2-17.8l-12.5-57.1c-15.8-6.5-30.6-15.1-44-25.4L83.1 425.9c-8.8 2.8-18.6 .3-24.5-6.8c-8.1-9.8-15.5-20.2-22.1-31.2l-4.7-8.1c-6.1-11-11.4-22.4-15.8-34.3c-3.2-8.7-.5-18.4 6.4-24.6l43.3-39.4C64.6 273.1 64 264.6 64 256s.6-17.1 1.7-25.4L22.4 191.2c-6.9-6.2-9.6-15.9-6.4-24.6c4.4-11.9 9.7-23.3 15.8-34.3l4.7-8.1c6.6-11 14-21.4 22.1-31.2c5.9-7.2 15.7-9.6 24.5-6.8l55.7 17.7c13.4-10.3 28.2-18.9 44-25.4l12.5-57.1c2-9.1 9-16.3 18.2-17.8C227.3 1.2 241.5 0 256 0s28.7 1.2 42.5 3.5c9.2 1.5 16.2 8.7 18.2 17.8l12.5 57.1c15.8 6.5 30.6 15.1 44 25.4l55.7-17.7c8.8-2.8 18.6-.3 24.5 6.8c8.1 9.8 15.5 20.2 22.1 31.2l4.7 8.1c6.1 11 11.4 22.4 15.8 34.3zM256 336a80 80 0 1 0 0-160 80 80 0 1 0 0 160z"/></svg>
                </span>
              </button>

                <div id="eurostocks-feedback-get-models-ajax">
                </div>

          </div>
        </div>
      </div>
    </div>
  </div>
</form>
</div>
