<?php

use \Slim\Http\Request as Request;
use \Slim\Http\Response as Response;
use App\Model\Articulo;
use App\Model\Stock;
use App\Model\Presentacion;

$app->get('/articulos[/{id}]', function (Request $request, Response $response, $args) {
    if (isset($args['id'])){
        $result = Articulo::find($args['id']);
        $response->getBody()->write(Articulo::toJSON($result));
        return $response;
    }else{
        $objects = null;
        $update = $request->getHeader("update-art")[0];
        $updateStock = $request->getHeader("update-stk")[0];
        if($update=="none")
            $objects = Articulo::all();
        else{
            $wh = "stk_articu = art_codigo"; 
            if ($updateStock !== "none") $wh = $wh." and stk_update > '".$updateStock."'";
            $objects = Articulo::all(
                array("conditions" => "art_update > '".$update."' OR
                    art_codigo in (SELECT stk_articu FROM stock WHERE ".$wh.") ")
            );
        }
        $json = ($updateStock=="none") ? Articulo::toJSON($objects) : Articulo::toJSON($objects, $updateStock);
        $response->getBody()->write($json);
        $response;
        return $response
                    ->withAddedHeader('update-stk', date('Y-m-d '.'00:00:00'))
                    ->withAddedHeader('update-art', date('Y-m-d '.'00:00:00'));
    }
});

$app->get('/articulos/limit/desde={desde}&limit={limit}', function (Request $request, Response $response, $args) {
    $objects = null;
    $update = $request->getHeader("update-art")[0];
    $updateStock = $request->getHeader("update-stk")[0];
    if($update=="none")
        $objects = Articulo::all(
            array('limit' => $args['limit'], 'offset' => $args['desde'])
        );
    else{
        $wh = "stk_articu = art_codigo"; 
        if ($updateStock !== "none") $wh = $wh." and stk_update > '".$updateStock."'";
        $objects = Articulo::all(
            array("conditions" => "art_update > '".$update."' OR
                art_codigo in (SELECT stk_articu FROM stock WHERE ".$wh.") ",
                'limit' => $args['limit'], 'offset' => $args['desde'])
        );
    }
    $json = ($updateStock=="none") ? Articulo::toJSON($objects) : Articulo::toJSON($objects, $updateStock);
    $response->getBody()->write($json);
    $response;
    return $response
                ->withAddedHeader('update-stk', date('Y-m-d '.'00:00:00'))
                ->withAddedHeader('update-art', date('Y-m-d '.'00:00:00'));
});

$app->post('/articulos', function (Request $request,  Response $response, $args) {
    $att = json_decode($request->getBody());
    $object = new Articulo();
    $object->cargar($att);
    $object->save();
    $objects = $object->all();
    $response->getBody()->write(Articulo::toJSON($objects));
    return $response;
});

$app->put('/articulos', function (Request $request,  Response $response, $args) {
    $att = json_decode($request->getBody());
    $object = Articulo::find($att->id);
    $object->cargar($att);
    $object->save();
    $objects = $object->all();
    $response->getBody()->write(Articulo::toJSON($objects));
    return $response;
});

$app->delete('/articulos/{id}', function (Request $request,  Response $response, $args) {
    Articulo::find($args['id'])->delete();
    $objects = Articulo::all();
    $response->getBody()->write(Articulo::toJSON($objects));
    return $response;
});
