<?php

declare(strict_types=1);

namespace app\admin\controller\mall;

use app\admin\model\MallPointsLog;
use app\common\controller\Backend;
use app\common\model\MallUserAsset;
use support\Response;
use Webman\Http\Request;

class PointsLog extends Backend
{
    protected ?object $model = null;

    protected array $withJoinTable = ['userAsset', 'admin'];
    protected array|string $preExcludeFields = ['id', 'before', 'after', 'memo', 'admin_id', 'create_time'];
    protected array|string $quickSearchField = [
        'userAsset.playx_user_id',
        'userAsset.username',
        'userAsset.phone',
    ];
    protected bool $autoFillAdminId = true;

    protected function initController(Request $request): ?Response
    {
        $this->model = new MallPointsLog();
        return null;
    }

    public function add(Request $request): Response
    {
        $response = $this->initializeBackend($request);
        if ($response !== null) {
            return $response;
        }

        if ($request->method() === 'POST') {
            return $this->_add();
        }

        $userAssetId = $request->get('userAssetId', $request->post('userAssetId', 0));
        $userAsset = MallUserAsset::where('id', $userAssetId)->find();
        if (!$userAsset) {
            return $this->error(__('User asset not found'));
        }

        return $this->success('', ['userAsset' => $userAsset]);
    }

    public function edit(Request $request): Response
    {
        $response = $this->initializeBackend($request);
        if ($response !== null) {
            return $response;
        }

        return $this->error(__('Points log cannot be modified'));
    }

    public function del(Request $request): Response
    {
        $response = $this->initializeBackend($request);
        if ($response !== null) {
            return $response;
        }

        return $this->error(__('Points log cannot be deleted'));
    }
}
