<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\TemplateMaster;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use DB, Validator ,DataTables;


class TemplateMasterCategoryController extends Controller{
    /** construct */
        public function __construct(){
            $this->middleware('permission:template_master_category-create', ['only' => ['create']]);
            $this->middleware('permission:template_master_category-edit', ['only' => ['edit']]);
            $this->middleware('permission:template_master_category-view', ['only' => ['view']]);
            // $this->middleware('permission:template_master-delete', ['only' => ['delete']]);
        }
    /** construct */
    /** index */
        public function index(Request $request){
            if($request->ajax()){
                $data = TemplateMaster::orderBy('id' , 'desc')->get();
                
                return Datatables::of($data)
                        ->addIndexColumn()
                        ->addColumn('action', function($data){
                            $return = '<div class="btn-group">';

                            if(auth()->user()->can('template_master-view')){
                                $return .= '<a href="'.route('template_master.view', ['id' => base64_encode($data->id)]).'" class="btn btn-default btn-xs">
                                                <i class="fa fa-eye"></i>
                                            </a> &nbsp;';
                            }  
                            
                            if(auth()->user()->can('template_master-edit')){
                                $return .= '<a href="'.route('template_master.edit', ['id' => base64_encode($data->id)]).'" class="btn btn-default btn-xs">
                                                <i class="fa fa-pencil"></i>
                                            </a> &nbsp;';
                            }   

                            /*if (auth()->user()->can('template_master-delete')) {
                                $return .= '<a href="javascript:;" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown">
                                                    <i class="fa fa-bars"></i>
                                                </a> &nbsp;
                                                <ul class="dropdown-menu">
                                                    <li><a class="dropdown-item" href="javascript:;" onclick="change_status(this);" data-status="active" data-id="' . base64_encode($data->id) . '">Active</a></li>
                                                    <li><a class="dropdown-item" href="javascript:;" onclick="change_status(this);" data-status="inactive" data-id="' . base64_encode($data->id) . '">Inactive</a></li>
                                                    <li><a class="dropdown-item" href="javascript:;" onclick="change_status(this);" data-status="deleted" data-id="' . base64_encode($data->id) . '">Delete</a></li>
                                                </ul>';
                            }*/

                            $return .= '</div>';

                            return $return;
                        })

                     
                        ->editColumn('status', function ($data) {
                            if ($data->status == 'active') {
                                return '<span class="badge badge-pill badge-success">Active</span>';
                            } else if ($data->status == 'inactive') {
                                return '<span class="badge badge-pill badge-warning">Inactive</span>';
                            } else if ($data->status == 'deleted') {
                                return '<span class="badge badge-pill badge-danger">Deleted</span>';
                            }else{
                                return '-';
                            }
                        })

                        ->rawColumns([ 'action' ,'status'])
                        ->make(true);
            }
            return view('template_master.index');
        }
    /** index */
    /** create */
        public function create()
        {
            return view('template_master.create');
        }

   /** create */
   /** insert */
        public function insert(Request $request){
            if($request->ajax()) { return true; }

            /*if(!empty($request->template_name)){
                $crud['template_name'] = $request->template_name;
            }*/

            $crud = [
                'template_name' => $request->template_name,
                'template_for' => $request->template_for,
                'template_data' => $request->template_data,
                'status' => 'active',
                'created_at' => date('Y-m-d H:i:s'),
                'created_by' => auth()->user()->id
            ];

            $last_id = TemplateMaster::insertGetId($crud);

            if ($last_id)
            return redirect()->route('template_master')->with('success', 'Record inserted successfully');
            else
                return redirect()->back()->with('error', 'Failed to insert record')->withInput();
            
            }
    /** insert */
    /** view */
        public function view(Request $request){
          
                $id = base64_decode($request->id);               
                    
                $data = TemplateMaster::where(['id' => $id])->first();
                
                                    
                return view('template_master.view')->with(['data' =>$data]);
        }
    /** view */
   /** Edit */
        public function edit(Request $request){
            $id = base64_decode($request->id);
            $data = TemplateMaster::where(['id' => $id])->first();
            return view('template_master.edit')->with(['data' => $data]);
        }
    /** Edit */

    /** update */
        public function update(Request $request){
            if($request->ajax()){ return true; }
            $id = $request->id;
            
            $crud = [
               'template_name' => $request->template_name,
                'template_for' => $request->template_for,
                'template_data' => $request->template_data,      
                'updated_at' => date('Y-m-d H:i:s'),
                'updated_by' => auth()->user()->id
            ];


           DB::enableQueryLog();
            $update = TemplateMaster::where(['id' => $id])->update($crud);
           ;
            if ($update){
                return redirect()->route('template_master')->with(['success' => 'Record updated successfully']);
            }else{
                return redirect()->back()->with(['error'=>'Faild to update Record!'])->withInput();
            }
        }
    /** update */

    /** change-status */
        public function change_status(Request $request){
            if (!$request->ajax()) { exit('No direct script access allowed'); }

            $id = base64_decode($request->id);
            $data = TemplateMaster::where(['id' => $id])->first();

            if(!empty($data)){
                $update = TemplateMaster::where(['id' => $id])->update(['status' => $request->status, 'updated_at' => date('Y-m-d H:i:s'), 'updated_by' => auth('sanctum')->user()->id]);
                if($update){
                    return response()->json(['code' => 200]);
                }else{
                    return response()->json(['code' => 201]);
                }
            }else{
                return response()->json(['code' => 201]);
            }
        }
    /** change-status */
}
