<?php

namespace App\Admin\Controllers;

use App\Models\VipConfig;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class VipConfigController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new VipConfig(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title','会员名称');
            $grid->column('rights','VIP权益')->display(function ($res){
                $config = config("filesystems.disks.oss");
                $res = json_decode($res,true);
                $str = '<div style="display: flex">';
                foreach ($res as $k=>$v){
                    $v['img_url'] = "https://".$config['bucket'].'.'.$config['endpoint'].'/'.$v['img_url'];
                    $str.="<div style='margin-right:10px;text-align: center'>";
                    $str.='<img data-action="preview-img" src="'.$v['img_url'].'" style="max-width:50px;max-height:50px;cursor:pointer" class="img img-thumbnail">';
                    $str.='<p style="margin-top: 5px">'.$v['title'].'</p>';
                    $str.='<p style="margin-top: 5px">'.$v['desc'].'</p>';
                    $str.="</div>";
                }
                $str .= '</div>';
                return $str;

            });
            $grid->column('weixin','解锁微信（次）');
            $grid->column('chat','聊天人数（人）');
            $grid->column('dynamic','每天动态数量（条）');
            $grid->column('is_destroy','阅后即焚')->using([0=>'否',1=>'是'])->label(['gray','primary']);
            $grid->column('identity','身份标识')->using([0=>'无',1=>'有'])->label(['gray','primary']);
            $grid->column('invisible','隐身状态')->using([0=>'否',1=>'是'])->label(['gray','primary']);


            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });

            $grid->disableCreateButton();
            //操作管理
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableView();
                $actions->disableDelete();
            });

            //批量操作
            $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
                $batch->disableDelete();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new VipConfig(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('value');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new VipConfig(), function (Form $form) {
            $form->display('id');
            $form->text('title','名称');
            $form->number('weixin','解锁微信（次）');
            $form->number('chat','聊天人数（人）');
            $form->number('dynamic','每天动态数量（条）');
            $form->switch('is_destroy','阅后即焚')->default(1);
            $form->switch('identity','身份标识')->default(1);
            $form->switch('invisible','隐身状态')->default(1);
            $form->array('rights', function ($form) {
                $form->image('img_url','权益图片')->autoUpload();
                $form->image('big_img','大图')->autoUpload();
                $form->text('title','权益名称');
                $form->text('desc','说明');
            })->saveAsJson()->label('VIP权益');

        });
    }
}
