<?php

  if( $Debug == 1 ){
    echo "View Tienda";
  }

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

  $debugin        = $decoded['debug'];
  $usr_id         = $decoded['usr_id'];
  $tie_id         = $decoded['tienda_id'];
  $usr_clave      = $decoded['usr_clave'];
  $nombre         = $decoded['nombre'];
  $calle          = $decoded['calle'];
  $num_ext        = $decoded['num_ext'];
  $num_int        = $decoded['num_int'];
  $colonia        = $decoded['colonia'];
  $municipio      = $decoded['municipio'];
  $estado         = $decoded['estado'];
  $pais           = $decoded['pais'];
  $codigopostal   = $decoded['codigopostal'];
  $lat            = $decoded['latitud'];
  $lng            = $decoded['longitud'];

  if( $debugin == 1 ){

    echo "Debug-in\n";
    echo $usr_id."\n";
    echo $tie_id."\n";
    echo $nombre."\n";
    echo $calle."\n";
    echo $num_ext."\n";
    echo $num_int."\n";
    echo $colonia."\n";
    echo $municipio."\n";
    echo $estado."\n";
    echo $pais."\n";
    echo $codigopostal."\n";
    echo $lat."\n";
    echo $lng."\n";

  }

  $Where = "
    WHERE tie.Id != '0'
  ";

  //Id Usuario
  if( $usr_id ){
    $Where .= "
      AND usr.fk_usuario = '$usr_id'
    ";
  }
  //End Id Usuario

  //Id Tienda
  if( $tie_id ){
    $Where .= "
      AND tie.id = '$tie_id'
    ";
  }
  //End Id Tienda

  //Nombre
  if( $nombre ){
    $Where .= "
      AND tie.nombre LIKE '$nombre%'
    ";
  }
  //End Nombre

  //Calle
  if( $calle ){
    $Where .= "
      AND tie.calle LIKE '$calle%'
    ";
  }
  //End Calle

  //Numero Exterior
  if( $num_ext ){
    $Where .= "
      AND tie.no_exterior = '$num_ext'
    ";
  }
  //End Numero Exterior

  //Numero Interior
  if( $num_int ){
    $Where .= "
      AND tie.no_interior = '$num_int'
    ";
  }
  //End Numero Interior

  //Colonia
  if( $colonia ){
    $Where .= "
      AND tie.colonia LIKE '$colonia%'
    ";
  }
  //End Colonia

  //Codigo Postal
  if( $codigopostal ){
    $Where .= "
      AND tie.codigo_postal = '$codigopostal'
    ";
  }
  //End Codigo Postal

  //Municipio
  if( $municipio ){
    $Where .= "
      AND tie.fk_municipio = '$municipio'
    ";
  }
  //End Municipio

  //Estado
  if( $estado ){
    $Where .= "
      AND tie.fk_estado = '$estado'
    ";
  }
  //End Estado

  //Pais
  if( $pais ){
    $Where .= "
      AND tie.fk_pais = '$pais'
    ";
  }
  //End Pais

  //Tiendaa
  $Query = "
    SELECT
      tie.id,
      tie.nombre,
      tie.fk_pais, tie.fk_estado, tie.fk_municipio,
      tie.codigo_postal, tie.colonia,
      tie.calle, tie.no_exterior, tie.no_interior,
      tie.lat, tie.lng,
      usr.fk_usuario,
      est.estado,
      mun.municipio
    FROM tiendas tie
    LEFT JOIN cat_estados est ON est.id = tie.fk_estado
    LEFT JOIN cat_municipios mun ON mun.id = tie.fk_municipio
    LEFT JOIN usuarios_tiendas usr ON usr.fk_tienda = tie.id
    $Where
    GROUP BY tie.id
  ";
  $Ties = $mysqli->query($Query);
  mysqli_data_seek($Ties, 0);
  if( $Ties->num_rows >= 1 ){
    while($Tie = mysqli_fetch_array($Ties)) {

      $Tiendas[] = array(
        'id'            => $Tie[id],
        'usr_id'        => $Tie[fk_usuario],
        'nombre'        => $Tie[nombre],
        'calle'         => $Tie[calle],
        'num_ext'       => $Tie[no_exterior],
        'num_int'       => $Tie[no_interior],
        'colonia'       => $Tie[colonia],
        'municipio_id'  => $Tie[fk_municipio],
        'municipio'     => $Tie[municipio],
        'estado_id'     => $Tie[fk_estado],
        'estado'        => $Tie[estado],
        'pais'          => $Tie[fk_pais],
        'codigopostal'  => $Tie[codigo_postal],
        'latitud'       => $Tie[lat],
        'longitud'      => $Tie[lng]
      );

    }

    $Data = array(
      'tiendas'   => $Tiendas
    );
    echo json_encode($Data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);

  }else{

    //Error
    $Query = "
      SELECT
        err.Id, err.error, err.msn
      FROM api_errores err
      WHERE err.error = 'ta-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);

  }

?>
