<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Inertia\Inertia;
use Illuminate\Support\Facades\DB;
use App\Models\Ledger;
use App\Models\LedgerCategory;
use App\Models\BankAccount;
use App\Models\SubBankAccount;
use App\Models\LastBankAccount;
use App\Models\MoreAccountBelow;
use App\Models\Beneficiary;
use App\Models\GeneralLedger;
use App\Traits\LogActivityTrait;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\Validator;
use App\Imports\ImportBeneficieriesExcelFile;
use App\Models\User;
use Auth;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Hash;


class UserController extends Controller
{

    use LogActivityTrait;

    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index() {

        $users = User::all();



        $data = array(
            'user' => Auth::user(),
            'users' => $users,
        );
        return view('users.index')->with($data);
    }


    public function createUser(Request $request) {

        //return $request->item_code;

        $validator = Validator::make($request->all(), [
            'firstname' => 'required',
            'lastname' => 'required',
            'email' => 'required|unique:users,email',
            'password' => 'required',

        ]);





        if ($validator->fails()) {
            return redirect()->back()->withInput()->withErrors($validator);
        }
        //Check if Account Exists

        User::create([
            'firstname' => $request->firstname,
            'lastname' => $request->lastname,
            'email' => $request->email,
            'password' => Hash::make($request->password)
        ]);



        return redirect('list/users')->with('message', 'Successfully Added User');
    }

    public function entryedit($id)
    {

        $id = Crypt::decrypt($id);

        $item = Beneficiary::find($id);

        $data = array(
            'user' => Auth::user(),
            'item' => $item,
        );
        return view('users.edit')->with($data);


    }

    public function entryupdate(Request $request)
    {


    $validator = Validator::make($request->all(), [
        'name' => 'required',
        'bank' => 'required',
        'account_number' => 'unique:beneficiaries,account_number,' . $request->id,
        'nin' => 'required',

    ]);



    if ($validator->fails()) {
        return redirect()->back()->withInput()->withErrors($validator);
    }

   // return 'hi';


    $item = Beneficiary::find( $request->input( 'id' ) );






        $item->name = ucfirst($request->name);
        $item->bank = $request->bank;
        $item->account_number = $request->account_number;
        $item->title = $request->title;
        $item->duty_station = $request->duty_station;
        $item->telephone_number = $request->telephone_number;
        $item->district = $request->district;
        $item->nin = $request->nin;


        $item->save();


    return redirect('/list/users')->with('success', 'Successfully Updated User ');


}



}
