<?php

namespace App\Http\Controllers\Reseller;

use App\DataTables\GroupDataTable;
use App\DataTables\StanbicUserDataTable;
use App\Group;
use App\Http\Controllers\Controller;
use App\Permission;
use App\StanbicUser;
use App\User;
use Illuminate\Support\Facades\DB;

class StanbicUserController extends Controller
{

    public function index(StanbicUserDataTable $table)
    {
        return $table->render('reseller.stanbic_users.index');
    }


    public function create()
    {
        $groups = Group::orderBy('name')->get(['id', 'name']);
        return view('reseller.stanbic_users.create', compact('groups'));
    }


    public function store()
    {
        $this->validate(request(), [
            'username' => 'required|unique:users,username,NULL,id,deleted_at,NULL|unique:stanbic_users,username,NULL,id,deleted_at,NULL',
            'group_id' => 'required|exists:groups,id',
            'password' => 'required|confirmed'
        ]);
        $data = request()->all();

        $stanbicUser = User::whereUsername('stanbicgh')->firstOrFail();
        $data['user_id'] = $stanbicUser->id;
        StanbicUser::create($data);

        session()->flash('success', 'Stanbic User created successfully!');
        return redirect()->route('stanbic-users.index');
    }

    public function edit(StanbicUser $stanbic_user)
    {
        $groups = Group::orderBy('name')->get(['id', 'name']);
        $record = $stanbic_user;
        return view('reseller.stanbic_users.edit', compact('record', 'groups'));
    }

    public function reset(StanbicUser $stanbic_user) {
        $record = $stanbic_user;
        return view('reseller.stanbic_users.reset', compact('record'));
    }

    public function updatePassword(StanbicUser $stanbic_user) {
        $this->validate(request(), [
            'new_password' => 'required|confirmed',
        ]);
        $data = request()->only('new_password');

        $stanbic_user->password = $data['new_password'];
        $stanbic_user->save();

        session()->flash('success', 'Stanbic User password updated successfully!');
        return redirect()->route('stanbic-users.index');
    }

    public function lockUnlockUser(StanbicUser $stanbic_user) {
        $stanbic_user->is_locked = !$stanbic_user->is_locked;
        $stanbic_user->save();

        session()->flash('success', 'The user has been successfully '.($stanbic_user->is_locked?'locked':'unlocked'));
        return back();
    }

    public function show(StanbicUser $stanbic_user)
    {
        abort(404);
    }


    public function update(StanbicUser $stanbic_user)
    {
        $this->validate(request(), [
            'username' => 'required|unique:users,username,NULL,id,deleted_at,NULL|unique:stanbic_users,username,'.$stanbic_user->id.',id,deleted_at,NULL',
            'group_id' => 'required|exists:groups,id',
        ]);
        $data = request()->only(['username','group_id']);

        $stanbic_user->update($data);

        session()->flash('success', 'Stanbic User details updated successfully!');
        return redirect()->route('stanbic-users.index');
    }

    public function destroy(StanbicUser $stanbic_user)
    {
        $stanbic_user->delete();

        session()->flash('success', 'Stanbic User deleted successfully!');
        return back();
    }
}
