<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Aset;
use Illuminate\Support\Facades\Storage;

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

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        if (!empty($request->foto)) {
            $uploadFolder = 'aset-images';
            $image = $request->file('foto');
            $image_uploaded_path = $image->store($uploadFolder, 'public_uploads');
            $uploadedImageResponse = array(
                "image_name" => basename($image_uploaded_path),
                "image_url" => Storage::disk('public_uploads')->url($image_uploaded_path),
                "mime" => $image->getClientMimeType()
            );
            $namafile = $uploadedImageResponse['image_name'];
        }
        
        $create = Aset::create([
            'keterangan' => request('keterangan'),
            'kategori' => request('kategori'),
            'nama' => request('nama'),
            'jumlah' => request('jumlah'),
            'status' => request('status'),
            'lokasi' => request('lokasi'),
            'penanggungjawab' => request('penanggungjawab'),
            'foto' => $namafile
        ]);

        try{
            return response()->json([
                $create,
                'status' => true,
                'message' => 'uploaded successfully',
            ]);
        } catch(\Exception $e) {
            return response()->json([
                'message' => 'Err',
                'errors' => $e -> getMessage()
            ]);
        }
        // return $create;
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validasi = $request->validate([
            // 'id_aset' => 'required',
            'keterangan' => 'required',
            'kategori' => 'required',
            'nama' => 'required',
            'status' => 'required',
            'lokasi' => 'required',
            'penanggungjawab' => 'required',
            'foto' => 'required|file|mimes:png,jpg'
        ]);
        try {
            $filename = time().$request->file('foto')->getClientOriginalName();
            $path = $request->file('foto')->storeAs('uploads/aset', $filename);
            $validasi['foto']=$path;
            $response = Aset::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 = Aset::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)
    {
        try {
            $response = Aset::findOrFail($id);
            if (!empty($request->foto)) {
                $uploadFolder = 'aset-images';
                $image = $request->file('foto');
                $image_uploaded_path = $image->store($uploadFolder, 'public_uploads');
                $uploadedImageResponse = array(
                    "image_name" => basename($image_uploaded_path),
                    "image_url" => Storage::disk('public_uploads')->url($image_uploaded_path),
                    "mime" => $image->getClientMimeType()
                );
                $namafile = $uploadedImageResponse['image_name'];
                $response->update([
                    'keterangan' => $request->keterangan,
                    'kategori' => $request->kategori,
                    'nama' => $request->nama,
                    'jumlah' => $request->jumlah,
                    'status' => $request->status,
                    'lokasi' => $request->lokasi,
                    'penanggungjawab' => $request->penanggungjawab,
                    'foto' => $namafile
                ]);
            } else {
                $response->update([
                    'keterangan' => $request->keterangan,
                    'kategori' => $request->kategori,
                    'nama' => $request->nama,
                    'jumlah' => $request->jumlah,
                    'status' => $request->status,
                    'lokasi' => $request->lokasi,
                    'penanggungjawab' => $request->penanggungjawab
                ]);
            }
            $i = 2;
            return response()->json([
                'keterangan' => $request->keterangan,
                'kategori' => $request->kategori,
                'nama' => $request->nama,
                'jumlah' => $request->jumlah,
                'status' => $request->status,
                'lokasi' => $request->lokasi,
                'penanggungjawab' => $request->penanggungjawab,
                'foto' => $namafile,
                'success' => true,
                'message' => 'success'
            ]);
        } catch(\Exception $e) {
            return response()->json([
                'message' => 'Err',
                'errors' => $e -> getMessage()
            ]);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id_aset)
    {
        try {
            $aset=Aset::find($id_aset);
            $aset->delete();
            return response()->json([
                'success'=>true,
                'message'=>'success'
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'message'=>'Err',
                'errors'=>$e->getMessage()
            ]);
        }
    }
}
