<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\dice\controller\play_record;

use plugin\saiadmin\basic\BaseController;
use app\dice\logic\play_record\DicePlayRecordLogic;
use app\dice\validate\play_record\DicePlayRecordValidate;
use app\dice\model\player\DicePlayer;
use app\dice\model\lottery_config\DiceLotteryConfig;
use app\dice\model\reward_config\DiceRewardConfig;
use plugin\saiadmin\service\Permission;
use support\Request;
use support\Response;

/**
 * 玩家抽奖记录控制器
 */
class DicePlayRecordController extends BaseController
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->logic = new DicePlayRecordLogic();
        $this->validate = new DicePlayRecordValidate;
        parent::__construct();
    }

    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    #[Permission('玩家抽奖记录列表', 'dice:play_record:index:index')]
    public function index(Request $request): Response
    {
        $where = $request->more([
            ['username', ''],
            ['lottery_config_name', ''],
            ['lottery_type', ''],
            ['is_win', ''],
            ['win_coin_min', ''],
            ['win_coin_max', ''],
            ['reward_ui_text', ''],
            ['reward_tier', ''],
        ]);
        $query = $this->logic->search($where);
        $query->with([
            'dicePlayer',
            'diceRewardConfig',
            'diceLotteryConfig',
        ]);
        $data = $this->logic->getList($query);
        return $this->success($data);
    }

    /**
     * 获取玩家选项（id、username）
     */
    #[Permission('玩家抽奖记录列表', 'dice:play_record:index:index')]
    public function getPlayerOptions(Request $request): Response
    {
        $list = DicePlayer::field('id,username')->select();
        $data = $list->map(function ($item) {
            return ['id' => $item['id'], 'username' => $item['username'] ?? ''];
        })->toArray();
        return $this->success($data);
    }

    /**
     * 获取彩金池配置选项（id、name）
     */
    #[Permission('玩家抽奖记录列表', 'dice:play_record:index:index')]
    public function getLotteryConfigOptions(Request $request): Response
    {
        $list = DiceLotteryConfig::field('id,name')->select();
        $data = $list->map(function ($item) {
            return ['id' => $item['id'], 'name' => $item['name'] ?? ''];
        })->toArray();
        return $this->success($data);
    }

    /**
     * 获取奖励配置选项（id、ui_text、tier）
     */
    #[Permission('玩家抽奖记录列表', 'dice:play_record:index:index')]
    public function getRewardConfigOptions(Request $request): Response
    {
        $list = DiceRewardConfig::field('id,ui_text,tier')->select();
        $data = $list->map(function ($item) {
            return [
                'id' => $item['id'],
                'ui_text' => $item['ui_text'] ?? '',
                'tier' => $item['tier'] ?? ''
            ];
        })->toArray();
        return $this->success($data);
    }

    /**
     * 读取数据
     * @param Request $request
     * @return Response
     */
    #[Permission('玩家抽奖记录读取', 'dice:play_record:index:read')]
    public function read(Request $request): Response
    {
        $id = $request->input('id', '');
        $model = $this->logic->read($id);
        if ($model) {
            $data = is_array($model) ? $model : $model->toArray();
            return $this->success($data);
        } else {
            return $this->fail('未查找到信息');
        }
    }

    /**
     * 保存数据
     * @param Request $request
     * @return Response
     */
    #[Permission('玩家抽奖记录添加', 'dice:play_record:index:save')]
    public function save(Request $request): Response
    {
        $data = $request->post();
        $this->validate('save', $data);
        $result = $this->logic->add($data);
        if ($result) {
            return $this->success('添加成功');
        } else {
            return $this->fail('添加失败');
        }
    }

    /**
     * 更新数据
     * @param Request $request
     * @return Response
     */
    #[Permission('玩家抽奖记录修改', 'dice:play_record:index:update')]
    public function update(Request $request): Response
    {
        $data = $request->post();
        $this->validate('update', $data);
        $result = $this->logic->edit($data['id'], $data);
        if ($result) {
            return $this->success('修改成功');
        } else {
            return $this->fail('修改失败');
        }
    }

    /**
     * 删除数据
     * @param Request $request
     * @return Response
     */
    #[Permission('玩家抽奖记录删除', 'dice:play_record:index:destroy')]
    public function destroy(Request $request): Response
    {
        $ids = $request->post('ids', '');
        if (empty($ids)) {
            return $this->fail('请选择要删除的数据');
        }
        $result = $this->logic->destroy($ids);
        if ($result) {
            return $this->success('删除成功');
        } else {
            return $this->fail('删除失败');
        }
    }

}
