<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\MultiProject;
use App\ProjectType;

class MultiProjectController extends Controller
{
    //
    public function index(){
    
       $multiprojects= MultiProject::where('name','!=','Super Admin')->get();

        activity('View')->log('View Multiproject List');

        return view('multiproject.index',compact('multiprojects'));
    }
    public function create(){
        $project_type=   ProjectType::all();

        activity('Add')->log('Add New Project');

       
         return view('multiproject.create',compact('project_type'));
     }
     public function store(Request $request ){
 
    $this->validate($request,[
        'project_name'=>['required', 'string', 'max:255','unique:multi_projects,name'],
        'project_type' => 'required',
      
        'image'=>['required'],
        'address'=>['nullable', 'string', 'max:255'],
        'description'=>['nullable', 'string', 'max:1055'],
        
     ]);
 

   $multiproject= new MultiProject;
   $multiproject->name = $request->project_name;
   $multiproject->address = $request->address;
   $multiproject->project_type_id=$request->project_type;
   if($request->file('image')){
    $file= $request->file('image');
    $filename= date('YmdHi').$file->getClientOriginalName();
    $file->move(public_path('images'), $filename);
    $multiproject->image= $filename;
}
   
   $multiproject->description = $request->description;
   $multiproject->save();

       
        return redirect()->route('multi.index')->with('message', 'Your Project Created Sucessfully');
    }


    public function edit($id){
try{
    
   $pro_id=   decrypt($id);
      $multiprojects= MultiProject::where('id', $pro_id)->first();
      $project_type=   ProjectType::all();
       
      return view('multiproject.edit',compact('multiprojects','id','project_type'));
}
catch(\Exception $e){

    return redirect()

    ->route('multi.index')
            ->with([
                'message' => 'Invalid Inputs!',
                'alert-type' => 'error',
            ]);
}


    }
    public function update(Request $request){
        $this->validate($request,[
            'project_name'=>['required', 'string', 'max:255'],
          'id' =>['required'],
            // 'image'=>['required'],
            'address'=>['nullable', 'string', 'max:255'],
            'description'=>['nullable', 'string', 'max:1055'],
            'project_type' => 'required'
            
         ]);
         try{
    
            $pro_id=   decrypt($request->id);
               $multiproject= MultiProject::where('id', $pro_id)->first();
               
               if( $multiproject->name != $request->project_name){
                $this->validate($request,[
                    'project_name'=>['unique:multi_projects,name'],
                ]);
               }


               $multiproject->name = $request->project_name;
               $multiproject->address = $request->address;
             
               $multiproject->project_type_id=$request->project_type;
   
               if($request->file('image')){
                $file= $request->file('image');
                $filename= date('YmdHi').$file->getClientOriginalName();
                $file->move(public_path('images'), $filename);
                $multiproject->image= $filename;
            }
               
               $multiproject->description = $request->description;
               $multiproject->save();
               return redirect()->route('multi.index')->with('message', 'Your Project Updated Sucessfully');
         }
         catch(\Exception $e){
         
             return redirect()
         
             ->back()
                     ->with([
                         'message' => 'Invalid Inputs!',
                         'alert-type' => 'error',
                     ]);
         }
    
    }

}
