<?php

namespace App\Admin\Controllers;

use App\Models\TransferRecord;
use App\Service\SelectService;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Models\BlindBoxList;
use Dcat\Admin\Admin;

/**
 * 三个状态
 * 未开始1
 * 进行中2
 * 已结束3-已开奖
 */
class BlindBoxRecordController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $model = new BlindBoxList();
        return Grid::make((clone $model), function (Grid $grid) {
            // 新增按钮-禁用
            $grid->disableCreateButton();
            // 行操作--禁用
            $grid->disableActions();
            // 行选择器
            $grid->disableRowSelector();
            // 禁用--批量操作
//            $grid->disableBatchActions();
            # 已结束
            # $grid->model()->where('status', '=', 3);
            $grid->addTableClass(['table-text-center']);
            $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->column('end_time');
            $grid->column('status', '当前状态')->display(function ($item) {
                $start_time = ToTimestamp($this->start_time);
                $end_time = ToTimestamp($this->end_time);
                # 获取当前时间
                $cur_time = ToTimestamp("now");
                $buy_num = json_decode($this->purchased);
                if ($start_time < $cur_time && $end_time > $cur_time) {
                    # 进行中
                    return "<div style='background: rgba(18, 185, 129, 0.1);color:#12B981;height:25px;display:flex;justify-content:center;align-items:center;border-radius: 15px;line-height: 25px;
font-weight: 600;
font-size: 13px;'>
                               进行中
                            </div>";
                } else if ($cur_time > $end_time && count($buy_num ?? []) < $this->min_participants_num) {
                    return "<div style='background: rgba(126, 126, 126, 0.1);color: #7E7E7E;height:25px;display:flex;justify-content:center;align-items:center;border-radius: 15px;line-height: 25px;
font-weight: 600;
font-size: 13px;'>
                               未完成
                            </div>";
                } else if ($cur_time > $end_time) {
                    # 已完成
                    return "<div style='background: rgba(22, 93, 255, 0.1);color:#165DFF;height:25px;display:flex;justify-content:center;align-items:center;border-radius: 15px;line-height: 25px;
font-weight: 600;
font-size: 13px;'>
                               已完成
                            </div>";
                }
            });
            # 参与钱包
            $grid->column('purchased', '参与钱包')->display(function () {
                $json = json_decode($this->purchased);
                if (!$json) {
                    return '暂无';
                }
                return '点击展开';
            })->expand(function ($model) {
                // 返回显示的详情
                $json = json_decode($this->purchased);
                $div = '';
                if ($json && count($json)) {
                    foreach ($json as $value) {
                        $div = $div . "<div>$value->address</div>";
                    }
                }
                return "<div style='padding:10px 0'>$div</div>";
//                $div = '';
//                $list = BlindBoxList::find($this->id)->user_wallet()->where('status', 1)->get();
//                if ($list && count($list)) {
//                    foreach ($list as $value) {
//                        $div = $div . "<div>$value->user_address</div>";
//                    }
//                }
//                return $div;
            });
            # winner_list-中奖钱包
            $grid->column('winner_list', '中奖钱包')->display(function ($item) {
                $json = json_decode($item);
                if (!$json) {
                    return '暂无';
                }
                return '点击展开';
            })->expand(function () {
                $json = json_decode($this->winner_list);
                if ($json && count($json)) {
                    # 是数组
                    $div = '';
                    foreach ($json as $value) {
                        $item = '';
                        # 查询是否中奖
                        $record = TransferRecord::query()->where('blind_box_id', $this->id)
                            ->whereIn('status', [1, 2])->first(['status', 'hash']);
                        if ($record) {
                            if ($record->status == 1) {
                                $item = "<span style='color:green;font-weight: bold'>已打款
                            --$record->hash
                             </span>";
                            } else {
                                $item = "<span style='color:red;font-weight: bold'>未打款</span>";
                            }
                        }
                        $div = $div . "<div style='margin: 12px 0'>$value--$item</div>";
                    }
                    return $div;
                }
                return '';
            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('status')->select(SelectService::$statusLabel);
            });
        });
    }

    /**
     * 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');
            #purchased--已购买
            # winner_list--中奖用户列表
//            $form->display('status');
            $form->number('winner_num', '中奖人数')->default(1)->min(0);
            $form->file('project_icon', '项目图片')->accept('jpg,png,gif,jpeg', 'image/*')->disk('admin')->autoUpload(true)->uniqueName();
            $form->number('box_num', '生成盲盒数量')->default(1)->min(0);
            $form->textarea('rules', '规则');
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
