<?php

namespace Modules\Laporan\Repositories;

use App\Models\Report;
use Illuminate\Support\Str;
use App\Models\ReportCategory;
use Yajra\DataTables\DataTables;
use App\Repositories\BaseRepository;
use Modules\Laporan\Entities\Laporan;
use Modules\Laporan\Repositories\LaporanRepositoryInterface;

class LaporanRepository extends BaseRepository implements LaporanRepositoryInterface
{
    protected $table = 'LaporanTable';

    public function __construct(Report $model)
    {
        $this->mainModel = $model;
    }

    public function datatable()
    {
        $data = $this->mainModel->orderBy('id', 'desc');

        return DataTables::of($data)
                ->addIndexColumn()
            ->editColumn('id', fn($row) => encryptString($row->id))
            ->editColumn('tittle', fn($row) => Str::limit($row->tittle, 30))
            ->addColumn('status', function ($row) {
                if ($row->status == 'publish') {
                    return '<span class="badge bg-success">' . __('lang.publish') . '</span>';
                } else {
                    return '<span class="badge bg-warning">' . __('lang.draft') . '</span>';
                }
            })
            ->editColumn('report_category', fn($row) => $row->ReportCategory->name)
            ->addColumn('publish_date', function ($row) {
                return $row->publish_date ? fullSimpleDate($row->publish_date) : '-';
            })
            ->addColumn('date_perubahan', function ($row) {
                return $row->updated_at ? fullSimpleDate($row->updated_at) : '-';
            })
                ->addColumn('action', function($row) {
                    return generateActionDropdown([
                        [
                            'url' => route('laporan.edit', ['id' => encryptString($row->id),]),
                            'label' => '<i class="bx bx-edit me-2"></i></i>' . __('lang.edit'),
                        ],
                        [
                            'class' => 'delete-action',
                            'attributes' => [
                                'data-url' => route('laporan.destroy', encryptString($row->id)),
                                'data-title' => __('lang.delete_data', ['data' => $row->name,]),
                                'data-table' => $this->table,
                            ],
                            'label' => '<i class="bx bx-trash me-2"></i>' . __('lang.delete'),
                        ]
                    ]);
                })
                ->rawColumns(['status','publish_date','date_perubahan','action'])
                ->make('true');
    }

    public function datatableLaporanCategory()
    {
        $data = ReportCategory::orderBy('id', 'desc')->get();

        return DataTables::of($data)
                ->addIndexColumn()
                ->editColumn('id', fn($row) => encryptString($row->id))
                ->editColumn('name', fn($row) => $row->name)
                ->addColumn('status', function ($row) {
                    if ($row->status == 'Y') {
                        return '<span class="badge bg-success">' . __('lang.yes') . '</span>';
                    } else {
                        return '<span class="badge bg-warning">' . __('lang.no') . '</span>';
                    }
                })
                ->addColumn('action', function($row) {
                    $btn = [
                        [
                            'class' => 'call-modal',
                            'attributes' => [
                                'data-url' => route('laporan.editReportCategory', ['id' => encryptString($row->id),]),
                                'data-title' => __('lang.edit_data', ['data' => __('lang.category'), 'value' => $row->name,]),
                                'data-table' => $this->table,
                            ],
                            'label' => '<i class="bx bx-edit me-2"></i>' . __('lang.edit'),
                        ],
                        [
                            'class' => 'delete-action',
                            'attributes' => [
                                'data-url' => route('laporan.destroyReportCategory', encryptString($row->id)),
                                'data-title' => __('lang.delete_data', ['data' => $row->name,]),
                                'data-table' => $this->table,
                            ],
                            'label' => '<i class="bx bx-trash me-2"></i>' . __('lang.delete'),
                        ]
                    ];

                    return generateActionDropdown($btn);
                })
                ->rawColumns(['status','action'])
                ->make('true');
    }

}
