<?php
namespace App\Http\Controllers;

use App\Models\BankAccount;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Auth;

class AddBankController extends Controller
{
    public function tambahBank(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'method' => ['required', 'string'],
            'nama_rek' => ['required', 'string'],
            'bank' => ['required', 'string'],
            'no_rek' => ['required', 'string'],
        ]);

        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()->first()], 400);
        }

        $bankAccount = new BankAccount();
        $bankAccount->method = $request->method;
        $bankAccount->nama_rek = $request->nama_rek;
        $bankAccount->bank = $request->bank;
        $bankAccount->no_rek = $request->no_rek;
        $bankAccount->user_id = Auth::user()->id; // Menggunakan ID pengguna yang sedang masuk

        $bankAccount->save();

        return response()->json(['success' => 'Bank baru berhasil ditambahkan'], 200);
    }
}