<?php

namespace App\Http\Controllers\Backend;

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

class BankController extends Controller
{
    public function index(Request $request)
    {
        if (auth()->guard('admin')->user()->level == 'master') {
            $banks = Bank::where('level', 'admin')->get();
            return view('backend.bank.bank', compact('banks'));
        } else {
            abort(404);
        }
    }

    public function edit($id, Request $request)
    {

        if (auth()->guard('admin')->user()->level == 'master') {
            $bank = Bank::find($id);
            $banks = Bank::where('level', 'admin')->get();
            return view('backend.bank.bank', compact('bank', 'banks'));
        } else {
            abort(404);
        }
    }

    public function create(Request $request)
    {
        if (auth()->guard('admin')->user()->level == 'master') {
            
            // 1. Validasi Input (Sangat disarankan)
            $request->validate([
                'nama_bank' => 'required',
                // Validasi input bernama 'file' agar hanya menerima gambar maksimal 2MB
                'file'  => 'required|image|mimes:jpeg,png,jpg,webp,gif|max:2048', 
                'nomor_rekening' => 'required',
                'nama_pemilik' => 'required',
                'type' => 'required',
                // tambahkan validasi lain sesuai kebutuhan...
            ]);

            $bank = new Bank();
            
            // 2. Siapkan variabel dan proses upload gambar
            $gambarPath = null;
    
            // Cek apakah ada file yang diunggah pada input bernama 'file'
            if ($request->hasFile('file')) {
                $gambarPath = $request->file('file')->store('bank', 'uploads');
            }
            
            $bank->icon = $gambarPath;
            $bank->nama_bank = e(strip_tags($request->nama_bank));
            $bank->nomor_rekening = e(strip_tags($request->nomor_rekening));
            $bank->nama_pemilik = e(strip_tags($request->nama_pemilik));
            $bank->type = e(strip_tags($request->type));
            $bank->id_user = auth()->guard('admin')->user()->id;
            $bank->level = 'admin';
            $bank->save();

            return back()->with('success', 'Bank Successfully added');
        } else {
            abort(404);
        }
    }

    public function update($id, Request $request)
    {
        if (auth()->guard('admin')->user()->level == 'master') {
            // 1. Validasi Input (Sangat disarankan)
            $request->validate([
                'nama_bank' => 'required',
                // Validasi input bernama 'file' agar hanya menerima gambar maksimal 2MB
                'file'  => 'nullable|image|mimes:jpeg,png,jpg,webp|max:2048', 
                'nomor_rekening' => 'required',
                'nama_pemilik' => 'required'
                // tambahkan validasi lain sesuai kebutuhan...
            ]);
            
            $bank = Bank::find($id);
            
            // 2. Siapkan variabel dan proses upload gambar
            $gambarPath = $bank->icon;
    
            // Cek apakah ada file yang diunggah pada input bernama 'file'
            if ($request->hasFile('file')) {
                // Simpan file ke disk 'uploads' di dalam folder 'promosi'
                if ($bank->icon && Storage::disk('uploads')->exists($bank->icon)) {
                    Storage::disk('uploads')->delete($bank->icon);
                }
            
                $gambarPath = $request->file('file')->store('bank', 'uploads');
            }
            
            $bank->icon = $gambarPath;
            $bank->nama_bank = e(strip_tags($request->nama_bank));
            $bank->nomor_rekening = e(strip_tags($request->nomor_rekening));
            $bank->nama_pemilik = e(strip_tags($request->nama_pemilik));
            $bank->type = e(strip_tags($request->type));
            $bank->id_user = auth()->guard('admin')->user()->id;
            $bank->level = 'admin';
            $bank->save();

            return back()->with('success', 'Bank Successfully update');
        } else {
            abort(404);
        }
    }

    public function delete($id, Request $request)
    {

        if (auth()->guard('admin')->user()->level == 'master') {
            $bank = Bank::find($id);
            // Opsional: Cek jika data tidak ditemukan agar tidak error
            if (!$bank) {
                return back()->with('error', 'Data bank tidak ditemukan.');
            }

            // Cek apakah kolom gambar ada isinya dan file fisiknya ada di disk 'uploads'
            if ($bank->icon && Storage::disk('uploads')->exists($bank->icon)) {
                // Hapus file gambar dari folder
                Storage::disk('uploads')->delete($bank->icon);
            }
            
            $bank->delete();

            return back()->with('success', 'Bank Successfully deleted');
        } else {
            abort(404);
        }
    }
}
