<?php

namespace Modules\Task\Http\Controllers\Admin;

use App\Admin\Traits\WithUploads;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Illuminate\Support\MessageBag;
use Modules\Linker\Traits\WithLinker;
use Modules\Task\Models\Category;
use Modules\Task\Models\Task;
use Modules\User\Models\AccountRule;
use Modules\User\Models\Identity;
use Modules\User\Models\IdentityMiddle;

class TaskController extends AdminController
{

    use WithUploads, WithLinker;

    protected $title = '任务管理';

    public function grid(): Grid
    {
        $grid = new Grid(new Task());
        $grid->filter(function (Grid\Filter $filter) {
            $filter->column(1 / 3, function (Grid\Filter $filter) {
                $filter->like('title', '任务名称');
            });
            $filter->column(1 / 3, function (Grid\Filter $filter) {
                $filter->equal('category_id', '分类')->select(Category::shown()->pluck('title', 'id'));
            });
            $filter->column(1 / 3, function (Grid\Filter $filter) {
                $filter->equal('status', '状态')->select((new Task())->status_map);
            });
        });

        $grid->column('id', '#ID#');
        $grid->column('ico', '任务图标')->image('', 40, 40);
        $grid->column('cover', '图片')->image('', 40, 40);
        $grid->column('title', '任务名称');
        $grid->column('key', '任务关键字');
        $grid->column('category.title', '所属分类');
        $grid->column('rule.title', '账变名称');
        $grid->column('remark', '任务描述');
        $grid->column('tips', '任务奖励提示');
        $grid->column('type', '任务计量类型')->using(Task::TYPES);
        $grid->column('cycle', '任务计量周期')->using(Task::CYCLES);
//        $grid->column('company_certify', '企业认证')
//            ->using(Task::COMPANY_CERTIFICATIONS)
//            ->label(Task::COMPANY_CERTIFICATION_LABEL);

        $status = [
            'on'  => ['value' => 1, 'text' => '打开', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '关闭', 'color' => 'danger'],
        ];

//        $grid->column('展示位置')
//            ->display(function () {
//                $data = [];
//                foreach ($this->position as $position) {
//                    $data[] = $this->position_map[$position];
//                }
//
//                return $data;
//            })
//            ->label();

        $grid->column('身份')
            ->display(function () {
                return $this->getIdentities();
            })->label();
        $grid->column('status', '状态')->switch($status);

        $grid->column('created_at', '创建时间');
        $grid->column('updated_at', '更新时间');

        return $grid;
    }

    public function form(): Form
    {
        $form = new Form(new Task());


        $form->text('title', '任务标题')->required();
//        $form->text('sub_title', '副标题');
//        $form->text('remark', '任务描述');
        $form->text('tips', '任务奖励提示')->required();
        $form->text('key', '任务关键字')->required();
        $form->select('category_id', '所属分类')
            ->options(Category::query()->shown()->pluck('title', 'id'))
            ->required();
        $this->cover($form, 'ico', 'ICO图标');
        $this->cover($form, 'cover', '图片');
//        $form->multipleSelect('position', '展示位置')
//            ->options($form->model()->position_map);
        $form->multipleSelect('identity', '可用身份')
            ->options(Identity::pluck('name', 'id'));

        $form->select('rule_id', '对应账变规则')
            ->options(function () {
                return AccountRule::where('type', 'score')->pluck('title', 'id');
            })
            ->required();
        $form->currency('rule_number', '账变数值')
            ->default(0)
            ->help('0为获取账变设置的数值')
            ->required();

        $this->withUrl($form);

//        $form->radio('company_certify', '企业认证')
//            ->options(Task::COMPANY_CERTIFICATIONS)
//            ->default(Task::COMPANY_CERTIFICATION_NO)
//            ->required();
        $form->radio('type', '任务计量类型')->default(Task::TYPE_COUNT)->options(Task::TYPES)->required();
        $form->radio('cycle', '任务计量周期')->default(Task::CYCLE_DAY)->options(Task::CYCLES)->required();
        $form->number('task_number', '任务量')->default(1)->required();
//        $form->ueditor('description', '任务详情')->required();
        $form->switch('status', '是否开启')->states([
            'on'  => ['value' => 1, 'text' => '打开', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '关闭', 'color' => 'danger'],
        ]);
        $form->saving(function (Form $form) {
            $task = Task::query()->where('key', $form->key)->first();
            if ($task && $form->isCreating()) {
                $error = new MessageBag([
                    'title'   => '错误',
                    'message' => '任务关键字已经存在',
                ]);
                return back()->withInput()->with(compact('error'));
            }
        });
        return $form;
    }

}
