<?php

namespace App\Repositories\Webprofile;

use App\Models\Webprofile\Slider;
use App\Repositories\StorageRepository;
use Illuminate\Support\Str;
use DataTables;
use Storage;

class SliderRepository extends StorageRepository
{
    protected $model;

    public function __construct(Slider $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)
    {
        $setting = webprofilesetting();

        return DataTables::of($data)
            ->addIndexColumn()
            ->addColumn('action', function ($row) {
                $btn = '<a href="' . url('/webprofile/sliders/' . $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('slider', function ($row) use ($setting) {
                if ($setting['external_storage'] == 1) {
                    $str = '<img src="'. $setting['url_static'] . '/' . $setting['directory'] . '/slider/' . $row->slider . '" height="100" width="auto">';
                } else {
                    $str = '<img src="'. url('/storage/slider/' . $row->slider) . '" height="100" width="auto">';
                }

                return $str;
            })
            ->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', 'slider'])
            ->make(true);
    }
}
