<?php

namespace App\Repositories\Webprofile;

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

class DesignRepository extends Repository
{
    protected $model;

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

    public function get($with = null, $name_design = null, $orderBy = null)
    {
        return $this->model
            ->when($with, function ($query) use ($with) {
                return $query->with($with);
            })
            ->when($name_design, function ($query) use ($name_design) {
                return $query->where('name_design', 'ilike', '%' . $name_design . '%');
            })
            ->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);
    }
}
