<?php
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);

// define('WP_DEBUG_LOG', true);
// define('SCRIPT_DEBUG', true);
// define('SAVEQUERIES', true);


try {

  //include_once("../../wp-config.php");
  //include_once("../../wp-includes/wp-db.php");
  include_once("find.php");
  include_once("functions.php");

  $the_json = json_decode(file_get_contents('php://input'), true);


  $wp_c_g_token = "";
  if (isset($the_json['wp_c_g_token'])) {
    $wp_c_g_token = $the_json['wp_c_g_token'];
  }

  $count_tokens = isActiveToken($the_json);

  //$count_tokens=1;


  $demarche = '';
  $isSIV  = '';
  $cp = '';
  $plaque = '';
  $reception = '';
  $energie = '';
  $cv = '';
  $co2 = '';
  $mom_kg = '';
  $first_immat = '';
  $marque = '';
  $dep = '';
  $genre = '';
  $handicape = '';
  $neuf_occasion = '';
  $host = '';



  if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    //demarche
    if (isset($the_json["demarche"]) && $the_json["demarche"] !== "false") {

      $demarche = $the_json["demarche"];
    } else {

      returnError("Selectionez votre demarche !");
    }

    //departement

    if (isset($the_json["dep"]) && $the_json["dep"] !== "false") {
      $dep = $the_json["dep"];
    } else {
      returnError("Selectionez votre departement !");
    }

    if (isset($the_json["genre"])) {
      $genre = $the_json["genre"];
    }
    if (isset($the_json["reception"])) {
      $reception = $the_json["reception"];
    }
    if (isset($the_json["first_immat"])) {
      $first_immat = $the_json["first_immat"];
    }
    if (isset($the_json["plaque"])) {
      $plaque = $the_json["plaque"];
    }
    if (isset($the_json["handicape"])) {
      $handicape = $the_json["handicape"];
    }
    if (isset($the_json["isSIV"])) {
      $isSIV = $the_json["isSIV"];
    }
    if (isset($the_json["energie"])) {
      $energie = $the_json["energie"];
    }
    if (isset($the_json["cv"])) {
      $cv = $the_json["cv"];
    }
    if (isset($the_json["co2"])) {
      $co2 = $the_json["co2"];
    }
    if (isset($the_json["mom_kg"])) {
      $mom_kg = $the_json["mom_kg"];
    }
    if (isset($the_json["marque"])) {
      $marque = $the_json["marque"];
    }
    if (isset($the_json["neuf_occasion"])) {
      $neuf_occasion = $the_json["neuf_occasion"];
    }

    if (isset($the_json["host"])) {

      $the_json["host"] = str_replace("http://", "", $the_json["host"]);
      $the_json["host"] = str_replace("https://", "", $the_json["host"]);
      $the_json["host"] = str_replace("/", "", $the_json["host"]);
      $the_json["host"] = str_replace("www.", "", $the_json["host"]);

      $host = $the_json["host"];
    }
    if (isset($the_json["no_plaque"])) {
      $no_plaque = $the_json["no_plaque"];
    }

    if ($count_tokens == 0) {

      // Return error by function
      returnError("Oups! Votre token n 'est pas valide, veuillez vérifier votre clé et réessayer si vous avez un problème contactez contact@mh-data.com ou consultez la page de contact sur www.MH-Data.com");
    } else {


      $insData = array(
        'token' => $wp_c_g_token,
        'plaque' => $plaque,
        'no_plaque' => $no_plaque,
        'host' =>  $host,
        'date' => date('Y-m-d H:i:s')
      );

      // tu_use_api

      $tu_use_api = 0;
      if ($host == "") {
        $tu_use_api = 1;
      }
      setHistToken($wp_c_g_token, $insData, $tu_use_api);



      // GET THE COST AND RETURN JSON

      $result  = getCost($mom_kg, $demarche, $cp, $plaque, $reception, $energie, $cv, $co2, $first_immat, $marque, $dep, $genre, $handicape, $neuf_occasion, $host, $no_plaque);
      header('Content-Type: application/json; charset=utf-8');
      echo json_encode($result);
    }
  }
} catch (Exception $e) {
  var_dump($e);
  returnError("Oups! catching error : " . $e->getMessage());
}
