<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Models\User;

class UserController extends Controller
{
    var $paginate = 10;

    public function index(Request $request)
    {
        if($request->page==''){
            $users = User::paginate($this->paginate);
            return redirect('user?page='.$queries->lastPage());
        }else{
            $users = User::paginate($this->paginate);
            return view('admin/user',['users'=>$users,'title'=>'User Management']);
        }
    }
    
    public function show(Request $request){
        $user = User::where('id',$request->id)->first();
        return response()->json(
            ['error'=>false,'data'=>$user]
        );
    }
    
    public function generate(): string
    {
        $codeLength = 10;
        $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $code = '';

        $code = substr(str_shuffle(str_repeat($characters, 5)), 0, $codeLength);
        return $this->checkAndRegenerate($code);
    }

    private function checkAndRegenerate(string $code): string
    {
        /*
        if (User::where('referral_code', $code)->exists()) {
            return $this->checkAndRegenerate($this->generate());
        }*/

        return $code;
    }
    
    /*    
    public function store(Request $request)
    {
        if($request->id === '' || $request->id === null)
        {
            return $this->insert($request);
        }
        else
        {
            return $this->update($request);
        }
    }

    public function insert(Request $request)
    {
        if($request->hasFile('photo'))
        {
            $file = $request->file('photo');
            $fileName = $file->getClientOriginalName();

            $file->move('imgs/care',$fileName);

            $values = array(
                'name'=>$request->name,
                'category'=>$request->category,
                'img'=>$fileName
            );

            $insertedId = Care::insertGetId($values);
            $values['id'] = $insertedId;
            $values['img'] = $fileName;

            return response()->json([
                'success' => true,
                'message' => "Insert data berhasil!",
                'data' => $values,
            ]);
    
        }else{
            return response()->json([
                'success' => false,
                'message' => "Insert data gagal!",
            ]);
        }
    }
    
    public function update(Request $request)
    {
        $fileName = '';
        $values = array(
            'id'=>$request->id,
            'name'=>$request->name,
            'category'=>$request->category
        );

        if($request->hasFile('photo')){
            $file = $request->file('photo');
            $fileName = $file->getClientOriginalName();
            $file->move('imgs/care',$fileName);
            $values['img'] = $fileName;
        }else{
            $values['img'] = $request->image_file;
        }

        Care::where('id',$request->id)->update($values);

        return response()->json([
            'success' => true,
            'message' => 'Data Berhasil Diperbaharui',
            'data'    => $values
        ]);
    }
    
    
    public function destroy(Request $request)
    {
        $careItems = CareItem::where('product_id','=',$request->id)->get();

        if(count($careItems) > 0){
            return response()->json([
                'success' => false,
                'message' => 'Hapus Data GAGAL!, Silahkan hapus produk item terlebih dahulu.'
            ]);
        }else{
            Care::where('id',$request->id)->delete();
            return response()->json([
                'success' => true,
                'message' => 'Hapus Data Berhasil',
            ]);
        }


    }*/
    
    
}

?>