<?php

namespace App\Repositories\Webprofile;

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

class SettingRepository extends Repository
{
    protected $model;

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

    public function get($with = null, $name_setting = null, $orderBy = null)
    {
        return $this->model
            ->when($with, function ($query) use ($with) {
                return $query->with($with);
            })
            ->when($name_setting, function ($query) use ($name_setting) {
                return $query->where('name_setting', 'ilike', '%' . $name_setting . '%');
            })
            ->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="javascript:void(0)" data-toggle="tooltip"  data-id="' . $row->id . '" data-original-title="Edit" class="edit btn btn-warning btn-round btn-sm edit">Edit</a>';

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

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

                return $btn;
            })
            ->addColumn('status', function ($row) {
                if ($row->is_active == 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;
            })
            ->rawColumns(['action', 'status'])
            ->make(true);
    }
}
