<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use DB;
use Image;
class AdsController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

  

     public function horizontal()
    {
    	$ads=DB::table('ads')->get();
    	return view('backend.ads.horizontal',compact('ads'));
    }
    public function store(Request $request)
    {

    	$data=array();
    	$data['link']=$request->link;
		$data['desigination']=$request->desigination;
		if ($request->type==2) {
    			$image=$request->ads;
    			$image_one= uniqid().'.'.$image->getClientOriginalExtension();    //123123.jpg
                Image::make($image)->resize(700,400)->save('public/ads/'.$image_one);    //   public/postimages/123123.jpg
                $data['ads']='public/ads/'.$image_one;   //   public/postimages/123123.jpg
                $data['type']=2;   //   public/postimages/123123.jpg
                 DB::table('ads') ->insert($data);
                    $notification=array(
                     'messege'=>'Successfully Post Inserted ',
                     'alert-type'=>'success'
                    );
            return Redirect()->back()->with($notification);
		}
        if ($request->type==3) {
    			$image=$request->ads;
    			$image_one= uniqid().'.'.$image->getClientOriginalExtension();    //123123.jpg
                Image::make($image)->resize(700,400)->save('public/ads/'.$image_one);    //   public/postimages/123123.jpg
                $data['ads']='public/ads/'.$image_one;   //   public/postimages/123123.jpg
                $data['type']=3;   //   public/postimages/123123.jpg
                 DB::table('ads') ->insert($data);
                    $notification=array(
                     'messege'=>'Successfully Post Inserted ',
                     'alert-type'=>'success'
                    );
            return Redirect()->back()->with($notification);
    	}else{
    		   $image=$request->ads;
    			$image_one= uniqid().'.'.$image->getClientOriginalExtension();    //123123.jpg
                Image::make($image)->resize(700,400)->save('public/ads/'.$image_one);    //   public/postimages/123123.jpg
                $data['ads']='public/ads/'.$image_one;   //   public/postimages/123123.jpg
                $data['type']=1;   //   public/postimages/123123.jpg
                 DB::table('ads') ->insert($data);
                    $notification=array(
                     'messege'=>'Successfully Post Inserted ',
                     'alert-type'=>'success'
                    );
            return Redirect()->back()->with($notification);
    	}

    }


    public function edit($id)
{
    $ad = DB::table('ads')->where('id', $id)->first();
    return view('your_edit_view_name', compact('ad'));
}



    public function update(Request $request, $id)
    {
        $data = [];
        $data['link'] = $request->link;
        $data['desigination'] = $request->desigination;
        $data['type'] = $request->type;
    
        if ($request->hasFile('ads')) {
            $image = $request->ads;
            $image_one = uniqid().'.'.$image->getClientOriginalExtension();
    
            // Delete old image if exists
            $old = DB::table('ads')->where('id', $id)->value('ads');
            if (file_exists($old)) {
                unlink($old);
            }
    
            // Save new image
            Image::make($image)->resize(700, 400)->save('public/ads/'.$image_one);
            $data['ads'] = 'public/ads/'.$image_one;
        }
    
        DB::table('ads')->where('id', $id)->update($data);
    
        $notification = [
            'messege' => 'Successfully Post Updated',
            'alert-type' => 'success'
        ];
    
        return Redirect()->back()->with($notification);
    }
    









	//delete Horizontal Ads gallery
    public function destory($id)
    {
    	 DB::table('ads')->where('id',$id)->delete();
    	   $notification=array(
                        'messege'=>'Successfully Deleted',
                        'alert-type'=>'success'
                         );
          return Redirect()->back()->with($notification);

    }

}
