<?php

namespace App\Http\Controllers\Admin;

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

class CategoryController extends Controller
{
    
    function __construct()
    {
        $this->middleware('permission:Category List', ['only' => ['index']]);
        $this->middleware('permission:Category Create', ['only' => ['store']]);
        $this->middleware('permission:Category Edit', ['only' => ['store']]);
        $this->middleware('permission:Category Delete', ['only' => ['destroy']]);
    }

    public function index(Request $request)
    {
        $per_page = $request->per_page == null?25: $request->per_page;
        $data = Category::search($request->search_text)
        ->orderBy('id','DESC')
        ->paginate($per_page);
        return $data;
    }

    public function create(Request $request)
    {
        $cat = Category::find($request->id);

        if($cat){
            $res = [
                'status' => 1,
                'message'=> 'Create category success',
                'data'   => $cat,
            ];
        }else{
            $res = [
                'status'  => 0,
                'message' => 'Create category not found',
                'data'    => [],
            ];
        }
        return response()->json($res,200);
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'title' => 'nullable|required',
        ]);
        try{
            DB::beginTransaction();
            $com = Category::updateOrCreate(['id' => $this->getCategoryRequest($request)['id']],$this->getCategoryRequest($request));

            DB::commit();
            $res = [
                'status' => 1,
                'message'=> $com->wasRecentlyCreated?'Create Category success':'Update Category success',
                'data'   => $com,
            ];
        }catch(Exception $e){
            DB::rollBack();
            $res = [
                'status'  => 0,
                'message' => 'Create Category not success',
                'data'    => $e->getMessage(),
            ];
        }
        return response()->json($res,200);
    }

    public function getCategoryRequest($request)
    {
        $data = [
            'id'      => $request->id,
            'title'   => $request->title,
            'purpose' => $request->purpose,
        ];
        return $data;
    }

    public function show(Category $category)
    {
        //
    }

    public function edit(Category $category)
    {
        //
    }

    public function update(Request $request, Category $category)
    {
        //
    }

    public function destroy($id)
    {
        DB::beginTransaction();
        try {
            $cat = Category::find($id);
            if($cat->collateralHasCategory->count()>0){
                $res = [
                    'status'  => 0,
                    'message' => 'មិនអាចលុបបានទេ ពីព្រោះលោកអ្នកបានធ្វើប្រតិបត្តិការរួចហើយ',
                    'data'    => '',
                ];
            } else{
                $cat->delete();
                $res = [
                    'status' => 1,
                    'message'=> 'Deleted success',
                    'data'   => $cat,
                ];
            }
            DB::commit();
        }catch(Exception $e){
            DB::rollBack();
            $res = [
                'status'  => 0,
                'message' => 'Deleted not success',
                'data'    => $e->getMessage(),
            ];
        }
        return response()->json($res,200);
    }
}