<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\Category;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Categorybanner;


class categoryController extends Controller
{
    public function category_section(){
        return view('admin.category.category');
    }

    public function create_category(Request $request){
    $imagename = time() . rand(1, 1000) . '.' . $request->category_img->extension();
    $request->category_img->move(public_path('admin/assets/img/category'), $imagename);
    $category=new Category();
    $category->category_name=$request->category_name;
    $category->category_image=$imagename;
    $category->status=1;
    $result=$category->save();
    if ($result) {
        return response()->json(['code' => 'true', 'msg' => 'Category created']);
    } else {
        return response()->json(['code' => 'false', 'msg' => 'somethong went wrong']);
    }
    }

    public function get_category(){
        $data = DB::table('category')
        ->get();
        // ->join('product', 'deals.product_id', '=', 'product.id') // joining the contacts table , where user_id and contact_user_id are sam
        return datatables()->of($data)
            ->addIndexColumn()

            ->addColumn('action', function ($data) {
                $btn = '<div class="dropdown">
                    <button type="button" class="btn p-0 dropdown-toggle hide-arrow" data-bs-toggle="dropdown">
                        <i class="bx bx-dots-vertical-rounded"></i>
                    </button>
                    <div class="dropdown-menu">
                           <a class="dropdown-item add-banner" href="javascript:void(0);" data-id="' . $data->id . '">
                        <i class="bx bx-file"></i>
                        </i>Banner
                        </a>
                        <a class="dropdown-item edit" href="javascript:void(0);" data-id="' . $data->id . '">
                        <i class="bx bx-edit"></i>
                        </i>Edit
                        </a>

                        <a class="dropdown-item delete" href="javascript:void(0);" data-id="' . $data->id . '">
                        <i class="bx bx-trash me-1"></i> Delete
                    </a>
                    </div>
                </div>';
                return $btn;
            })
            ->addColumn('image', function ($data) {
                $url = asset('admin/assets/img/category/' . $data->category_image);
                $image = '<img src="' . $url . '" style="height:100px; max-height: 100px; max-width:100px; width: 100px;  border-radius:50%; border: 1px solid #555;" />';
                return $image;
            })

            ->rawColumns(['image', 'action'])
            ->make(true);
    }

    public function edit_category($id){
     $data=Category::where('id',$id)->first();
     return $data;
    }

    public function update_category(Request $request){
    $data=Category::find($request->updateid);
    if ($request->has('editimage')) {
        $file_old = 'admin/assets/img/category/' . $data->category_image;
        unlink($file_old);
        $image = time() . rand(1, 1000) . '.' . $request->editimage->extension();
        $request->editimage->move(public_path('admin/assets/img/category/'), $image);
        $data->update(['category_image' => $image]);
    }
    $data->category_name=$request->categoryedit_name;
    $result=$data->update();
    if ($result) {
        return response()->json(['code' => 'true', 'msg' => 'category updated']);
    } else {
        return response()->json(['code' => 'false', 'msg' => 'somethong went wrong']);
    }

    }

    public function banner_index($id){
    return view('admin.category.categorybanner.category_banner',compact('id'));
    }

    public function delete_category($categoryId){
        try {
            $data=Category::find($categoryId);
            $data->delete();
            return response()->json(['message' => 'User deleted successfully']);
        } catch (\Exception $e) {
            return response()->json(['error' => 'Error deleting user', 'message' => $e->getMessage()], 500);
        }
    }

    public function addcategory_banner(Request $request){

        $requestid=$request->categoryid;
        $exists = DB::table('category_banner')->where('category_id', $requestid)->exists();
        if ($exists) {
            return response()->json(['code' => 'false', 'msg' => 'banner already placed']);
        } else {
            $categoryBanner=new Categorybanner();
            $categoryBanner->category_id=$request->categoryid;
            $categoryBanner->title=$request->title;
            $imagename = time() . rand(1, 1000) . '.' . $request->category_banner->extension();
            $request->category_banner->move(public_path('admin/assets/img/category/banner'), $imagename);
            $categoryBanner->image=$imagename;
            $categoryBanner->status=1;
            $result=$categoryBanner->save();
        if ($result) {

            return response()->json(['code' => 'true', 'msg' => 'Category banner added']);
        } else {
            return response()->json(['code' => 'false', 'msg' => 'somethong went wrong']);
        }
        }

    }

    public function removecategory_banner(Request $request){
        $bannerId = $request->input('id');

        // Find the banner by ID and delete it
        $banner = Categorybanner::find($bannerId);

        if ($banner) {
            $banner->delete();
            return response()->json(['success' => true, 'message' => 'Banner deleted successfully.']);
        } else {
            return response()->json(['success' => false, 'message' => 'Banner not found.'], 404);
        }
    }
}
