<?php

namespace App\Http\Controllers;
use App\Models\OtherCustomization;
use Illuminate\Http\Request;

class OtherCustomizationController extends Controller
{
    ## STORE
    public function storeothercusomization(Request $request){
        $validate   =   $request->validate([
            'other_customization_product'       => 'required',
            'other_customization_price'         => 'required',
        ]);
        $storedata                                  = new OtherCustomization();
        $storedata->other_customization_price       = $request->other_customization_price;
        $storedata->other_customization_product     = $request->other_customization_product;
        $StoreData                                  = $storedata->save();
        if ($StoreData) {
            return response()->json(['status' => true,'message'=>'Other Customization Created Successfully'],200);
        }else{
            return response()->json(['message'=>'Update Failed'],400);
        }
    }

    ## GET
    public function getothercusomization(){
        $getdata     = OtherCustomization::all();
        if ($getdata->isNotEmpty()){
            return response()->json(['status' => true, 'data' => $getdata], 200);
        } else {
            return response()->json(['status' => false,'data' => 'failed data'], 400);
        }
    }

    ##FIND
    public function findothercusomization($id){
        $find  = OtherCustomization::where('other_customization_id', $id)->orderBy('other_customization_id', 'desc')->get();
        if ($find->isNotEmpty()){
            return response()->json(['status' => true,'message'=>'Find Data Successfully','data' =>  $find],200);
        }else if($find->isEmpty()){
            return response()->json(['status' => true,'message'=>'data is empty'],204);
        }else{
            return response()->json(['status' => false,'message'=>'failed find records'],400);
        }
    }

    ## SWITCH
    public function switchothercusomization($id){
        $switchData                  = OtherCustomization::find($id);
        $status                      = $switchData->active_status === 1 ? ['num' => 0, 'message' => 'Deactiveted'] : ['num' => 1, 'message' => 'Activeted'];
        $switchData->active_status   = $status['num'];
        $Switch                      = $switchData->save();
        if ($Switch) {
            return response()->json(['status' => true,'message'=> $status['message'].'  '.'Successfully'],200);
        }else{
            return response()->json(['status' => false,'message'=>'failed switch '],400);
        }
    }

    ## UPDATE
    public function updateothercusomization(Request $request){        
        $validate   =   $request->validate([
            'other_customization_product'       => 'required',
            'other_customization_price'         => 'required',
        ]);
        $updatedata                                  = OtherCustomization::find($request->id);
        $updatedata->other_customization_price       = $request->other_customization_price;
        $updatedata->other_customization_product     = $request->other_customization_product;
        $update                                      = $updatedata->save();
        if (!$updatedata){
            return response()->json(['status' => false,'message'=>'update data not found'],404);
        }else if ($update == true) {
            return response()->json(['status' => true,'message'=>'update Success'],200);
        }else{
            return response()->json(['status' => false,'message'=>'failed update'],400);
        }
    }

    ##DESTORY
    public function destroyothercusomization($id){
        $destroy     = OtherCustomization::where('other_customization_id', $id)->delete();
        if ($destroy == true) {
            return response()->json(['status' => true,'message'=>'destroy Success'],200);
        }else{
            return response()->json(['status' => false,'message'=>'failed destroy'],400);
        }
    }

    ##SEARCH
    public function searchothercusomization($name){
        $searchRecord   = OtherCustomization::where('other_customization_product', 'LIKE', '%'.$name.'%')->get();
        if (!empty($searchRecord)) {
            return response()->json(['status' => true,'message'=>'search Record Successfully','data' =>  $searchRecord],200);
        }else{
            return response()->json(['status' => false,'message'=>'searchRecord Record Failed'],400);
        }
    }
    
}
