<?php

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

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

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

  if( $debugin == 1 ){

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

  }

  if( $email && $fuente && $token ){

    //Valida Usuario Facebook
    if( $fuente == 'facebook' ){

      $Query = "
        SELECT
          usr.id
        FROM usuarios_app usr
        WHERE
          usr.email = '$email'
          AND usr.token_facebook = '$token'
      ";
      $Result = mysqli_query($mysqli, $Query);
      $User = mysqli_fetch_assoc($Result);

    }
    //End Valida Usuario Facebook

    //Valida Usuario Google
    if( $fuente == 'google' ){

      $Query = "
        SELECT
          usr.id
        FROM usuarios_app usr
        WHERE
          usr.email = '$email'
          AND usr.token_google = '$token'
      ";
      $Result = mysqli_query($mysqli, $Query);
      $User = mysqli_fetch_assoc($Result);

    }
    //End Valida Usuario Google

    if( $User[id] ){

      $Data = array(
        'action'      => 'ok',
        'id_user'     => $User[id],
        'login'      => '1'
      );
      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 = 'ua-4'
      ";
      $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);

    }

  }else{

    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' ";
      }

      $Where .= " AND usr.pass = '$pass' ";

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

      if( $User[id] ){

        $Data = array(
          'action'      => 'ok',
          'id_user'     => $User[id],
          'tipo_user'   => '1',
          'login'       => '1'
        );
        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-1'
        ";
        $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);

      }

    }

  }

?>
