<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Form\CreateBlindBox;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Models\BlindBoxList;

/**
 * 三个状态
 * 未开始1
 * 进行中2
 * 已结束3
 */
class BlindBoxListController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        # $model = new BlindBoxList();
        return Grid::make((new BlindBoxList()), function (Grid $grid) {
            # 未完成
            $grid->model()->where('status', '!=', 3);
            $grid->column('id')->sortable();
            $grid->column('projectName');
            $grid->column('price')->display(function ($text) {
                return $text . '$';
            });
            $grid->column('max_participants_num');
            $grid->column('min_participants_num');
            $grid->column('start_time', '活动时间')->display(function ($start) {
                return "<div>
                         <div>$start</div>
                         <div>$this->end_time</div>
                        </div>";
            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('projectName');
                # $filter->panel();
            });
            $grid->tools(function (Grid\Tools $tools) {
                $tools->append(new CreateBlindBox());
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new BlindBoxList(), function (Show $show) {
            $show->field('id');
            $show->field('projectName');
            $show->field('price');
            $show->field('max_participants_num');
            $show->field('min_participants_num');
            $show->field('start_time');
            $show->field('end_time');
            $show->field('status');
            $show->field('winner_num');
            $show->field('project_icon');
            $show->field('box_num');
            $show->field('rules');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new BlindBoxList(), function (Form $form) {
            $form->display('id');
            $form->text('projectName');
            $form->text('price');
            $form->number('max_participants_num')->default(5)->min(0);
            $form->number('min_participants_num')->default(1)->min(0);
            $form->datetime('start_time');
            $form->datetime('end_time');
            $form->number('winner_num', '中奖人数')->default(1)->min(0);
            $form->number('box_num', '生成盲盒数量')->default(1)->min(0);
            $form->number('can_invite_num', '最高邀请人数')->default(1)->min(0);
            $form->number('invite_up_rate', '单次邀请增加的几率')->default(1)->min(0);
            $form->file('project_icon', '项目图片')->accept('jpg,png,gif,jpeg', 'image/*')->disk('admin')->autoUpload(true)->uniqueName();
            $form->textarea('rules', '规则');
            $form->display('created_at');
            $form->display('updated_at');
            $form->saving(function (Form $form) {
                $env = '1';
                if ($env == 'dev') {
                    $form->project_icon = url("/uploads/$form->project_icon");
                } else {
                    # 线上环境
                    $form->project_icon = url("/storage/$form->project_icon");
                }
            });
            $form->saved(function (Form $form, $result) {
                // 自增ID
                $newId = $result;
                if ($newId) {
                    # 获取开始时间和结束时间
                    $start_time = ToTimestamp($form->start_time);
                    $end_time = ToTimestamp($form->end_time);
                    $status = getBlindBoxStatus($start_time, $end_time);
                    BlindBoxList::query()->where('id', $newId)->update(['status' => $status]);
                    # 判断是否需要添加多个
                    $num = $form->box_num;
                    if ((int)$num > 1) {
                        for ($i = 1; $i < $num; $i++) {
                            BlindBoxList::query()->create([
                                'projectName' => $form->projectName,
                                'price' => $form->price,
                                'max_participants_num' => $form->max_participants_num,
                                'min_participants_num' => $form->min_participants_num,
                                'start_time' => $form->start_time,
                                'end_time' => $form->end_time,
                                'winner_num' => $form->winner_num,
                                'project_icon' => $form->project_icon,
                                'box_num' => $form->box_num,
                                'rules' => $form->rules,
                                'status' => $status
                            ]);
                        }
                    }
                    return $form->response()->success('添加成功')->redirect('/blindBoxSetting/setting');
                } else {
                    return $form->response()->success('添加失败');
                }
            });
        });
    }
}
