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

use app\dice\helper\AdminScopeHelper;
use plugin\saiadmin\basic\BaseController;
use app\dice\logic\player_ticket_record\DicePlayerTicketRecordLogic;
use app\dice\validate\player_ticket_record\DicePlayerTicketRecordValidate;
use app\dice\model\player\DicePlayer;
use plugin\saiadmin\service\Permission;
use support\Request;
use support\Response;

/**
 * 抽奖券获取记录控制器
 */
class DicePlayerTicketRecordController extends BaseController
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->logic = new DicePlayerTicketRecordLogic();
        $this->validate = new DicePlayerTicketRecordValidate;
        parent::__construct();
    }

    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    #[Permission('抽奖券获取记录列表', 'dice:player_ticket_record:index:index')]
    public function index(Request $request): Response
    {
        $where = $request->more([
            ['username', ''],
            ['use_coins_min', ''],
            ['use_coins_max', ''],
            ['ante', ''],
            ['total_ticket_count_min', ''],
            ['total_ticket_count_max', ''],
            ['paid_ticket_count_min', ''],
            ['paid_ticket_count_max', ''],
            ['free_ticket_count_min', ''],
            ['free_ticket_count_max', ''],
            ['create_time_min', ''],
            ['create_time_max', ''],
        ]);
        $query = $this->logic->search($where);
        AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null, $request->input('dept_id'));
        $query->with([
            'dicePlayer',
        ]);
        $data = $this->logic->getList($query);
        return $this->success($data);
    }

    /**
     * 获取玩家选项（id、username）用于下拉
     * @param Request $request
     * @return Response
     */
    #[Permission('抽奖券获取记录列表', 'dice:player_ticket_record:index:index')]
    public function getPlayerOptions(Request $request): Response
    {
        $query = DicePlayer::field('id,username');
        AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null, $request->input('dept_id'));
        $list = $query->select();
        $data = $list->map(function ($item) {
            return ['id' => $item['id'], 'username' => $item['username'] ?? ''];
        })->toArray();
        return $this->success($data);
    }

    /**
     * 读取数据
     * @param Request $request
     * @return Response
     */
    #[Permission('抽奖券获取记录读取', 'dice:player_ticket_record:index:read')]
    public function read(Request $request): Response
    {
        $id = $request->input('id', '');
        $model = $this->logic->read($id);
        if (!$model) {
            return $this->fail('not found');
        }
        if (!AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $model->dept_id ?? null, $request->input('dept_id'))) {
            return $this->fail('no permission to view this record');
        }
        $data = is_array($model) ? $model : $model->toArray();
        return $this->success($data);
    }

    /**
     * 保存数据
     * @param Request $request
     * @return Response
     */
    #[Permission('抽奖券获取记录添加', 'dice:player_ticket_record:index:save')]
    public function save(Request $request): Response
    {
        $data = $request->post();
        $this->validate('save', $data);
        AdminScopeHelper::prepareBusinessSaveData($data, $this->adminInfo ?? null, $request->input('dept_id'), $data);
        $result = $this->logic->add($data);
        if ($result) {
            return $this->success('add success');
        } else {
            return $this->fail('add failed');
        }
    }

    /**
     * 删除数据
     * @param Request $request
     * @return Response
     */
    #[Permission('抽奖券获取记录删除', 'dice:player_ticket_record:index:destroy')]
    public function destroy(Request $request): Response
    {
        $ids = $request->post('ids', '');
        if (empty($ids)) {
            return $this->fail('please select data to delete');
        }
        $result = $this->logic->destroy($ids);
        if ($result) {
            return $this->success('delete success');
        } else {
            return $this->fail('delete failed');
        }
    }

}
