<?php

namespace App\Repositories\Webprofile;

use App\Models\Webprofile\Gallery;
use App\Repositories\StorageRepository;
use DataTables;

class GalleryRepository extends StorageRepository
{
    protected $model;

    public function __construct(Gallery $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/gallery/' . $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('gallery', function ($row) use ($setting) {
                if ($setting['external_storage'] == 1) {
                    $str = '<img src="' . $setting['url_static'] . '/' . $setting['directory'] . '/gallery/' . $row->gallery . '" height="100" width="auto">';
                } else {
                    $str = '<img src="' . url('/storage/gallery/' . $row->gallery) . '" 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', 'gallery'])
            ->make(true);
    }
}
