Current File : /home/tradevaly/new.tradevaly.com.bd/app/Http/Controllers/User/PackageController.php
<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Models\Package;
use App\Models\PackageFeature;
use Brian2694\Toastr\Facades\Toastr;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Str;
class PackageController extends Controller
{
    public function index(){
        $packages= Package::latest()->get();
        return view('backend.pages.package.index')->with(compact('packages'));
    }
    
    public function features($id){
     if($id == 6){
         return '<select name="value" class="form-control" required>
         <option value="">Select Value</option>
         <option value="Buyer Inquiries">Buyer Inquiries</option>
         <option value="Verified Buyer Inquiries">Verified Buyer Inquiries</option>
         <option value="Connected Buyers">Connected Buyers</option>
         <option value="Connected A & B Grade Buyers">Connected A & B Grade Buyers</option>
         <option value="Connected A Grade Buyers">Connected A Grade Buyers</option>
         </select>';
     } elseif($id == 7){
          return '<select name="value" class="form-control" required>
          <option value="">Select Value</option>
         <option value="10">10</option>
         <option value="15">15</option>
         <option value="20">20</option>
         <option value="30">30</option>
         <option value="50">50</option>
         </select>';
     } elseif($id == 11){
          return '<select name="value" class="form-control" required>
         <option value="0">Not Available</option>
         <option value="Yearly">Yearly</option>
         <option value="Quarterly">Quarterly</option>
         <option value="Monthly">Monthly</option>
         <option value="Weekly / Monthly">Weekly / Monthly</option>
         </select>';
     }  elseif($id == 17){
          return '<select name="value" class="form-control" required>
         <option value="">Select Value</option>
         <option value="0">Not Available</option>
         <option value="Shared">Shared</option>
         <option value="01 Hour">01 Hour</option>
         <option value="02 Hours">02 Hours</option>
         <option value="08 Hours">08 Hours</option>
         </select>';
     }  elseif($id == 24){
          return '<select name="value" class="form-control" required>
         <option value="">Select Value</option>
         <option value="0">Not Available</option>
         <option value="(5 Keywords) 1st Page Guaranteed Ranking on Google">(5 Keywords) 1st Page Guaranteed Ranking on Google</option>
         <option value="(10 Keywords) 1st Page Guaranteed Ranking on Google">(10 Keywords) 1st Page Guaranteed Ranking on Google</option>
         </select>';
     }  elseif($id == 26){
          return '<select name="value" class="form-control" required>
         <option value="">Select Value</option>
         <option value="0">Not Available</option>
         <option value="Basic Marketing">Basic Marketing</option>
         <option value="Advance Marketing">Advance Marketing</option>
         <option value="Paid Marketing">Paid Marketing</option>
         </select>';
     } else {
         return '<select name="value" class="form-control" required>
         <option value="">Select Value</option>
         <option value="0">Not Available</option>
         <option value="1">Available</option>
         </select>';
     }
    }
    public function featured($id){
       $packages= PackageFeature::where('package_id',$id)->get();
        return view('backend.pages.package.featured')->with(compact('packages', 'id'));
    }
    
    public function add(Request $request){
       
        if($request->isMethod('post')){
            
            $data=new Package();
            if($request->hasFile('image'))
            {
            $image=$request->file('image');
            $currentDate=Carbon::now()->toDateString();
            $imageName=$currentDate.'-'.uniqid().'.'.$image->getClientOriginalExtension();
            
            if(Storage::disk('public')->exists('images/admin/package'))
            {
            Storage::disk('public')->makeDirectory('images/admin/package');
            }
            $packageImage = Image::make($image)->resize(400,200)->stream();
            Storage::disk('public')->put('images/admin/package/'.$imageName,$packageImage);
            $data->image=$imageName;
            }else{
                $data->image='default.png';
            }
            $data->name=$request->name;
            $data->details=$request->details;
            $data->price=$request->price;
            $data->slug=Str::slug($request->name);
            $data->status='1';
            $data->save();
            Toastr::success('Uploaded!');
            return redirect('b2b/package/index');die;
        }
        
        return view('backend.pages.package.add');
    }
    public function edit(Request $request, $id){
       
        if($request->isMethod('post')){
            
            $data=Package::find($id);
            if($request->hasFile('image'))
            {
            $image=$request->file('image');
            $currentDate=Carbon::now()->toDateString();
            $imageName=$currentDate.'-'.uniqid().'.'.$image->getClientOriginalExtension();
            
            if(Storage::disk('public')->exists('images/admin/package'))
            {
            Storage::disk('public')->makeDirectory('images/admin/package');
            }
            $packageImage = Image::make($image)->resize(400,200)->stream();
            Storage::disk('public')->put('images/admin/package/'.$imageName,$packageImage);
            $data->image=$imageName;
            }else{
                $data->image=$data->image;
            }
            $data->name=$request->name;
            $data->details=$request->details;
            $data->price=$request->price;
            if($data->status==0){
                $data->status='0';
            }else{
                $data->status='1';
            }
            $data->save();
            Toastr::success('Updated!');
            return redirect('b2b/package/index');die;
        }
        
        $package=package::find($id);
        return view('backend.pages.package.edit')->with(compact('package'));
    }
    public function delete($id){
        $data= Package::find($id);
        if(Storage::disk('public')->exists('images/admin/package/'.$data->image))
        {
           Storage::disk('public')->delete('images/admin/package/'.$data->image);
        }
        $collection = PackageFeature::where('package_id', $id)->get(['id']);
        PackageFeature::destroy($collection->toArray());
        $data->delete();
        Toastr::success('package Deleted!','Success!');
      return redirect()->back();die;
    }
    
    
    public function featured_add(Request $request, $id){
             if($request->isMethod('post')){
                $feature = new PackageFeature();
                $feature->value = $request->value;
                $feature->feature_id = $request->feature_id;
                $feature->package_id = $id;
                $feature->save();
                Toastr::success('Added!');
                 return redirect()->route('admin.package.featured', $id);
             }
             return view('backend.pages.package.add_features', compact('id'));
        
    }
    
    public function featured_delete(Request $request, $id){
            $data= PackageFeature::find($id);
            $data->delete();
            Toastr::success('Featured Deleted!','Success!');
            return redirect()->back();die;
    }
    
    public function featured_edit(Request $request, $id){
       $data= PackageFeature::find($id);
        if($request->isMethod('post')){
            $data->name = $request->name;
            $data->status = $request->status;
            $data->save();
            Toastr::success('Updated!');
            return redirect()->route('admin.package.featured', $data->package_id);
        }
        return view('backend.pages.package.edit_features', compact('id', 'data'));
        
    }
    
    public function updatePackageStatus(Request $request){
        if($request->ajax()){
            $data= $request->all();
            if($data['status']=='Active'){
                $status=0;
            }else{
                $status=1;
            }
            Package::where('id',$data['package_id'])->update(['status'=>$status]);
            return response()->json(['status'=>$status, 'id'=>$data['package_id']]);
        }
    }
}