<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use App\Exports\UserExport;
use App\Imports\UserImport;
use App\Exports\UserTemplateExport;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\Validator;

class DataUserController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function userexport(){
        return Excel::download(new UserExport,'data-user.xlsx');
    }

    public function userimport(Request $request)
    {
        $request->validate([
            'file' => 'required|file|mimes:xlsx',
        ]);

        $file = $request->file('file');
        $nameFile = $file->getClientOriginalName();
        $file->move('DataUser', $nameFile);

        Excel::import(new UserImport, public_path('/DataUser/'.$nameFile));
        return redirect('user')->with('success', 'Data Berhasil Di Import!');
    }

    public function downloadUserTemplate()
    {
        return Excel::download(new UserTemplateExport, 'data-user-template.xlsx');
    }

     public function index(Request $request)
    {
        if ($request->has('search')) {
            $users = User::where('name', 'LIKE', '%'.$request->search.'%')->paginate(10);
        } else {
            $users = User::paginate(10);
        }
        return view('user.user', compact('users'));
    }

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

    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->only('name', 'username', 'email', 'role'), [
            'name' => 'required',
            'username' => 'required',
            'email' => 'required',
            'role' => 'required',
        ]);

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

        $defaultPassword = 'man3bwi';

        User::create([
            'name' => $request->name,
            'username' => $request->username,
            'email' => $request->email,
            'password' => bcrypt($defaultPassword),
            'role' => $request->role,

        ]);

        return redirect()->route('user.index')->with('success', 'Data User Berhasil Ditambah!');
    }

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

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        $user = User::findOrFail($id);

        return view('user.edit', compact('user'));
    }

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

        // return redirect('user')->with('success', 'Data User Berhasil Diperbarui!');

        $validatedData = $request->validate([
            'name' => 'required',
            'username' => 'required',
            'email' => 'required',
            'role' => 'required',

        ]);

        $user = User::findOrFail($id);
        $user->update($validatedData);

        return redirect()->route('user.index')->with('success', 'Data User Berhasil Diperbarui!');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        $user = User::findorfail($id);
        $user->delete();

        return back()->with('info', 'Data User Berhasil Dihapus!');
    }
}
