<?php

namespace App\Repositories\Webprofile;

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

class PagesRepository extends Repository
{
    protected $model;

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

    public function get($with = null, $slug = null, $orderBy = null)
    {
        return $this->model
            ->when($with, function ($query) use ($with) {
                return $query->with($with);
            })
            ->when($slug, function ($query) use ($slug) {
                return $query->where('slug', $slug);
            })
            ->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/pages/' . $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('sum', function ($row) {
                $str = trans('label.viewer') . ' : ' . $row->viewer . '<br>';
                $str .= trans('label.command') . ' : ' . $row->comment_count;

                return $str;
            })
            ->rawColumns(['action', 'sum'])
            ->make(true);
    }
}
