<?php

namespace App\Admin\Controllers;

use App\Models\TaskList;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use TencentCloud\Asr\V20190614\Models\Task;

class TaskListController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new TaskList(), function (Grid $grid) {
            $grid->model()->with('userData')->where('is_handpick',0)->orderByDesc('id');
            $grid->column('id')->sortable();
            $grid->column('userData.name','昵称');
            $grid->column('userData.mobile','手机号');
            $grid->column('userData.avatar','头像')->image('',40,40);
            $grid->column('title')->display(function ($initContent) {
                $maxLength = 10; // 你希望显示的最大字符数
                $ellipsis = '...'; // 省略号

                // 使用 mb_substr 截取字符串
                $displayContent = mb_substr($initContent, 0, $maxLength);

                // 如果原始字符串长度超过最大长度，添加省略号
                if (mb_strlen($initContent) > $maxLength) {
                    $displayContent .= $ellipsis;
                }

                return $displayContent;
            });;
//            $grid->column('role_id');
//            $grid->column('content');
            $grid->column('state')->using(TaskList::$state)
                ->dot(
                    [
                        0 => 'primary',
                        1 => 'secondary',
                        2 => 'danger',
                        3 => 'warning',
                        4 => 'info',
                        5 => 'light',
                        6 => 'success',
                    ],
                    'danger' // 第二个参数为默认值
                );;
//            $grid->column('image')->image('',80,80);
//            $grid->column('init_content')->width('300px')->display(function ($initContent) {
//                $maxLength = 10; // 你希望显示的最大字符数
//                $ellipsis = '...'; // 省略号
//
//                // 使用 mb_substr 截取字符串
//                $displayContent = mb_substr($initContent, 0, $maxLength);
//
//                // 如果原始字符串长度超过最大长度，添加省略号
//                if (mb_strlen($initContent) > $maxLength) {
//                    $displayContent .= $ellipsis;
//                }
//
//                return $displayContent;
//            });
//            $grid->column('keyword');
//            $grid->column('sd_image');
//            $grid->column('desc');
//            $grid->column('sd_id')->image('',80,80);
            $grid->column('surplus_diamond');
//            $grid->column('nickname');
//            $grid->column('plot');
            $grid->column('is_handpick')->switch();
            $grid->column('sort')->sortable();
            $grid->column('pdf_path')->display(function ($pdfPath) {
//                $downloadUrl = url($pdfPath);
                if (!empty($pdfPath)){
                    return "<a target='_blank' href='".$pdfPath."'>下载PDF</a>";
                }else{
                    return "还未生成PDF";
                }
            });
            $grid->column('image_path','图片地址')->display(function ($image_path) {
//                $downloadUrl = url($pdfPath);
                if (!empty($image_path)){
                    return "<a target='_blank' href='".$image_path."'>浏览图片</a>";
                }else{
                    return "还未生成图片";
                }
            });
//            $grid->column('pinyin_content');

            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
            $grid->disableViewButton();
            $grid->disableCreateButton();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->between('created_at')->datetime();
                $filter->equal('is_handpick','是否精选')->select([0 => '不是精选',1 => '精选']);
                $filter->where('mobile', function ($query) {
                    $query->whereHas('userData', function ($query) {
                        $query->where('mobile', 'like', "%{$this->input}%");
                    });
                }, '用户手机号');
                $filter->where('name', function ($query) {
                    $query->whereHas('userData', function ($query) {
                        $query->where('name', 'like', "%{$this->input}%");
                    });
                }, '用户昵称');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new TaskList(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('title');
            $show->field('role_id');
            $show->field('content');
            $show->field('state');
            $show->field('image');
            $show->field('init_content');
            $show->field('keyword');
            $show->field('sd_image');
            $show->field('desc');
            $show->field('sd_id');
            $show->field('surplus_diamond');
            $show->field('nickname');
            $show->field('plot');
            $show->field('is_handpick');
            $show->field('sort');
            $show->field('pinyin_content');
            $show->field('pdf_path');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new TaskList(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('title');
            $form->text('role_id');
            $form->text('content');
            $form->text('state');
            $form->text('image');
            $form->text('init_content');
            $form->text('keyword');
            $form->text('sd_image');
            $form->text('desc');
            $form->text('sd_id');
            $form->text('surplus_diamond');
            $form->text('nickname');
            $form->text('plot');
            $form->text('is_handpick');
            $form->text('sort');
            $form->text('pinyin_content');
            $form->text('pdf_path');

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