<?php

namespace App\Http\Controllers\Api\Cobranzas;

use Illuminate\Support\Facades\DB;

use App\Danoral\Cobranzas\Cobros\CobrosLista;
use App\Http\Controllers\Controller;
use App\Http\Resources\CobrosListaView as CobrosListaViewResources;
use App\Http\Resources\CobrosLista as CobrosListaResources;
use Illuminate\Http\Request;

class CobrosListaController extends Controller
{
    public function __construct() {
        date_default_timezone_set('America/Caracas');
    }

    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $cobrosLista = new CobrosLista;
        $cobrosLista->dateCobrosLista = date("Y-m-d H:i:s");
        $cobrosLista->idEUser = $request->user()->idE;
        if(!is_null($request->typeCobrosLista))    { $cobrosLista->typeCobrosLista = $request->typeCobrosLista;}
        if(!is_null($request->dateCobro))          { $cobrosLista->dateCobro = date("Y-m-d",strtotime($request->dateCobro));}         

        $cobrosLista->save();

        return response()->json(
            new CobrosListaResources($cobrosLista), 
            200);
        
    }

    /**
     * Display the specified resource.
     */
    public function show(CobrosLista $cobrosLista)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, CobrosLista $cobrosLista)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(CobrosLista $cobrosLista)
    {
        //
    }

    public function getCobrosListas(Request $request) {

        $cobrosListasv = CobrosLista::getCobrosListasView();

        return response()->json(
            CobrosListaViewResources::collection($cobrosListasv),
            200
        );
    }

    public function getCobrosListaTypes() {

        return response()->json(
            DB::table('config.cobros_lista_type')->get(), 
            200);

    }



}
