<?php

namespace App\Http\Controllers;

use App\DataTables\UsersDataTable;
use App\Http\Controllers\Controller;
use App\Models\Account;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Spatie\Permission\Models\Role;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(UsersDataTable $dataTable)
    {
        return $dataTable->render('users.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('users.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data=$this->validate($request,[
            'name'=>'required|string|max:191',
            'password'=>'required',
            'email'=>'required|email|unique:users',
            'phone'=>'required|numeric',
            'office_id'=>'required|unique:users',
            'image' => 'nullable|image',
            'locale' => 'nullable|string'
        ]);
        $data['status']=$request->status?true:false;
        if(isset($data['image'])){
            $data['image'] = time().'.'.$request->image->extension();  
            $request->image->move(public_path('uploads'), $data['image']);
        }

        $data['password']=bcrypt($request->password);
        $user = User::create($data);
        $user->assignRole('office manager');

        return redirect()->route('users.index')->with('success', __('Task was successful'));

    }

    /**
     * Display the specified resource.
     *
     * @param  \App\User  $user
     * @return \Illuminate\Http\Response
     */
    public function show(User $user)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Admin  $admin
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $user= User::find($id);
        return view('users.edit',['admin'=>$user]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Admin  $admin
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
       
        $admin= User::findOrFail($id);
        $data=$this->validate($request,[
            'name'=>'required|string|max:191',
            'email'=>'required|email|unique:users,email,'.$id,
            'phone'=>'required|numeric',
            'office_id'=>'unique:users,office_id,'.$id,
            'locale' => 'nullable|string',
            'image' => 'image',
        ]);
        $data['status']=$request->status?true:false;
        if($request->image){
            $data['image'] = time().'.'.$request->image->extension();  
            $request->image->move(public_path('uploads'), $data['image']);
            $this->destroyImage($admin->image);
        }
        if($request->password){
            $data['password']=bcrypt($request->password);
        }
        $admin->update($data);

        return redirect()->route('users.index')->with('success', __('Task was successful'));  
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\User  $admin
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $admin = User::find($id);
        $image_name = $admin->image;
        $admin->delete();
        $this->destroyImage($image_name);
        return ['status'=>'success','message'=>__('Task was successful')];
    }

    public function destroyImage($name)
    {
        $image_path = public_path() . '/uploads/' . $name;
        return @unlink($image_path);
    }
}
