<?php

namespace App\Distributor\Metrics;

use Dcat\Admin\Widgets\Metrics\Round;
use Illuminate\Http\Request;
use App\Distributor\Repositories\DistCustomMessage;

class DistMessage extends Round
{
    protected $limit = 10;
    /**
     * 初始化卡片内容
     */
    protected function init()
    {
        parent::init();

        // 卡片内容宽度
        $this->contentWidth(12, 0);
        $this->title(admin_trans( 'admin.messages')); // 标题'Messages');

    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {
        // 获取最新的10条消息


        $messages = (new DistCustomMessage())->getRecentMessages($this->limit);



        // 卡片内容
        $this->withContent($messages);
    }

    /**
     * 卡片内容.
     *
     * @param \Illuminate\Support\Collection $messages
     *
     * @return $this
     */
    public function withContent($messages)
    {
        $content = '';



        foreach ($messages as $message) {

            $isReadText = $message['is_read'] == 0 ? admin_trans_label('unread') : '';
            $isReadClass = $message['is_read'] == 0 ? 'font-weight-bold' : '';

            $content .= <<<HTML
<div class="chart-info d-flex justify-content-between mb-1">
    <div class="series-info d-flex align-items-center">
        <i class="fa fa-fw feather icon-message-circle"></i> <a href="/dist/messages/{$message['id']}"><span class="text-bold-600 ml-50 {$isReadClass}">{$message['title']}  ({$message['created_at']->toDateString()})</span>
        </a>
    </div>
    <div class="product-result">
        <span>{$isReadText}</span>
    </div>
</div>
HTML;
        }

        return $this->content(
            <<<HTML

<div class="col-12 d-flex flex-column flex-wrap text-center" style="">
    {$content}
</div>
HTML
        );
    }
}
