<?php

namespace App\Livewire;

use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Views\Column;
use Rappasoft\LaravelLivewireTables\Views\Columns\ButtonGroupColumn;
use Rappasoft\LaravelLivewireTables\Views\Columns\LinkColumn;
use Carbon\Carbon;
use App\Models\ActivityLog;
use App\Models\ActivityLog as ActivityLogModel;
use Livewire\Attributes\On;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\ActivitylogsExport;
use Illuminate\Database\Eloquent\Builder;
use Rappasoft\LaravelLivewireTables\Views\Filters\DateRangeFilter;
use Rappasoft\LaravelLivewireTables\Views\Filters\DateFilter;
use App\Exports\ActivityLogExport;

class ActivitylogTable extends DataTableComponent
{
	//protected $model = ActivityLog::class;
	
	public bool $columnSelect = true;
	
	public bool $rememberColumnSelection = false;
	
	
	public function configure(): void
    {
        $this->setPrimaryKey('id');
		//->setPaginationMethod('simple');
		
		$this->setBulkActionsEnabled();
		
		$this->setTableAttributes([
			'default' => false,
			'class' => 'table table-striped',
		  ]);

		$this->setEmptyMessage('No data available in table');
		
		$this->setSortingPillsStatus(true);
		
		$this->setSortingPillsItemAttributes([
			'class' => 'badge rounded-pill btn-kco-green d-inline-flex align-items-center', // Add these classes to the sorting pills item
			'default-colors' => false, // Do not output the default colors
			'default-styling' => false // Output the default styling
			]);
		
		  $this->setBulkActionsButtonAttributes([
			'class' => 'btn btn-kco-green dropdown-toggle',
			'default-colors' => false,
			'default-styling' => false,
			]);
			
		$this->setLoadingPlaceholderStatus(true);
		
		$this->setLoadingPlaceholderContent('Loading..');
		
		$this->setLoadingPlaceHolderRowAttributes([
        'class' => 'bg-light',
        'default' => true,
		]);
		
		$this->setLoadingPlaceHolderIconAttributes([
        'class' => 'lds-hourglass',
        'default' => false,
		]);
		
		$this->setTableWrapperAttributes([
			'default' => true,
			'class' => 'full-width-no-padding',
		  ]);
		  
		/*$this->setSearchFieldAttributes([
			'class' => 'block w-full form-control',
		]);*/
		
		$this->setperPageAccepted([20,50,100,500,1000,-1]);
		$this->setperPage(20);
		
		$this->setDefaultSort('created_at','desc');
    }
	
	public function builder(): Builder
		{
			$logs=ActivityLog::query();
			return $logs;
				//->with() // Eager load anything
				//->join() // Join some tables
				//->select('*'); // Select some things
		}
		
		public function filters(): array
		{
			return [
				DateRangeFilter::make('Activity Date'),
			  //DateFilter::make('Activity Date'),
			];
		}
	
	public function columns(): array
    {
        return [
            Column::make("Activity ID", "id")
			//->excludeFromColumnSelect() //hide from column select
			//->deselectedIf(!Auth::user())
			//->selectedIf(Auth::user())
				->hideIf(true) //hidden
                ->sortable(),
			Column::make("Activity Date", "created_at")
                ->sortable()
				->format(fn($row) => Carbon::parse($row)->format('d/m/Y, h:i A')),
			Column::make("Action", "event")
                ->sortable()
				->searchable()
				->collapseOnMobile(),
			Column::make("Table", "table_name")
                ->sortable()
				->searchable()
				->collapseOnMobile(),
			Column::make("Record ID", "subject_id")
                ->sortable()
				->searchable()
				->collapseOnMobile(),
			Column::make("User", "user.name")
                ->sortable()
				->searchable()
				->collapseOnMobile(),
			Column::make('Show')
			->label(
				function ($row, Column $column) {
					$show='';
					$show = '<span class="" data-bs-toggle="tooltip" data-bs-placement="top" alt="Show" data-bs-original-title="Show" role="button" wire:click="dispatch(\'showEdits\',{ \'id\':'.$row->id.'})"><i class="bx bx-show-alt bx-sm me-sm-1 txt-kco-green"></i></span>';
					return $show;
				}
			)->html(),
				
        ];
    }
	
	public function bulkActions(): array
	{
    return [
        'excel' => 'Export to Excel',
		'csv' => 'Export to CSV',
    ];
	}
 
	public function excel()
	{
    $activitylog = $this->getSelected();
	$recordcount=count($activitylog);
	if($recordcount==0){
		$this->dispatch('alert', [
				'type' => 'error',
				'message' => "Select at least one record to export!",
			]);
			return;
	}
    $this->clearSelected();
	$dt=date('d_m_Y_H_i_a');
    return Excel::download(new ActivityLogExport($activitylog), 'activitylog_export_'.$dt.'.xlsx');
	}
	
	public function csv()
	{
    $activitylog = $this->getSelected();
	$recordcount=count($activitylog);
	if($recordcount==0){
		$this->dispatch('alert', [
				'type' => 'error',
				'message' => "Select at least one record to export!",
			]);
			return;
	}
    $this->clearSelected();
	$dt=date('d_m_Y_H_i_a');
    return Excel::download(new ActivityLogExport($activitylog), 'activitylog_export_'.$dt.'.csv');
	}		
}
