<?php

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

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

  $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) && $codigo ){

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

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

    if( $codigo ){
      $Where .= "
        AND usr.codigo = '$codigo'
      ";
    }


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

    if( $User[id] ){

      //Update Pass
      $Query = "
        UPDATE usuarios_app
        SET
          pass = '$pass'
        WHERE Id = '$User[id]'
      ";
      $Result = mysqli_query($mysqli, $Query);
      //End Actualiza Tienda

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

    }

  }

?>
