<?php

namespace App\Http\Controllers\admin\news;

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

class newsController extends Controller
{
    public function news_index(){
      return view('admin.news.news');
    }

    public function add_news(Request $request){
     $news=new News();
     $news->news_title=$request->news_title;
     $news->description=$request->news_description;
     $imagename = time() . rand(1, 1000) . '.' . $request->news_img->extension();
     $request->news_img->move(public_path('admin/assets/img/news'), $imagename);
     $news->news_image=$imagename;
     $news->status=1;
     $result=$news->save();
     if ($result) {
        return response()->json(['code' => 'true', 'msg' => 'News added successfully']);
    } else {
        return response()->json(['code' => 'false', 'msg' => 'somethong went wrong']);
    }
    }

    public function get_news(){
        $data = DB::table('news')
        ->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 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/news/' . $data->news_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_news($id){
        $data = News::find($id);
        return $data;
    }

    public function update_news(Request $request){
        $validatedData = $request->validate([
            'edit_news_title' => 'required|string|max:255',
            'edit_news_description' => 'required|string',
            'updateid' => 'required|integer|exists:news,id', // ensure updateid exists in news table
        ]);
      $news=News::find($validatedData['updateid']);
      $news->news_title = $validatedData['edit_news_title'];
      $news->description = $validatedData['edit_news_description'];
        if ($request->hasFile('editimage')) {
        $imagename = time() . rand(1, 1000) . '.' . $request->editimage->extension();
        $request->editimage->move(public_path('admin/assets/img/news'), $imagename);
        $news->news_image = $imagename;
    }
      $result=$news->update();
      if ($result) {
        return response()->json(['code' => 'true', 'msg' => 'News updated successfully']);
    } else {
        return response()->json(['code' => 'false', 'msg' => 'somethong went wrong']);
    }
    }

    public function delete_news($newsid){
        $result = News::find($newsid)->delete();
        if ($result) {
            return response()->json(['code' => 'true', 'msg' => 'News deleted successfully']);
        } else {
            return response()->json(['code' => 'false', 'msg' => 'somethong went wrong']);
        }
    }
}
