<?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 KB_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"));
      $decoded64      = json_decode($content, true);
      $decoded_type   = $decoded64['type'];
      if( $decoded_type == 'base64' ){
        $decoded_    = base64_decode($decoded64['code']);
        $decoded     = json_decode($decoded_, true);
      }else{
        $decoded     = json_decode($content, true);
      }


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

      //Content
      if( $content ){

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

        //Productos
        if( $tipo == 'productos' ){

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

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

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

          //Delete
          if( $accion == 'delete' ){
            require_once('productos_delete.php');
          }
          //End Delete

        }
        //End Productos

        //Ventas
        if( $tipo == 'ventas' ){

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

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

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

          //Delete
          if( $accion == 'delete' ){
            require_once('ventas_delete.php');
          }
          //End Delete

        }
        //End Ventas

      }
      //End Content


    }
    //End Cuenta Validar

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

  }

?>
