<?php

namespace Modules\Internal\Http\Controllers\Api;

use App\Helpers\Helper;
use App\Models\RequestLogMedicine;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;


class RequestLogMedicineController extends Controller
{
    
    public function index(Request $request)
    {
       
    }

    /**
     * Show the form for creating a new resource.
     * @return Renderable
     */
    public function create()
    {
        return view('internal::create');
    }

    /**
     * Show the specified resource.
     * @param int $id
     * @return Renderable
     */
    public function show($id)
    {
        
    }

     /**
     * Store a newly created resource in storage.
     * @param Request $request
     * @return Renderable
     */
    public function store(Request $request)
    {

        $customMessages = [
            'required' => 'Kolom :attribute wajib diisi.',
            'numeric' => 'Kolom :attribute harus berupa angka.',
        ];

        $validator = Validator::make($request->all(), [
            'medicine' => 'required|array',
            'medicine.*' => 'required',
        ], $customMessages);

        if ($validator->fails()) {
            return Helper::responseJson([],'error', 400, $validator->errors());
        } else {
            $medicine = $request->medicine;

            if (count($medicine)>0){
                // BeginTransaction
                DB::beginTransaction();
                foreach($medicine as $key => $value){
                    $data = [
                        'request_log_id' => $value['request_log_id'],
                        'medicine' => $value['medicine_name'],
                        'price' => $value['medicine_price'],
                    ];
                    // Insert Data
                    try {
                        RequestLogMedicine::create($data);
                    } catch (\Throwable $th) {
                        DB::rollBack();
                        return Helper::responseJson(status: 'failed', statusCode: 500, message: $th->getMessage());
                    }
                }
                DB::commit();
                return Helper::responseJson(status: 'success', statusCode: 201, message: 'success', data: $request->toArray());
            };

        }
        
        
        $requestLogMedicine = RequestLogMedicine::insert($data);
        return $requestLogMedicine;
    }

    /**
     * Show the form for editing the specified resource.
     * @param int $id
     * @return Renderable
     */
    public function edit($id)
    {
        return view('internal::edit');
    }

    /**
     * Update the specified resource in storage.
     * @param Request $request
     * @param int $id
     * @return Renderable
     */
    public function update(Request $request, $id)
    {
        $requestLogBenefit = requestLogBenefit::findOrFail($id);
        $requestLogBenefit->amount_approved = $request->amount_approved;
        $requestLogBenefit->amount_incurred = $request->amount_incurred;
        $requestLogBenefit->amount_not_approved = $request->amount_not_approved;
        $requestLogBenefit->excess_paid = $request->excess_paid;
        $requestLogBenefit->keterangan = $request->keterangan;

        $requestLogBenefit->save();
      
        return response()->json([
            'error' => false,
            'message' => 'Update succses',
            'data' =>  $requestLogBenefit],
        200);
    }

    /**
     * Remove the specified resource from storage.
     * @param int $id
     * @return Renderable
     */
    public function destroy($id)
    {
       $RequestLogMedicine = RequestLogMedicine::findOrFail($id);
       $RequestLogMedicine->delete();
    }
    
}
