Index: app/Http/Controllers/Dashboard/DocumentsController.php
===================================================================
--- app/Http/Controllers/Dashboard/DocumentsController.php	(revision b9c4a92282ce0d7aa095082d7109f6d3d7b68dad)
+++ app/Http/Controllers/Dashboard/DocumentsController.php	(revision ea7b12a15d1afa472a54d5b8c75c55917907862e)
@@ -5,9 +5,13 @@
 use App\Helpers\Alert;
 use App\Http\Requests\Dashboard\DocumentRequest;
+use App\Http\Requests\Dashboard\FileRequest;
+use App\Http\Requests\Dashboard\PasswordSettingsRequest;
 use App\Models\Department;
 use App\Models\Document;
 use App\Models\File;
 use App\Services\UploadService;
+use Carbon\Carbon;
 use Illuminate\Http\Request;
+use Illuminate\Http\Response;
 use Illuminate\Support\Facades\Storage;
 use App\Http\Controllers\Controller;
@@ -115,5 +119,5 @@
         $document->description = $request->description;
 
-        $location = DIRECTORY_SEPARATOR . 'Departments' . DIRECTORY_SEPARATOR . $document->department->name . DIRECTORY_SEPARATOR . $request->name;
+        $location = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $request->name;
 
         if (!Storage::disk('local')->has($location)) {
@@ -121,13 +125,16 @@
         }
 
-        foreach ($request->file_item as $file) {
-            $file->storeAs($location . DIRECTORY_SEPARATOR, $file->getClientOriginalName());
-            $newFile = new File();
-            $newFile->link = $location . DIRECTORY_SEPARATOR . $file->getClientOriginalName();
-            $newFile->document()->associate($document);
-            $newFile->save();
-        }
-
         $document->save();
+
+        if ($request->has('file_item')) {
+            foreach ($request->file_item as $file) {
+                $file->storeAs($location . DIRECTORY_SEPARATOR, $file->getClientOriginalName());
+                $newFile = new File();
+                $newFile->name = $file->getClientOriginalName();
+                $newFile->location = $location . DIRECTORY_SEPARATOR . $file->getClientOriginalName();
+                $newFile->document()->associate($document);
+                $newFile->save();
+            }
+        }
 
         Alert::flash("New document created successfully");
@@ -140,5 +147,6 @@
         return view("dashboard.documents.edit")->with([
             "document" => Document::findOrFail($id),
-            "departments" => Department::all()
+            "departments" => Department::all(),
+            "files" => File::where('document_id', $id)->get()
         ]);
     }
@@ -147,8 +155,11 @@
     {
         $document = Document::findOrFail($id);
+        $files = File::where('document_id', $id)->get();
 
         $department = Department::find($request->department);
 
         $document->department()->associate($department);
+
+        $oldLocation = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $document->name;
 
         $document->name = $request->name;
@@ -156,39 +167,51 @@
         $document->description = $request->description;
 
-        $oldLocation = DIRECTORY_SEPARATOR . 'Departments' . DIRECTORY_SEPARATOR . $document->department->name . DIRECTORY_SEPARATOR . $document->name;
-        $location = DIRECTORY_SEPARATOR . 'Departments' . DIRECTORY_SEPARATOR . $document->department->name . DIRECTORY_SEPARATOR . $request->name;
+        $location = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $request->name;
 
         if ($document->isDirty('name')) {
             if (!Storage::disk('local')->has($location)) {
                 Storage::disk('local')->move($oldLocation, $location);
-            }
-        }
-
-        $hasFileError = false;
+                foreach($files as $file){
+                    $file->location = "test";
+                }
+            }
+        }
+
         if ($request->has('file_item')) {
             foreach ($request->file_item as $file) {
                 $fileName = $file->getClientOriginalName();
-                if(Storage::disk('local')->has($location . DIRECTORY_SEPARATOR . $fileName)) {
-                    $hasFileError = true;
-                    break;
+                if (Storage::disk('local')->has($location . DIRECTORY_SEPARATOR . $fileName)) {
+                    // $hasFileError = true;
+                    // break;
+                    $NewFileName = time() . $fileName;
+                    $file->storeAs($location . DIRECTORY_SEPARATOR, $NewFileName);
+                    $newFile = new File();
+                    $newFile->name = rand() . $fileName;
+                    $newFile->location = $location . DIRECTORY_SEPARATOR . $NewFileName;
+                    $newFile->document()->associate($document);
+                    $newFile->save();
+                } else {
+                    $file->storeAs($location . DIRECTORY_SEPARATOR, $fileName);
+                    $newFile = new File();
+                    $newFile->name = $fileName;
+                    $newFile->location = $location . DIRECTORY_SEPARATOR . $fileName;
+                    $newFile->document()->associate($document);
+                    $newFile->save();
                 }
-                $file->storeAs($location . DIRECTORY_SEPARATOR, $fileName);
-                $newFile = new File();
-                $newFile->link = $location . DIRECTORY_SEPARATOR . $fileName;
-                $newFile->document()->associate($document);
-                $newFile->save();
-            }
-        }
-
-        if($hasFileError) {
-            Alert::flash('Document with the same name exists', 'error');
-            return redirect()->route("dashboard.documents.edit", ['id' => $document->id]);
+            }
         }
 
         $document->save();
 
+//        if($hasFileError) {
+//            Alert::flash('Document with the same name exists', 'error');
+//            return redirect()->route("dashboard.documents.edit", ['id' => $document->id]);
+//        }
+
+
+
         Alert::flash("Document edited successfully");
 
-        return redirect()->route("dashboard.documents.index");
+        return redirect()->back();
     }
 
@@ -230,9 +253,57 @@
     {
         $document = Document::find($id);
+        $files = File::where('document_id', $id)->get();
         if (auth()->user()->hasPermission("delete_all_documents")) {
+           // @dd($files);
+            foreach ($files as $file) {
+                $file->delete();
+            }
             $document->delete();
+            $location = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $document->name;
+            Storage::disk('local')->deleteDirectory($location);
             Alert::flash($document->name . " deleted successfully");
         }
         return redirect()->route("dashboard.documents.index");
     }
+
+    public function deleteFile($id)
+    {
+        $file = File::find($id);
+        $file->delete();
+        Storage::disk('local')->delete($file->location);
+
+        Alert::flash($file->name . " deleted successfully");
+
+        return redirect()->back();
+    }
+
+    public function downloadFile($id)
+    {
+        $file = File::find($id);
+        return Storage::download($file->location);
+    }
+
+    public function renameFile(FileRequest $request, $id)
+    {
+        $file = File::find($id);
+        $fileExtension = explode('.', $file->name)[1];
+
+        $file->name = $request->name . '.' . $fileExtension;
+        $newLocation = 'Departments' . DIRECTORY_SEPARATOR . explode(DIRECTORY_SEPARATOR, $file->location)[1] . DIRECTORY_SEPARATOR . explode(DIRECTORY_SEPARATOR, $file->location)[2] . DIRECTORY_SEPARATOR . $file->name;
+
+        if(Storage::disk('local')->has($newLocation)) {
+            Alert::flash("A file with the same name already exists", "error");
+            return redirect()->back();
+        }
+        else {
+            Storage::disk('local')->copy($file->location, $newLocation);
+            Storage::disk('local')->delete($file->location);
+
+        $file->location = $newLocation;
+        $file->save();
+
+        Alert::flash($file->name . " updated successfully");
+        return redirect()->back();
+        }
+    }
 }
Index: app/Http/Requests/Dashboard/DocumentRequest.php
===================================================================
--- app/Http/Requests/Dashboard/DocumentRequest.php	(revision b9c4a92282ce0d7aa095082d7109f6d3d7b68dad)
+++ app/Http/Requests/Dashboard/DocumentRequest.php	(revision ea7b12a15d1afa472a54d5b8c75c55917907862e)
@@ -5,5 +5,4 @@
 use App\Models\Department;
 use App\Models\Document;
-use App\Models\FileType;
 use Illuminate\Foundation\Http\FormRequest;
 
@@ -32,5 +31,4 @@
     public function rules()
     {
-
         $rules = [
             "arch_id" => [
@@ -57,9 +55,9 @@
         if ($this->isMethod("patch")) {
             $fileRules = [
-                "file_item.*" => "mimes:jpg,jpeg,png|max:4096"
+                "file_item.*" => "mimes:jpg,jpeg,png,pdf|max:4096"
             ];
         } else {
             $fileRules = [
-                "file_item.*" => "mimes:jpg,jpeg,png|max:4096"
+                "file_item.*" => "mimes:jpg,jpeg,png,pdf|max:4096"
             ];
         }
Index: app/Http/Requests/Dashboard/FileRequest.php
===================================================================
--- app/Http/Requests/Dashboard/FileRequest.php	(revision ea7b12a15d1afa472a54d5b8c75c55917907862e)
+++ app/Http/Requests/Dashboard/FileRequest.php	(revision ea7b12a15d1afa472a54d5b8c75c55917907862e)
@@ -0,0 +1,30 @@
+<?php
+
+namespace App\Http\Requests\Dashboard;
+
+use Illuminate\Foundation\Http\FormRequest;
+
+class FileRequest extends FormRequest
+{
+    /**
+     * Determine if the user is authorized to make this request.
+     *
+     * @return bool
+     */
+    public function authorize()
+    {
+        return true;
+    }
+
+    /**
+     * Get the validation rules that apply to the request.
+     *
+     * @return array
+     */
+    public function rules()
+    {
+        return [
+            "name" => "required|max:255|regex:/^[^.]+$/",
+        ];
+    }
+}
Index: app/Models/Document.php
===================================================================
--- app/Models/Document.php	(revision b9c4a92282ce0d7aa095082d7109f6d3d7b68dad)
+++ app/Models/Document.php	(revision ea7b12a15d1afa472a54d5b8c75c55917907862e)
@@ -22,8 +22,4 @@
     }
 
-    public function ago() {
-        return Carbon::parse($this->created_at)->diffForHumans();
-    }
-
     public function files()
     {
Index: app/Models/File.php
===================================================================
--- app/Models/File.php	(revision b9c4a92282ce0d7aa095082d7109f6d3d7b68dad)
+++ app/Models/File.php	(revision ea7b12a15d1afa472a54d5b8c75c55917907862e)
@@ -4,9 +4,10 @@
 
 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Support\Facades\Storage;
 
 class File extends Model
 {
     protected $table = "files";
-    protected $fillable = ["link", "document_id"];
+    protected $fillable = ["name", "location", "document_id"];
 
     public function document()
@@ -14,3 +15,10 @@
         return $this->belongsTo(Document::class);
     }
+
+    public function getSize($location)
+    {
+        $fileSize = Storage::disk('local')->size($location) / 1024 / 1024;
+        $fileSize = round($fileSize, 2);
+        return $fileSize;
+    }
 }
Index: app/Models/FileType.php
===================================================================
--- app/Models/FileType.php	(revision b9c4a92282ce0d7aa095082d7109f6d3d7b68dad)
+++ 	(revision )
@@ -1,26 +1,0 @@
-<?php
-
-namespace App\Models;
-
-use App\Rules\ValidationRuleSyntaxChecker;
-use Illuminate\Database\Eloquent\Factories\HasFactory;
-use Illuminate\Database\Eloquent\Model;
-
-class FileType extends Model
-{
-    use HasFactory;
-
-    protected $table = "file_types";
-
-    protected $fillable = [
-        'name',
-        'file_validations',
-        'file_maxsize',
-        "user_id"
-    ];
-
-    public function getCreatedByName()
-    {
-        return User::where('id', $this->user_id)->pluck('username')->first();
-    }
-}
