<?php
namespace Incoviba\Controller\Ventas;

use DateTimeImmutable;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Alias\View;
use Incoviba\Service;

class Pagos
{
    public function pendientes(ServerRequestInterface $request, ResponseInterface $response, View $view): ResponseInterface
    {
        return $view->render($response, 'ventas.pagos.pendientes');
    }
    public function depositar(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService): ResponseInterface
    {
        $body = $request->getBody();
        $json = json_decode($body->getContents());
        $date = new DateTimeImmutable($json->fecha);
        $response->getBody()->write(json_encode(['fecha' => $date->format('d-m-Y'), 'message' => 'Not implemented']));
        return $response->withHeader('Content-Type', 'application/json');
    }
    public function abonar(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService): ResponseInterface
    {
        $body = $request->getBody();
        $json = json_decode($body->getContents());
        $date = new DateTimeImmutable($json->fecha);
        $response->getBody()->write(json_encode(['fecha' => $date->format('d-m-Y'), 'message' => 'Not implemented']));
        return $response->withHeader('Content-Type', 'application/json');
    }
    public function para_pendientes(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService): ResponseInterface
    {
        $pagos = $pagoService->getPendientes();
        $pagos_pendientes = [];
        foreach ($pagos as $pago) {
            $pagos_pendientes []= [
                'id' => $pago->id
            ];
        }
        $response->getBody()->write(json_encode(['pagos' => $pagos_pendientes, 'total' => count($pagos_pendientes)]));
        return $response->withHeader('Content-Type', 'application/json');
    }
    public function para_abonar(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService): ResponseInterface
    {
        $pagos = $pagoService->getDepositados();
        $pagos_depositados = [];
        foreach ($pagos as $pago) {
            $pagos_depositados []= [
                'id' => $pago->id
            ];
        }
        $response->getBody()->write(json_encode(['pagos' => $pagos_depositados, 'total' => count($pagos_depositados)]));
        return $response->withHeader('Content-Type', 'application/json');
    }
    public function rebotes(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService): ResponseInterface
    {
        $pagos = $pagoService->getRebotes();
        $rebotes = [];
        foreach ($pagos as $pago) {
            $rebotes []= [
                'id' => $pago->id
            ];
        }
        $response->getBody()->write(json_encode(['pagos' => $rebotes, 'total' => count($rebotes)]));
        return $response->withHeader('Content-Type', 'application/json');
    }
}
