<?php

namespace App\Admin\Controllers;

use App\Admin\Models\Feedback;
use App\Admin\Models\Tag;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\Admin\Selectable\SelectFeedback;
use App\Admin\Selectable\SeleteTag;
use Illuminate\Support\Facades\Auth;
use App\Admin\Actions\Post\StatusEdit;
use App\Admin\Actions\Post\Msg;
use App\Admin\Actions\Post\BatchStatus;
use App\Admin\Actions\Post\BatchMsg;
use Encore\Admin\Auth\Permission;
// use Encore\Admin\Auth\Admin;

class FeedbackController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'Feedback';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Feedback());
        $grid->column('id', __('Id'))->sortable();
        $grid->column('content', __('问题内容'));
        $grid->column('handler', __('提交人'));
        $grid->column('mobile', __('用户电话'));
        $grid->column('create_time', __('提交时间'));
        $grid->tags('问题标签')->display(function ($tags) {
            $res = array_map(function ($tag) {
                return "<span class='label label-success'>{$tag['name']}</span>";
            }, $tags);
            return join(' ', $res);
        });
        $grid->actions(function ($actions) {
            // 去掉删除操作
            $actions->disableDelete();
        });
        
        // 去掉批量删除
        $grid->batchActions(function ($batch) {
            $batch->disableDelete();
        }); 
        if (!Auth::guard('admin')->user()->can('status-edit')) {
            // 去除所有操作
            $grid->disableActions();
            // 标签式状态
            // $grid->column('status')->label(['0' => 'warning', '1' => 'success']);
            $grid->status(trans('状态'))->display(function ($status) {
                if ($status == 0) {
                    return "<span class='label bg-red'>未读</span>";
                } else if ($status == 1) {
                    return "<span class='label bg-green'>已读</span>";
                }
            });
            $grid->column('msg', __('回复内容'));
        }else{
            // 开关式状态
            $states = [
                'off' => ['value' => 0, 'text' => '未读', 'color' => 'primary'],
                'on' => ['value' => 1, 'text' => '已读', 'color' => 'success'],
            ];
            $grid->column('status', __('状态'))->switch($states);
            // 可行内编辑回复内容
            $grid->column('msg', __('回复内容'))->editable('textarea');
    
            // 自定义操作  修改状态
            $grid->actions(function ($actions) {
                // 添加修改状态操作
                $actions->add(new StatusEdit);
                // 添加回复操作
                $actions->add(new Msg);
            });
            // 添加批量操作  修改状态
            $grid->batchActions(function ($batch) {
                $batch->add(new BatchStatus);
            });
            // $grid->tools(function (Grid\Tools $tools) {
            //     $tools->append(new BatchStatus);
            // });
            // 添加批量操作  回复
            $grid->batchActions(function ($batch) {
                $batch->add(new BatchMsg);
            });
            // $grid->tools(function (Grid\Tools $tools) {
            //     $tools->append(new BatchMsg);
            // });
        }


        
        return $grid;
    }

    /**
     * Make a show builder.
     * 显示标签、状态??
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Feedback::findOrFail($id));
        // $show->field('id', __('Id'));
        $show->field('content', __('问题内容'));
        $show->field('handler', __('提交人'));
        $show->field('mobile', __('用户电话'));
        $show->field('create_time', __('提交时间'));
        $show->tag('tags')->label();
        $show->field('status')->as(function ($status) {
            if ($status == 1) {
                return "<span class='label label-success'>已读</span>";
            } else {
                return "<span class='label label-false'>未读</span>";
            }
        });
        // $show->tags('tags')->label(function ($tags) {
        //     $res = array_map(function ($tag) {
        //         return "<span class='label label-success'>{$tag['name']}</span>";
        //     }, $tags);
        //     return join(' ', $res);
        // });
        // $show->field('tags', __('问题标签'));
       
        // $show->field('status', __('状态'))->using([0 => '未读', 1 => '已读'])->label();
        $show->field('msg', __('回复'));

        return $show;
    }

    /**
     * Make a form builder.
     * 
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Feedback());
        // $form->text('tag', 'ceshi');
        // $tagForm = new Form(new Tag());
        // $grid->column('id', __('Id'));
        if ($form->isCreating()) {

            $user = Auth::guard('admin')->user(); //当前登录用户对象
            $form->text('content', __('问题内容'))->required();
            $form->hidden('handler', __('提交人'))->value($user->username);
            $form->text('num', __('车辆号/订单号'));
            $form->mobile('mobile', '用户电话')->required();
            $form->hidden('create_time', __('提交时间'))->value(date('Y-m-d H:i:s'));
            // 新增标签保存
            $form->tags('tags', __('问题标签'))->pluck('name', 'id')->options(Tag::all())->saving(function ($value) {
                foreach ($value as $key => $v) {
                    // 判断是否为字符串型整数 否新增  数字但tag表无相关数据 新增
                    if (!ctype_digit($v) || (ctype_digit($v) && !Tag::find($v))) {
                        $value[$key] = Tag::insertGetId(['name' => $v]);
                    }
                }
                return $value;
            });
            $form->hidden('status', __('状态'))->value(0);
        }
        if ($form->isEditing()) {
            $form->text('content', __('问题内容'))->readonly();
            $form->text('handler', __('提交人'))->readonly();
            $form->text('num', __('车辆号/订单号'))->readonly();
            $form->mobile('mobile', __('用户电话'))->readonly();
            $form->datetime('create_time', __('提交时间'))->readonly();
            // 新增标签保存
            $form->tags('tags', __('问题标签'))->pluck('name', 'id')->options(Tag::all())->saving(function ($value) {
                foreach ($value as $key => $v) {
                    // 判断是否为字符串型整数 否新增  数字但tag表无相关数据 新增
                    if (!ctype_digit($v) || (ctype_digit($v) && !Tag::find($v))) {
                        $value[$key] = Tag::insertGetId(['name' => $v]);
                    }
                }
                return $value;
            });
            $states = [
                'on' => ['value' => 1, 'text' => '已读', 'color' => 'success'],
                'off' => ['value' => 0, 'text' => '未读', 'color' => 'danger'],
            ];
            $form->switch('status', __('状态'))->states($states);
            $form->textarea('msg', __('回复内容'))->value(function ($value) {
                dd($value);
                if ($value) {
                    $form->hidden('status', __('状态'))->value(1);
                }
            });
        }

        $form->tools(function (Form\Tools $tools) {

            // 去掉`列表`按钮
            // $tools->disableList();
        
            // 去掉`删除`按钮
            $tools->disableDelete();
        
            // 去掉`查看`按钮
            $tools->disableView();

        });
        $form->footer(function ($footer) {

            // 去掉`重置`按钮
            $footer->disableReset();
        
            // 去掉`提交`按钮
            // $footer->disableSubmit();
        
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
        
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
        
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
            // 添加一个按钮, 参数可以是字符串, 或者实现了Renderable或Htmlable接口的对象实例
            // $footer->add('<a class="btn btn-sm btn-danger">标记为已读</a>');

        });

        return $form;
    }
}
