<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\ReportLog;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ReportLogController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ReportLog(), function (Grid $grid) {
            $grid->model()->with(['user:id,name,avatar,company_name','product:id,name,image','report:id,title']);
            $grid->column('id')->sortable();
            $grid->column('user_id')->display(function (){
                $str = "";
                if(!empty($this->user)){
                    $str .= "<div style='margin-right:10px;display: flex;align-items: center'>";
                    $str .= '<img data-action="preview-img" src="' . $this->user->avatar . '" style="height:50px;width:50px;cursor:pointer;margin-right:10px;" class="img img-thumbnail">';
                    $str .= '<div>';
                    $str .= '<p style="margin-bottom: 5px">' . $this->user->name . '</p>';
                    $str .= "</div>";
                    $str .= "</div>";
                }
                return $str;
            });
            $grid->column('product_id')->display(function (){
                $str = "";
                if(!empty($this->product)){
                    $str .= "<div style='margin-right:10px;display: flex;align-items: center'>";
                    $str .= '<img data-action="preview-img" src="' . $this->product->image . '" style="height:50px;width:50px;cursor:pointer;margin-right:10px;" class="img img-thumbnail">';
                    $str .= '<div>';
                    $str .= '<p style="margin-bottom: 5px">' . $this->product->name . '</p>';
                    $str .= "</div>";
                    $str .= "</div>";
                }
                return $str;
            });
            $grid->column('report_id')->display(function (){
                return $this->report->title;
            });
            $grid->column('created_at');
            $grid->disableCreateButton();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->like('id')->width(4);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ReportLog(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('product_id');
            $show->field('report_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ReportLog(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('product_id');
            $form->text('report_id');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
