<?php

namespace App\Distributor\Controllers;

use App\Distributor\Repositories\DistCustomMessage;
use App\Distributor\Repositories\DistMessage;
use App\Distributor\Repositories\DistReadStatus;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;

class DistMessageController extends AdminController
{
    public function title()
    {
        return admin_trans('admin.messages');

    }
    /**
     * page index
     */
    public function index(Content $content)
    {



        return $content

            ->header(admin_trans( 'admin.messages'))
            ->description(admin_trans('admin.all'))
            ->breadcrumb(['text'=>'list','url'=>''])
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {

        return Grid::make(new DistCustomMessage(), function (Grid $grid)  {


//            $grid->setActionClass(Grid\Displayers\Actions::class);
//            // 添加“查看”按钮
//            $grid->actions(function (Grid\Displayers\Actions $actions) {
//                $actions->append('<a href="/dist/messages/' . $actions->row->id . '" class="btn btn-sm btn-primary">View Detail</a>');
//            });

            $grid->column('created_at',admin_trans_field('time'));

            $grid->column('is_read', admin_trans_field('is_read'))->display(function ($isRead) {
                $text = $isRead == 1 ? admin_trans_label('read') : "<strong>".admin_trans_label('unread')."</strong>";
                return $text;
            });

            $grid->column('title',admin_trans_field('message_title'))->display(function ($title) {
                $isRead = $this->is_read;
                $style = $isRead == 0 ? 'font-weight: bold;' : '';
                $label = $isRead == 0 ? ' (Unread) ' : '';
                return "<span style='{$style}'>{$title}</span>";
            });
            $grid->column('content');


            $grid->column('custom_column', admin_trans('admin.action'))->display(function () {
                return view('admin.grid.actions-view', ['id' => $this->id,'text'=>admin_trans('admin.view')]);
            });

            $grid->disableCreateButton();
            $grid->disableDeleteButton();
            $grid->disableEditButton();
            $grid->disableActions();

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {

        if(!DistCustomMessage::canViewMessage($id))
        {
            abort('403');
        };

        $this->markAsRead($id);
        return Show::make($id, new DistMessage(), function (Show $show) {
            //$show->field('id');
            $show->field('created_at',admin_trans_field('time'));
            $show->field('title');
            $show->field('content');
            // Disable all toolbar actions and buttons
            $show->panel()
                ->tools(function (Show\Tools $tools) {
                    $tools->disableEdit();   // Disable edit button
                    $tools->disableDelete(); // Disable delete button
                });
        });
    }
//
//    /**
//     * Make a form builder.
//     *
//     * @return Form
//     */
//    protected function form()
//    {
//        return Form::make(new DistMessage(), function (Form $form) {
//            $form->display('id');
//            $form->text('title');
//            $form->text('content');
//            $form->text('sender_id');
//            $form->text('target_type');
//            $form->text('target_ids');
//
//            $form->display('created_at');
//            $form->display('updated_at');
//        });
//    }


    /**
     * 标记消息为已读
     */
    public function markAsRead($messageId)
    {
        $userId = getDistributorId();


        if (DistReadStatus::markAsRead($messageId, $userId)) {
            return true;
        }

        return false;
    }


}
