<?php

namespace App\Http\Controllers;

use App\Radicado;
use App\Adjunto;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\File;

class AdjuntosController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function Upload($guid, Request $request)
    {

        if (isset($_FILES['adjuntos'])) {

            foreach ($request->file('adjuntos') as $f) {

                //die( public_path() . '/tmpDocs/' . $guid . '/');

                $f->move(
                    public_path() . '/tmpDocs/' . $guid . '/', $f->getClientOriginalName()
                );

                return '';

            }

        }

        return true;
    }

    public function RemoveTmp(Request $request)
    {

        try {
            $path = public_path() . '/tmpDocs/' . $request->guid . '/' . $request->file;

            \unlink($path);
            return (new Response('true', 200));
        } catch (\Exception $e) {
            return $e->getMessage();

        }
        // $PathFile = $request->PathFile;
        // $path = public_path() . '/tmpDocs/' . $PathFile;

        // return response()->json(\unlink($path));
    }

    public function RemoveFromRadicado(Request $request)
    {

       
        try{
            $path = public_path() . '/../../public_html' . '/uploads/' . $request->guid . '/' . $request->file;
            $rad = Radicado::Find($request->idRadicado);

            $adj = Adjunto::Find($request->idAdjunto);

            $rad->adjuntos()->detach($adj->id);
            $adj->delete();


            \unlink($path);
            return (new Response('true', 200));
        } catch (\Exception $e) {
            return $e->getMessage();

        }
    }

}