<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use DB;

class FileUploadController extends Controller
{
    //
    public function UploadFile(Request $request) {
        //Move Uploaded File to public folder
            $destinationPath = 'uploaded_files';
            if($request->hasFile('FILE')) {
               $myFile = $request->file('FILE')->getClientOriginalName();
               $path = $request->file('FILE')->move(public_path($destinationPath), $myFile);
               $response_data = [
                'status' => 'true',
                'response' => 'File Uploaded Successfully!',
               ];
               return $response_data;
            }
            else
            {
                $response_data = [
                    'status' => 'false',
                    'response' => 'Oops something went wrong!',
                ];
                return $response_data;
            }
    }

    public function downloadFile(Request $request)
    {
        //PDF file is stored under project/public/download/info.pdf
        $filename = $request -> FILE_NAME;
        $headers = [
            'Content-Type' => 'application/pdf',
        ];
        if($filename != "" || null){
            $file= public_path(). '/uploaded_files/'.$filename;
            return response()->download($file, 'filename.pdf', $headers);
        }
        $response_data = [
            'status' => 'false',
            'response' => 'Oops Something Went Wrong!',
        ];
        return $response_data;
    }

    //api-get-download-word-file
    public function DownloadWordFile(Request $request)
    {
        //PDF file is stored under project/public/download/info.pdf
        $filename = $request -> FILE_NAME;
        $headers = [
            'Content-Type' => 'application/msword',
        ];
        if($filename != "" || null){
            $file= public_path(). '/uploaded_files/'.$filename;
            return response()->download($file, 'filename.pdf', $headers);
        }
        $response_data = [
            'status' => 'false',
            'response' => 'Oops Something Went Wrong!',
        ];
        return $response_data;
    }

    public function uploadMultipleFiles(Request $request)
    {
        // Validate the request
        $request->validate([
            'files.*' => 'required|mimes:jpeg,png,pdf|max:2048',
        ]);

        $destinationPath = 'uploaded_files';
        foreach ($request->file('files') as $file) {
            $myFile = $file->getClientOriginalName();
            $path = $file->move(public_path($destinationPath), $myFile);
        }
 
        $response_data = [
            'status' => 'true',
            'response' => 'File Uploaded Successfully!',
        ];
        return $response_data;
    }

    public function downloadFileWeb(Request $request)
    {
        //PDF file is stored under project/public/download/info.pdf
        $filename = $request -> FILE_NAME;
        $headers = [
            'Content-Type' => 'application/pdf',
        ];
        if($filename != "" || null){
            $file= public_path(). '/uploaded_files/'.$filename;
            return response()->download($file, 'filename.pdf', $headers);
        }
        $response_data = [
            'status' => 'false',
            'response' => 'Oops Something Went Wrong!',
        ];
        return $response_data;
    }

    public function UploadFileWeb(Request $request) {
        $request->validate([
            'FILE' => 'required|max:102400',
        ]);
        //Move Uploaded File to public folder
            $destinationPath = 'uploaded_files';
            if($request->hasFile('FILE')) {
               $myFile = $request->file('FILE')->getClientOriginalName();
               $path = $request->file('FILE')->move(public_path($destinationPath), $myFile);
               $response_data = [
                'status' => 'true',
                'response' => 'File Uploaded Successfully!',
               ];
               return $response_data;
            }
            else
            {
                $response_data = [
                    'status' => 'false',
                    'response' => 'Oops something went wrong!',
                ];
                return $response_data;
            }
    }

    
}
