<?php

namespace App\Http\Controllers\Master;

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

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

    /**
     * Display a listing of the resource.
     */
    public function index($id = null)
    {
        permission('is_read', $this->route, 'module',true);
        
        $data['breadcrumbs'] = [
            ['name' => 'Dashboard','url' => url('dashboard')],
            ['name' => 'Master Data'],
            ['name' => 'Tabel Data'],
            ['name' => 'Tabel Data Kolom','active' => true],
        ];

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

    public function grid(Request $request,$id = null)
    {

        $data = TabelDataKolom::where('ms_tabel_data_id',decode_id($id))->orderBy('order','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/kolom/'.encode_id($row->ms_tabel_data_id).'/update/'.encode_id($row->MsTabelDataKolomId).'').'" data-toggle="tooltip" title="Edit Data" class="btn btn-sm btn-block bg-primary"><i class="ri-pencil-line text-white"></i></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->MsTabelDataKolomId)).'" data-toggle="tooltip" title="Hapus Data" class="remove_data btn btn-sm btn-block bg-danger"><i class="ri-delete-bin-line text-white"></i></a>';
                }
                $action .= '</div>';
            }
            
           $_data[] = [
            'no'     => $key+1,
            'id'     => encode_id($row->MsTabelDataKolomId),
            'name'   => @$row->name,
            'key'    => @$row->key,
            'order'  => @$row->order,
            'action' => @$action,
        ];

        }

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

    }

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

        $keyId = decode_id($kolomId);
        $data['title'] = $this->title;
        $data['route'] = $this->route;
        $data['keyId'] = $kolomId;
        $data['tabelId'] = $id;
        $data['item'] = TabelDataKolom::where('MsTabelDataKolomId',$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)
    {
        if(@$request->secure_id){
            $item = TabelDataKolom::find(decode_id($request->secure_id));
            $item->name     = $request->name;
            $item->key      = $request->key;
            $item->order    = $request->order;
            $item->save();
        }else{
            $item = new TabelDataKolom;
            $item->ms_tabel_data_id = decode_id($request->tabelId);
            $item->name             = $request->name;
            $item->key              = $request->key;
            $item->order            = $request->order;
            $item->save();
        }

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

    /**
     * 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)
    {
        //
    }
}
