<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Aboutus;
use App\Models\MainCategory;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Exception;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;



class AboutUsController extends Controller
{
   
   
    /**
     * Created By : sayali parab
     * Created at : 20 May 2024
     * Use : To view about us  page.
     */
   
    public function index()
    {
        $view_about = Aboutus::get()->toArray();


        return view('Admin.pages.manage_cms.manage_aboutus.manage_about_us', compact('view_about'));
    }
    /**
     * Created By : sayali parab
     * Created at : 20 May 2024
     * Use : To edit about us page.
     */

    public function edit($id)
    {
        $edit_aboutUs_cust =  Aboutus::find($id)->toArray();
        // dd($edit_privacy_policy);
        return view('Admin.pages.manage_cms.manage_aboutus.manage_about_us_cust', compact('edit_aboutUs_cust'));

    }


     /**
     * Created By : sayali parab
     * Created at : 20 May 2024
     * Use : To  edit about us  page.
     */

    public function edit_rest($id)
    {
        $edit_about_rest =  Aboutus::find($id)->toArray();
        // dd('sdfnkjfn');
        return view('Admin.pages.manage_cms.manage_aboutus.manage_about_us_rest', compact('edit_about_rest'));
    }

    /**
     * Created By : sayali parab
     * Created at : 20 May 2024
     * Use : To update about us  page.
     */
    public function update(Request $request)
    {


        try {
            DB::beginTransaction();
            $about_data = Aboutus::where('id', $request->about_id)->first();

            if ($request->hasFile('about_image')) {
                $image = $request->file('about_image');
                $normalImage = saveSingleImageWithoutCrop($image, 'about_images');
                $about_data->thumbnail_image = $normalImage;
            }

            $about_data->title = $request->input('about_title');
            $about_data->description = $request->input('about_des');
            $about_data->category_xid = $request->input('category');

            $about_data->save();


            DB::commit();

            return jsonResponseWithSuccessMessage(__('success.update_data'));
        } catch (Exception $e) {
            DB::rollBack();
            Log::error("updateCustomerNewsArticle Services Page Load Failed " . $e->getMessage());
            return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
        }
    }

    /**
     * Created By : sayali parab
     * Created at : 21 May 2024
     * Use : To delete about us  page.
     */
    public function delete_about($id)
    {


        try {
            $blog = Aboutus::find($id);

            if (!$blog) {
                return response()->json(['error' => 'Aboutus entry not found.'], 404);
            }

            $blog->delete();

            return response()->json(['success' => 'Aboutus entry deleted successfully.']);
        } catch (\Exception $e) {
            // Log the exception or handle it in a way that makes sense for your application
            return response()->json(['error' => 'An error occurred while deleting the Aboutus entry.'], 500);
        }
    }

     /**
     * Created By : sayali parab
     * Created at : 21 May 2024
     * Use : To change status about us  page.
     */
    public function change_about_Status(Request $request)
    {


        try {
            $status = Aboutus::find($request->program_id);

            if (!$status) {
                return response()->json(['error' => 'Aboutus entry not found.'], 404);
            }

            $status->is_active = $request->status;
            $status->save();

            return response()->json(['success' => 'Status change successfully.']);
        } catch (\Exception $e) {
            // Log the exception or handle it in a way that makes sense for your application
            return response()->json(['error' => 'An error occurred while changing the status.'], 500);
        }
    }
    /**
     * Created By : sayali parab
     * Created at : 21 May 2024
     * Use : To add about us  page.
     */
    public function add()
    {
        $about_us_cat =  MainCategory::all()->toArray();
        return view('Admin.pages.manage_cms.manage_aboutus.manage_about_us_add', compact('about_us_cat'));
    }
    /**
     * Created By : sayali parab
     * Created at : 21 May 2024
     * Use : To insert about us  page.
     */
    public function insert(Request $request)
    {

        try {
            DB::beginTransaction();

            if (isset($request->about_image)) {

                $image = $request->about_image;
                $image_db = null;
            } else {
                $image = null;
                $image_db = $request->about_image;
            }
            $tnormalImage = saveSingleImageWithoutCrop($image, 'about_images', $image_db);


            $about_data = new Aboutus();
            $about_data->title = $request->input('about_title');
            $about_data->description = $request->input('about_des');

            $about_data->thumbnail_image = $tnormalImage;
            $about_data->category_xid = $request->input('category');
            $about_data->save();

            DB::commit();
            return jsonResponseWithSuccessMessage(__('success.save_data'));
            // return $voucher_data;
        } catch (Exception $e) {
            DB::rollBack();
            Log::error("About Store Page Load Failed " . $e->getMessage());
            return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
        }
    }
   

    //new update customer aboutUs 
     /**
     * Created By : sayali parab
     * Created at : 11 May 2024
     * Use : To update customer about us  page.
     */
    public function update_aboutUS_Cust(Request $request)
    {
        $update = Aboutus::find($request->about_custom_id);
        $update->description = $request->input('about_us');
        // dd( $update);
        $update->save();
        return response()->json(['success' => true, 'status' => 200]);
    }


    //new update restatuant aboutUs 
 

     /**
     * Created By : sayali parab
     * Created at : 11 May 2024
     * Use : To update resturant about us  page.
     */
    public function update_aboutUS_Rest(Request $request)
    {
        $update_rest = Aboutus::find($request->about_rest_id);
        $update_rest->description = $request->input('about_rest');
        // dd($update_rest);
        $update_rest->save();
        return response()->json(['success' => true, 'status' => 200]);
    }


}
