<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Intervention\Image\ImageServiceProvider;
use Illuminate\Support\Facades\validator;
use Intervention\Image\Image;
use Input;
use File;
use App\Affiliation;

class AffiliationController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $Affiliation=Affiliation::all();
        return view('Admin.Affiliation')->with('save',$Affiliation);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request,[
        'image'=>'required',
        'title'=>'required',
        
        ]);
            $save=new Affiliation;
            $save->name=$request->title;
            
            
            $file=Input::file('image');
       

            $filename=time().''.$file->getClientOriginalName();
            $save->filename=$filename;
            $file->move(base_path().'/public/img/upload',$filename);
    
           
            $save->save();
    
    
            return redirect('admin_affiliation')->with('message',"Saved Successfully");
    
    
        }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $editAffiliation=Affiliation::where('id',$id)->first();
        return redirect('admin_affiliation')->with('edit',$editAffiliation);
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $repalce= Affiliation::find($id);
         if(Input::hasfile('image'))
         {
            $Image=public_path("/img/upload/{$repalce->filename}");
            if(File::exists($Image))
            {
                $image=Affiliation::where('id',$id)->first();
                $file=$image->filename;
                $filename=public_path().'/img/upload/'.$file;
                File::delete($filename);
            }
            

        $file=Input::file('image');
       

        $filename=time().$file->getClientOriginalName();
       
        $file->move(base_path().'/public/img/upload/',$filename);
$data=array(
            'filename'=>$filename,
            'name'=>$request->title,

        );


         }else{
             $data=array(
            'name'=>$request->title,
            
                
        );
         }


          

        Affiliation::where('id',$id)->update($data);
        return redirect('admin_affiliation')->With('message',"Successfully updated");
    }
           

   

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
    $image=Affiliation::where('id',$id)->first();
    $file=$image->filename;
    $filename=public_path().'/img/upload/'.$file;
    File::delete($filename);
    Affiliation::where ('id',$id)->delete();
    return redirect('admin_affiliation')->With('Affiliation'," Successfully deleted");
    }
}
