<?php

namespace App\Repositories\Webprofile;

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

class CategoryRepository extends Repository
{
    protected $model;

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

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

                $btn = $btn . ' <a href="'.url('/webprofile/category/'.$row->id.'/editperbahasa').'" data-toggle="tooltip"  data-id="'.$row->id.'" data-original-title="'.trans('label.edittiapbahasa').'" class="edit btn btn-warning btn-round btn-sm edit">'.trans('label.edittiapbahasa').'</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;
            })
            ->addColumn('name', function ($row) {                
                $str = $row->name . '<br>';
                if ($row->rEn) {
                    $str .= '<i style="color: blue;">' . $row->rEn->name . '</i>';
                }
                
                return $str;
            })
            ->rawColumns(['action', 'status', 'name'])
            ->make(true);
    }
}
