<?php

  // error_reporting(E_ALL);
  // ini_set('display_errors', 1);
  error_reporting(0);
  $Debug    = 0;

  if( $Debug == 1 ){
    echo "Debug\n";
  }

  //echo "Produccion -";

  //Variables Acceso
  $Usr = $_SERVER['PHP_AUTH_USER'];
  $Key = $_SERVER['PHP_AUTH_PW'];
  //$Key = md5($Key);

  //Si no recibe como Header
  if( $Usr && $Key ){
  }else{
    $Auth = $_SERVER['HTTP_AUTHORIZATION'];
    $Auth = base64_decode($Auth);
    $Arr = explode(':', $Auth);
    $Usr = $Arr[0];
    $Key = $Arr[1];
  }

  if( $Debug == 1 ){
    echo $Usr;
    echo $Key;
  }

  if( $Usr && $Key ){

    //Conection
    $Position   = '';
    require_once($Position.'config/config.php');
    require_once($Position.'config/conexion.php');
    require_once($Position.'funciones/funciones.php');

    //Acceso Validar
    $Query = "
      SELECT
        usr.Id
      FROM api_conexion usr
      WHERE usr.user = '$Usr'
        AND usr.pass = '$Key'
    ";
    $Result = mysqli_query($mysqli, $Query);
	  $Conexion = mysqli_fetch_assoc($Result);
    if( $Conexion[Id] ){

      $Cuenta = $Conexion[Id];

      //Varaibles
      $content = trim(file_get_contents("php://input"));
      //$decoded = json_decode($content, true);
      //var_dump($decoded);

      //Varaibles
      $content = trim(file_get_contents("php://input"));
      $decoded = json_decode($content, true);

      if( $Debug == 1 ){
        var_dump($decoded);
      }

      //Content
      if( $content ){

        echo $code              = $decoded['code'];

        //echo "Action: ";
        $accion            = $decoded['accion'];
        $tipo              = $decoded['tipo'];

        //Tipos
        //1- Usuarios Tenderos
        //2- Usuarios Fuerza de Ventas
        //3- Tiendas

        //Acciones
        //1- Add
        //2- Edit
        //3- View
        //4- Delete
        //5- Validate
        //6- Change Password

        //Usuarios
        if( $tipo == 1 ){

          //Add
          if( $accion == 1 ){
            // require_once('usuarios_add.php');
          }
          //End Add

          //Edit
          if( $accion == 2 ){
            // require_once('usuarios_edit.php');
          }
          //End Edit

          //View
          if( $accion == 3 ){
            // require_once('usuarios_view.php');
          }
          //End View

          //Delete
          if( $Acciaccionon == 4 ){
            // require_once('usuarios_delete.php');
          }
          //End Delete

          //Validate
          if( $accion == 5 ){
            // require_once('usuarios_validate.php');
          }
          //End Validate

          //Validate Password
          if( $accion == 6 ){
            // require_once('usuarios_validate_password.php');
          }
          //End Validate Password

          //Change Password
          if( $accion == 7 ){
            // require_once('usuarios_change_password.php');
          }
          //End Change Password

        }
        //End Usuarios

        //Tiendas
        if( $tipo == 3 ){

          //Add
          if( $accion == 1 ){
            // require_once('tiendas_add.php');
          }
          //End Add

          //Add
          if( $accion == 2 ){
            // require_once('tiendas_edit.php');
          }
          //End Add

        }
        //End Tiendas

      //Guarda Log de Acciones
      // $Fecha     = date('Y-m-d');
      // $FechaHora = date('Y-m-d H:i:s');
      // $Query = "
      //   INSERT INTO api_logp_eticiones (
      //     fecha, fk_api_conexion
      //     petition
      //   )VALUES(
      //     '$Fecha', '$FechaHora', '$User[Id]', '$Conexion[Id]',
      //     '$Acc', '$Tip', '$Acc', '$Conexion[Url]', '$Conexion[Code]',
      //     '$Conexion[CKey]', '$Conexion[CSecret]',
      //     '$content'
      //   )
      // ";
      //$Result = mysqli_query($mysqli, $Query);
      //$IdLog = mysqli_insert_id($mysqli);

    }
    //End Content


    }
    //End Cuenta Validar

    require_once($Position.'config/cerrarconexion.php');

  }

?>
