<?php

namespace App\Http\Controllers\Admin\Transfer;

use App\Http\Controllers\Controller;
use App\Models\Bank;
use App\Models\SalesMaster;
use App\Service\Transfer\BankService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Inertia\Inertia;

class BankController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        return Inertia::render('Transfer/Bank');
    }

    public function view()
    {
        return response()->json(['bank' => Bank::orderByDesc('id')->where('shop_id', Auth::user()->shop_id)->get()]);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        BankService::BankStore(request()->all());

        return response()->json(['success' => 'Bank is added ', 'bank' => Bank::orderByDesc('id')->where('shop_id', Auth::user()->shop_id)->get()]);
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id) {}

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        BankService::BankUpdate(request()->all(), $id);

        return response()->json(['success' => 'Bank is Update ', 'bank' => Bank::orderByDesc('id')->where('shop_id', Auth::user()->shop_id)->get()]);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        $salse = SalesMaster::where('bank_id', $id)->first();
        if ($salse) {
            return response()->json(['error' => 'You can not delete this data'], 422);
        }
        Bank::where('id', $id)->delete();

        return response()->json(['success' => 'Data is Deleted', 'bank' => Bank::orderByDesc('id')->where('shop_id', Auth::user()->shop_id)->get()]);
    }
}
