<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\FileUpload;
use File;

class FileUploadController extends Controller
{
    
    public function index()
    {
        $data['title'] = "Upload File";
        $data['all'] = FileUpload::all();
        return view('admin.file_upload',$data);
    }

    public function create()
    {
        //
    }


    public function store(Request $req)
    {
        $file = $req->file('file');
        $file_name = $req->name;
        $details = $req->details;

        if ($req->hasFile('file')) {
            $extension = $file->getClientOriginalExtension();
            $os = array("zip", "rar", "pdf", "doc", "docx", "txt");
            if (in_array($extension, $os)){
                $name = $file->getClientOriginalName();
                $newFileName = $newFileName = "file_".date('d_m_y_h_m_s').".".$extension;
                $fileee = $file->move('assets/document/files/', $newFileName);

                $file_name = empty($file_name)?$name:$file_name;
               
                $cat = new FileUpload; 
                $cat->url = $newFileName;
                $cat->details = $details;
                $cat->name = $file_name;
                $cat->save();       
            }
        }

        return redirect()->back();
    }

    
    public function show($id) //use as delete
    {
        $del = FileUpload::find($id);

        if (File::exists('assets/document/files/'.$del->url) && !empty($del->url))
            {
                File::delete('assets/document/files/'.$del->url);
                $del->delete();
            }

        return redirect()->back();
    }

  
    public function edit($id)  //use for download
    {
        
    }

    
    public function update(Request $request, $id)
    {
        //
    }

    
    public function destroy($id)
    {
        //
    }
}
