<?php

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

class GownsizeController extends Controller
{
    ##STORE
    public function storegownssize(Request $request){
        $validate   =   $request->validate([
            'gown_size'                             => 'required',
            'gown_size_age'                         => 'required',
            'gown_size_chest'                       => 'required',
            'gown_size_shoulder_waist_length'       => 'required',
            'gown_size_shoulder_angle__full_length' => 'required',
        ]);
        $storedata                                           = new Gownsize();
        $storedata->gown_size                                = $request->gown_size;
        $storedata->gown_size_age                            = $request->gown_size_age;
        $storedata->gown_size_chest                          = $request->gown_size_chest;
        $storedata->gown_size_shoulder_waist_length          = $request->gown_size_shoulder_waist_length;
        $storedata->gown_size_shoulder_angle__full_length    = $request->gown_size_shoulder_angle__full_length;
        $store                                               = $storedata->save();
        if ($store == true){
            return response()->json(['status' => true,'data' => 'Gowns Size added Successfully'],200);
        }else{
            return response()->json(['status' => false,'data' => 'failed Store'],200);
        }
    }

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

    ##SEARCH
    public function searchgownssize($name){
        $searchRecord   = Gownsize::where('gown_size', 'LIKE', '%'.$name.'%')->orWhere('gown_size_age', 'LIKE', '%'.$name.'%')->orWhere('gown_size_chest', 'LIKE', '%'.$name.'%')->orWhere('gown_size_shoulder_waist_length', 'LIKE', '%'.$name.'%')->orWhere('gown_size_shoulder_angle__full_length', 'LIKE', '%'.$name.'%')->get();
        if ($searchRecord->isNotEmpty()){
            return response()->json(['status' => true,'message'=>'search Record Successfully','data' =>  $searchRecord],200);
        }else if($searchRecord->isEmpty()){
            return response()->json(['status' => true,'message'=>'search Record Empty'],204);
        }else{
            return response()->json(['status' => false,'message'=>'searchRecord Record Failed'],400);
        }
    }

    ## SWITCH
    public function switchgownssize($id){
        $switchData                  = Gownsize::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);
        }
    }

     ##FIND
     public function findgownssize($id){
        $find  = Gownsize::where('gown_size_id', $id)->orderBy('gown_size_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);
        }
    }

    ## UPDATE
    public function updategownssize(Request $request){
        $validate   =   $request->validate([
            'gown_size'                             => 'required',
            'gown_size_age'                         => 'required',
            'gown_size_chest'                       => 'required',
            'gown_size_shoulder_waist_length'       => 'required',
            'gown_size_shoulder_angle__full_length' => 'required',
        ]);
        $updatedata                                           = Gownsize::find($request->id);
        $updatedata->gown_size                                = $request->gown_size;
        $updatedata->gown_size_age                            = $request->gown_size_age;
        $updatedata->gown_size_chest                          = $request->gown_size_chest;
        $updatedata->gown_size_shoulder_waist_length          = $request->gown_size_shoulder_waist_length;
        $updatedata->gown_size_shoulder_angle__full_length    = $request->gown_size_shoulder_angle__full_length;
        $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 destroygownssize($id){
        $destroy     = Gownsize::where('gown_size_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);
        }
    }
}
