<?php

namespace App\Http\Controllers\Management;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use App\Models\Master\Group;

class RoleController extends Controller
{
    protected $title = 'Role';
    protected $template = 'modules.management.role';
    protected $route = 'modules.management.role';

    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        permission('is_read', $this->route, 'module',true);
        $data['breadcrumbs'] = [
            ['name' => 'Dashboard','url' => url('dashboard')],
            ['name' => 'Management & Akses Role'],
            ['name' => 'Data Role User','active' => true],
        ];
        $data['title'] = $this->title;
        $data['route'] = $this->route;
        return view($this->template.'.index',$data);
    }

    public function grid(Request $request)
    {

        $data = Group::where('MsGroupId','!=',1)->orderBy('MsGroupId','DESC')->get();
        $_data = [];


        foreach ($data as $key => $row) {


            $action = '';

            if((permission('is_create', $this->route.'.*','module',false)) || (permission('is_update', $this->route.'.*','module',false))){
                $action .= '<div class="flex gap-1 justify-center items-center flex-row">';
                $action .= '<a href="'.url('/management/role/akses/'.encode_id($row->MsGroupId)).'/edit" data-toggle="tooltip" title="Edit Hak Akses Role" class="p-2 rounded bg-blue-500"><i class="ri-list-settings-line text-white"></i></a>';
                $action .= '<a href="'.url('/management/role/update/'.encode_id($row->MsGroupId)).'" data-toggle="tooltip" title="Edit Data" class="p-2 rounded bg-emerald-500"><i class="ri-pencil-line text-white"></i></a>';
                // $action .= '<a data-toggle="tooltip" title="Hapus Data" class="btn btn-xs bg-danger"><i class="ri-trash-line text-white"></i></a>';
                $action .= '<a href="#" data-href="'.url('management/role/delete/'.encode_id($row->MsGroupId)).'" data-toggle="tooltip" title="Hapus Data" class="remove_data p-2 rounded bg-red-500"><i class="ri-delete-bin-line text-white"></i></a>';
                $action .= '</div>';
            }
            
           $_data[] = [
            'no'                => $key+1,
            'id'                => encode_id($row->id),
            'name'              => @$row->name,
            'action'            => @$action,
        ];

        }

        // return response()->json($_data);  // Return the data as a JSON response
        return response()->json($_data);

    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $data['breadcrumbs'] = [
            ['name' => 'Dashboard','url' => url('dashboard')],
            ['name' => 'Management & Akses Role'],
            ['name' => 'Data Role User','active' => true],
        ];

        $data['title'] = $this->title;
        $data['route'] = $this->route;
        return view($this->template.'.form',$data);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        try {

            $keyId = decode_id($request->secure_id);

                Validator::make($request->all(), [
                    'name'            => 'required',
                    'alias'           => 'required',
                ])->validate();

            if(@$keyId){
                $group          = Group::find($keyId);
                $group->name    = $request->name;
                $group->alias   = $request->alias;
                $group->save();
            }else{
                $group = new Group;
                $group->name        = $request->name;
                $group->alias       = $request->alias;
                $group->created_by  = auth()->user()->id;
                $group->status      = true;
                $group->save();
            }

            return redirect()->back()->with([
                'message' => 'Berhasil update data',
                'type'    => 'success',
            ]);

        }catch (Exception $e) {
            return redirect()->back()->with([
                     'message' => $e->getMessage(),
                     'type'    => "error"
                ]);
        }
    }

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

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update($id = null)
    {
        $data['breadcrumbs'] = [
            ['name' => 'Dashboard','url' => url('dashboard')],
            ['name' => 'Management & Akses Role'],
            ['name' => 'Data Role User','active' => true],
        ];
        $keyId = decode_id($id);
        $data['title'] = $this->title;
        $data['route'] = $this->route;
        $data['keyId'] = $id;
        $data['item'] = Group::find($keyId);
        return view($this->template.'.form',$data);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        //
    }

    public function delete($id)
    {
        $keyId = decode_id($id);

        $group = Group::where('MsGroupId',$keyId)->delete();

        return response()->json(['success' => true,'message' => 'Berhasil update data','type' => 'success']);
    }
}
