<?php

namespace App\Http\Controllers\Master;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use App\Models\Master\Template;
use Illuminate\Http\Request;

class ResourceController extends Controller
{
    protected $title = 'Data Resource';
    protected $template = 'modules.master.resource';
    protected $route = 'modules.master.resource';

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

    public function grid(Request $request)
    {

        $data = Template::orderBy('MsTemplateId','ASC')->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-3 justify-center items-center flex-row">';
                $action .= '<a href="'.url('master/resource/update/'.encode_id($row->MsTemplateId)).'" data-toggle="tooltip" title="Edit Data" class="btn btn-sm btn-block bg-primary"><i class="ri-pencil-line text-white"></i></a>';
                $action .= '</div>';
            }

            if($row->show_dashboard == 0){
                $radio = '<input data-key="'.encode_id($row->MsTemplateId).'" class="showDashboard" type="checkbox" name="show_dashboard">';
            }else{
                $radio = '<input data-key="'.encode_id($row->MsTemplateId).'" class="showDashboard" type="checkbox" name="show_dashboard" checked>';
            }
            
           $_data[] = [
            'no'                => $key+1,
            'id'                => encode_id($row->MsTemplateId),
            'name'              => @$row->name,
            'show_dashboard'    => $radio,
            '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()
    {
        permission('is_read', $this->route, 'module',true);
        
        $data['breadcrumbs'] = [
            ['name' => 'Dashboard','url' => url('dashboard')],
            ['name' => 'Master Data'],
            ['name' => 'Data Resource','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);

            if(@$keyId){
                Validator::make($request->all(), [
                    'name'            => 'required',
                ])->validate();

                $user = Template::find($keyId);
                $user->name = $request->name;
                $user->save();
            }else{
                Validator::make($request->all(), [
                    'name'            => 'required',
                ])->validate();

                $user = new Template;
                $user->name = $request->name;
                $user->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' => 'Master Data'],
            ['name' => 'Data Topik','active' => true],
        ];
        $keyId = decode_id($id);
        $data['title'] = $this->title;
        $data['route'] = $this->route;
        $data['keyId'] = $id;
        $data['item'] = Template::where('MsTemplateId',$keyId)->first();
        return view($this->template.'.form',$data);
    }

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

    function changeShowDashboard(Request $request){
        try {
            // dd($request->all());
            $keyId = decode_id($request->id);
            $template = Template::where('MsTemplateId',$keyId)->first();
            // dd($template);
            $template->show_dashboard = ($request->val == 'true' ? '1' : '0');
            $template->save();
            return response()->json(['data' => $template,'status' => true]);
        } catch (\Throwable $th) {
            return response()->json(['status' => false]);
        }
        
    }
}
