<?php

  if( $Debug == 1 ){
    echo "Validate User Tendero";
  }

  $debugin    = $decoded['debug'];
  $id         = $decoded['id'];
  $email      = $decoded['email'];
  $cel        = $decoded['cel'];

  $FechaHoy       = date('Y-m-d H:i:s');

  if( $debugin == 1 ){

    echo "Debug-in\n";
    echo $id."\n";
    echo $cel."\n";

  }

  if( $id || $cel || $email ){

    $Where = " usr.id != '0' ";

    if( $id ){
      $Where .= " AND usr.id = '$id' ";
    }
    if( $email ){
      $Where .= " AND usr.email = '$email' ";
    }
    if( $cel ){
      $Where .= " AND usr.celular = '$cel' ";
    }

    //Valida Usuario
    $Query = "
      SELECT
        usr.id,
        usr.email,
        usr.codigo
      FROM usuarios_app usr
      WHERE
        $Where
    ";
    $Result = mysqli_query($mysqli, $Query);
    $User = mysqli_fetch_assoc($Result);

    if( $User[id] ){

      //Actualiza codigo
      $codigo = randomCode(6);

      $Query = "
        UPDATE usuarios_app SET
          codigo = '$codigo'
        WHERE
          id = '$User[id]'
      ";
      $Result = mysqli_query($mysqli, $Query);

      //Envio de Codigo a Correo
      $EmailTitulo      = "Cambio de contraseña SwitchPoint App";
      $EmailTo          = $User[email];
      $EmailMensaje     = "Solisitaste cambio de contraseña en SwitchPoint App<br><br>Código: ".$codigo."<br><br>Si no fue así, omite este correo.";
      $SendEmailDebug   = "0";

      if( !$UrlPago && !$UrlTracker ){
        $DatosEmail = [
          'enviar'     => 'Ok',
          'titulo'     => $EmailTitulo,
          'mail'       => $EmailTo,
          'mensaje'    => $EmailMensaje,
          'debug'      => $SendEmailDebug
        ];
      }

      $UrlPost = $Url."/email-sender.php";
      if( $Debug == 1 ){
        echo "\n";
        echo $UrlPost;
        echo "\n";
      }

      $curl = curl_init();

      curl_setopt_array($curl, array(
        CURLOPT_URL => $UrlPost,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => $DatosEmail
      ));

      $response = curl_exec($curl);
      $err = curl_error($curl);
      curl_close($curl);

      if( $Debug == 1 ){
        echo "\n--Email Sended--\n";
        echo $response;
      }
      //End Envio de Codigo a Correo

      if( $response == 'Ok' ){
        $EmailSened = 'ok';
      }else{
        $EmailSened = 'error';
      }

      $Data = array(
        'action'        => 'ok',
        'id_user'       => $User[id],
        'codigo'        => $codigo,
        'email_sended'  => $EmailSened
      );
      echo json_encode($Data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);

    }else{

      //Error uv-1 ( El Usuario no se encontró. )
      $Query = "
        SELECT
          err.Id, err.error, err.msn
        FROM api_errores err
        WHERE err.error = 'up-2'
      ";
      $Result = mysqli_query($mysqli, $Query);
      $Error = mysqli_fetch_assoc($Result);

      $Data = array(
        'action'      => 'error',
        'error'       => $Error[error],
        'error_msn'   => $Error[msn]
      );
      echo json_encode($Data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);

    }

  }

?>
