<?php

namespace App\Repositories\Webprofile;

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

class PostRepository extends Repository
{
    protected $model;

    public function __construct(Posts $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/posts/'.$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('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('category', function ($row) {
                $str = '';
                if ($row->rCategory) {
                    $str = $row->rCategory->name;
                }

                return $str;
            })
            ->addColumn('post_date', function ($row) {
                $str = '';

                if ($row->post_status == 1) {
                    $str .= '<div style="color: green;">'.trans('label.publish').'</div>';
                }

                if ($row->post_status == 0) {
                    $str .= '<div style="color: red;">'.trans('label.draft').'</div>';
                }

                $str .= date('d-m-Y', strtotime($row->post_date));

                return $str;
            })
            ->addColumn('sum', function ($row) {
                $str = trans('label.viewer').' : '.$row->viewer.'<br>';
                $str .= trans('label.command').' : '.$row->comment_count;

                return $str;
            })
            ->addColumn('title', function ($row) {
                $str = $row->title.'<br>';
                if ($row->rEn) {
                    $str .= '<i style="color: blue;">'.$row->rEn['title'].'</i>';
                }

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