<?php

namespace Modules\Internal\Http\Controllers\Api;

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


class RequestLogBenefitController 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(), [
            'benefit_data' => 'required|array',
            'benefit_data.*' => 'required',
        ], $customMessages);

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

            if (count($benefitData)>0){
                // BeginTransaction
                DB::beginTransaction();
                foreach($benefitData as $key => $value){
                    $data = [
                        'request_log_id' => $value['request_log_id'],
                        'benefit_id' => $value['benefit_id'],
                        'amount_incurred' => $value['amount_incurred'],
                        'amount_approved' => $value['amount_approved'],
                        'amount_not_approved' => $value['amount_not_approved'],
                        'excess_paid' => $value['excess_paid'],
                        'keterangan' => $value['keterangan'],
                        'created_by' => auth()->user()->id,

                    ];
                    // Insert Data
                    try {
                        RequestLogBenefit::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());
            };

        }
        
        
        $requestLogBenefit = RequestLogBenefit::insert($data);
        return $requestLogBenefit;
    }

    /**
     * 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->updated_by = auth()->user()->id;
        $requestLogBenefit->updated_at = Carbon::now();

        $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)
    {
       $requestLogBenefit = RequestLogBenefit::findOrFail($id);
       $requestLogBenefit->delete();
    }
    
}
