<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\KategoriAset;
use Exception;
use Illuminate\Support\Facades\Log;

class KategoriAsetController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = KategoriAset::all();
        return response()->json($data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        KategoriAset::create([
            'nama' => request('nama')
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validasi = $request->validate([
            'nama' => 'required'
        ]);
        try {
            $response = KategoriAset::create($validasi);
            return response()->json([
                'success' => true,
                'message' => 'success'
            ]);
        } catch(\Exception $e) {
            return response()->json([
                'message' => 'Err',
                'errors' => $e -> getMessage()
            ]);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $data = KategoriAset::where('id', $id)->get()->first();
        return response()->json($data);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $item = KategoriAset::findOrFail($id);
        try {
            $item->update([
                'nama' => $request->nama
            ]);
            return ['result'=>$item];
        } catch (Exception $err) {
            Log::error($err);
            return ['result'=>$err];
        }

        // $data = KategoriAset::find($id);
        // $data->id = $request->id;
        // $data->nama = $request->nama;
        // $result = $data->save();
        // if($result){
        //     return ['result'=>'data has been updated'];
        // } else {
        //     return ['result'=>'update data has been failed'];
        // }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $item = KategoriAset::findOrFail($id);
        $result = $item->delete();
        if($result){
            return ['result' => $id ." ". 'Record has been delete.'];
        } else {
            return ['result' => $id ." ". 'Delete failed.'];
        }
    }
}
