<?php

namespace App\Http\Controllers;

use App\Traits\Api;
use Inertia\Inertia;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Log;

class LeaveController extends Controller
{
    use Api;
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request): JsonResponse
    {

        $data = [
            'paginate' => true,
            'page' => $request->input('page', 1),
            'per_page' => $request->input('per_page', 10),
            'schema' => 'leaves',
            'search_query' => $request->input('filter.search_query'),
        ];
        $response = $this->get(config('services.kedebah.pim_service_url') . "/schemas/data", $data);
        return new JsonResponse($response->object(), $response->status());
    }


    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request): JsonResponse
    {

        $formData = $request->all();
        // dd($formData);
        $formData['schema'] = 'leaves';

        $response = $this->post(config('services.kedebah.pim_service_url') . "/schemas/data", $formData);

        return new JsonResponse($response->object(), $response->status());

    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id): JsonResponse
    {
        $formData = $request->all();
        $formData['schema'] = 'leaves';

        $response = $this->patch(config('services.kedebah.pim_service_url') . "/schemas/data/{$id}", $formData);
        // dd($response, $formData);
        return new JsonResponse($response->object(), $response->status());
    }



    /**
     * Remove the specified resource from storage.
     */

    public function destroy(string $id): JsonResponse
    {
        $formData = [
            'schema' => 'leaves',
        ];
        $response = $this->remove(config('services.kedebah.pim_service_url') . "/schemas/data/$id", $formData);
        return new JsonResponse($response->object(), $response->status());
    }


}
