<?php

namespace App\Repositories\Webprofile;

use App\Models\Webprofile\Information;
use App\Repositories\Repository;
use DataTables;

class InformationRepository extends Repository
{
    protected $model;

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

    public function get($with = null, $title = null, $orderBy = null)
    {
        return $this->model
            ->when($with, function ($query) use ($with) {
                return $query->with($with);
            })
            ->when($title, function ($query) use ($title) {
                return $query->where('title', 'ilike', '%' . $title . '%');
            })
            ->when($orderBy, function ($query) use ($orderBy) {
                return $query->orderBy($orderBy[0], $orderBy[1]);
            })
            ->get();
    }

    public function datatable($data)
    {
        return DataTables::of($data)
            ->addIndexColumn()
            ->addColumn('action', function ($row) {
                $btn = '<a href="' . url('/webprofile/informations/' . $row->id . '/edit') . '" data-toggle="tooltip"  data-id="' . $row->id . '" data-original-title="' . trans('label.edit') . '" class="edit btn btn-warning btn-round btn-sm edit">' . trans('label.edit') . '</a>';

                $btn = $btn . ' <a href="javascript:void(0)" data-toggle="tooltip"  data-id="' . $row->id . '" data-original-title="' . trans('label.delete') . '" class="btn btn-danger btn-round btn-sm delete">' . trans('label.delete') . '</a>';

                $btn = $btn . '<br>';

                return $btn;
            })
            ->addColumn('date', function ($row) {
                $str = Date('d-m-Y', strtotime($row->event_date));

                return $str;
            })
            ->addColumn('status', function ($row) {
                if ($row->info_status == true) {
                    $str = '<div style="color: green;"><i class="fa fa-check"></i></div>';
                } else {
                    $str = '<div style="color: red;"><i class="fa fa-times"></i></div>';
                }
                return $str;
            })
            ->addColumn('title', function ($row) {
                $str = $row->title . '<br>';
                if ($row->rEn) {
                    $str .= '<i style="color: blue;">' . $row->rEn->title . '</i>';
                }

                return $str;
            })
            ->rawColumns(['action', 'status', 'date', 'title'])
            ->make(true);
    }
}
