<?php

namespace App\DataTables\Admin;

use App\Helper\Util;
use App\Models\Thread;
use Yajra\DataTables\Services\DataTable;
use Yajra\DataTables\EloquentDataTable;

/**
 * Class ThreadDataTable
 * @package App\DataTables\Admin
 */
class ThreadDataTable extends DataTable
{
    /**
     * Build DataTable class.
     *
     * @param mixed $query Results from query() method.
     * @return \Yajra\DataTables\DataTableAbstract
     */
    public function dataTable($query)
    {
        $query     = $query->with(['user', 'production_user']);
        $dataTable = new EloquentDataTable($query);
        $dataTable->editColumn('comments', function (Thread $model) {
            return str_limit(strip_tags($model->comments), 50);
        });
        $dataTable->editColumn('user.name', function (Thread $model) {
            return $model->user->name ?? '';
        });
        $dataTable->editColumn('Production.name', function (Thread $model) {
            return $model->production_user->name ?? '';
        });

        $dataTable->rawColumns(['action', 'comments']);
        return $dataTable->addColumn('action', 'admin.threads.datatables_actions');
    }

    /**
     * Get query source of dataTable.
     *
     * @param \App\Models\Thread $model
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query(Thread $model)
    {
        $model = $model->newQuery();
        if ($this->order_id != null) {
            $model = $model->where('order_id', $this->order_id);
        }
        return $model->orderBy('id', 'desc');
    }

    /**
     * Optional method if you want to use html builder.
     *
     * @return \Yajra\DataTables\Html\Builder
     */
    public function html()
    {
        $buttons = [];
        if (\Entrust::can('threads.create') || \Entrust::hasRole('super-admin')) {
            $buttons = ['create'];
        }
        $buttons = array_merge($buttons, [
//            'export',
//            'print',
//            'reset',
//            'reload',
        ]);
        return $this->builder()
            ->columns($this->getColumns())
            ->minifiedAjax()
            ->addAction(['width' => '80px', 'printable' => false])
            ->parameters(array_merge(Util::getDataTableParams(), [
                'dom'     => 'Blfrtip',
                'order'   => [[0, 'desc']],
                'buttons' => $buttons,
                'stateSave' => true,
            ]));
    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            'order_id',
            'user.name'       => ['title' => 'Sale User Name', 'searchable' => true],
            'Production.name' => ['title' => 'Production User Name', 'searchable' => true],
            'comments'        => ['title' => 'Comments', 'searchable' => true, 'width' => '50%'],
//            'attachment'
        ];
    }

    /**
     * Get filename for export.
     *
     * @return string
     */
    protected function filename()
    {
        return 'threadsdatatable_' . time();
    }
}