<?php

namespace App\Http\Controllers;

use App\Models\PaymentType;
use Illuminate\Http\Request;
use Inertia\Inertia;

class PaymentTypeController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $paymentType = PaymentType::all();
        return Inertia::render('list-admin/payment/PaymentType', [
            'paymentType' => $paymentType,
            'fields' => [
                'payment_type' => 'text'
            ]
        ]);
    }

    public function store(Request $request)
    {
        $request->validate([
            'payment_type' => 'required|string'
        ], [
            'payment_type.required' => 'wajib mengisi payment type'
        ]);

        try {
            PaymentType::create([
                'payment_type' => $request->payment_type
            ]);

            return redirect()->back()->with('success', 'berhasil insert data');
        } catch (\Throwable $th) {
            return redirect()->back()->with('error', 'Data gagal di tambahkan' . $th->getMessage());
        }
    }

    public function update(Request $request, PaymentType $paymentType, $id)
    {
        $request->validate([
            'payment_type' => 'required|string'
        ], [
            'payment_type.required' => 'wajib mengisi payment type'
        ]);

        try {
            $paymentType = PaymentType::findOrFail($id);

            $paymentType->payment_type = $request->payment_type;
            $paymentType->update();

            return redirect()->back()->with('success', 'Berhasil Update Data');
        } catch (\Throwable $th) {
            return redirect()->back()->with('error', 'Gagal Mengupdate Data');
        }
    }

    public function destroy(PaymentType $paymentType, $id)
    {
        $paymentType = PaymentType::findOrFail($id);

        $paymentType->delete();

        return redirect()->back()->with('success', 'Berhasil Menghapus Data');
    }
}
