<?php

namespace App\Http\Controllers\Master;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Master\TabelData;
use App\Models\Master\TabelDataKolom;

class TabelDataController extends Controller
{
    protected $title = 'Tabel Data';
    protected $template = 'modules.master.tabel-data';
    protected $route = 'modules.master.tabel-data';

    /**
     * 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' => 'Tabel Data','active' => true],
        ];

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

    public function grid(Request $request)
    {

        $data = TabelData::orderBy('MsTabelDataId','ASC')->get();
        // $data = User::with(['group'])->orderBy('id','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-3 justify-center items-center flex-row">';
                $action .= '<a href="'.url('master/tabel-data/update/'.encode_id($row->MsTabelDataId)).'" data-toggle="tooltip" title="Edit Data" class="p-2 rounded bg-blue-500 text-white">Edit</a>';
                $action .= '<a href="'.url('master/tabel-data/kolom/'.encode_id($row->MsTabelDataId)).'" data-toggle="tooltip" title="View Data" class="p-2 rounded text-white bg-emerald-500">Detail</a>';
                if((session('group_id') == 1) || (session('group_alias') == 'admin') || (session('group_alias') == 'admin_ta') ){
                    // $action .= '<a href="#" data-href="'.url('master/table-data/delete/'.encode_id($row->MsTabelDataId)).'" data-toggle="tooltip" title="Hapus Data" class="remove_data p-2 rounded bg-danger"><i class="ri-delete-bin-line text-white"></i></a>';
                }
                $action .= '</div>';
            }
            
           $_data[] = [
            'no'     => $key+1,
            'id'     => encode_id($row->MsTabelDataId),
            'name'   => @$row->name,
            'key'    => @$row->key,
            'tahun'  => @$row->tahun,
            'nomor_tabel'  => @$row->nomor_tabel,
            'jml_kolom'  => TabelDataKolom::where('ms_tabel_data_id',$row->MsTabelDataId)->count(),
            'action' => @$action,
        ];

        }

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

    }

    public function update($id = null)
    {
        $data['breadcrumbs'] = [
            ['name' => 'Dashboard','url' => url('dashboard')],
            ['name' => 'Master Data'],
            ['name' => 'Tabel Data','active' => true],
        ];

        $keyId = decode_id($id);
        $data['title'] = $this->title;
        $data['route'] = $this->route;
        $data['keyId'] = $id;
        // $data['item'] = TabelData::where('MsTabelDataId',$keyId)->first();
        
        return view($this->template.'.form',$data);
    }

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

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

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

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

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