<?php

namespace App\Http\Controllers\Api\Bancos;

use Illuminate\Support\Facades\DB;
use App\Danoral\Bancos\Banco;
use App\Danoral\Bancos\Ingreso;
use App\Danoral\Entidades\Entidad;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Resources\Banco as BancoResources;
use App\Http\Resources\BancoCodigos as BancoCodigosResources;
use App\Http\Resources\BancoTrans as BancoTransResources;
use App\Http\Resources\Ingreso as IngresoResources;
use App\Danoral\Entidades\SyncEntidades;

class BancoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $bancos = DB::table('admin.bancos_view')
            ->where('enableE', true)
            ->orderBy(DB::raw("FORMAT('%10s', \"codE\")::bytea"), 'asc')
            ->orderBy('codCTA', 'asc')
            ->get();

        return response()->json(BancoResources::collection($bancos), 200);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Danoral\Bancos\Banco  $banco
     * @return \Illuminate\Http\Response
     */
    public function show(Banco $banco)
    {
        $res = DB::table('admin.bancos_view')
            ->where('idBanco',$banco->idBanco)
            ->first();

        return response()->json(new BancoResources($res), 200);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Danoral\Bancos\Banco  $banco
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Banco $banco)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Danoral\Bancos\Banco  $banco
     * @return \Illuminate\Http\Response
     */
    public function destroy(Banco $banco)
    {
        //
    }

    public static function getBancos_Entidad($idE){

        if($idE < 999999999){
            $idE = SyncEntidades::cambioID_edif($idE);
        }

        $entidad = Entidad::find($idE);

        if($entidad){

            if($entidad->idESuper == 401 || (
                ($idE >= 100100020000000893 && $idE <= 100100020000000909) ||
                ($idE == 100100020000001193) ||
                ($idE == 100100020000001194)
            )){
                $idE = 401;
            }


    
            $bancos = Banco::where([
                    ['idE', $idE],
                    ['visiblePago', true],
                ])
                ->leftJoin('config.bancos_codigos', 'bancos.codBanco', '=', 'bancos_codigos.codBanco')
                ->get();
    
    
            return response()->json(BancoResources::collection($bancos),200);
    
        }

        return null;
    }

    public static function getBancosCodigos(){
        return response()->json(
            BancoCodigosResources::collection(
                DB::table('config.bancos_codigos')->get()
            ),200);
    }

    public static function getTrans(Request $request){

        $idBanco = $request->idBanco;

        $banco = Banco::find($idBanco);
        if($banco){
            $bancoT = $banco->getTrans();

            return response()->json(BancoTransResources::collection($bancoT), 200);
        }

        return response()->json($request, 400);
    }

    public function getIngresos(Request $request) {

        return response()->json(
            IngresoResources::collection(
                Ingreso::getIngresos($request->all())
            ), 
            200);
    }

    public function getFileTXT() {
        $contenido = "DAN2506000100000000000000009";

        return response($contenido, 200)
            ->header('Content-Type', 'text/plain')
            ->header('Content-Disposition', 'attachment; filename="DAN25060001.txt"');


    }
}
