<?php
namespace App\Admin\Actions\Users;

use App\Models\User;
use App\Models\UserIdentify;
use App\Models\UserVipLogModel;
use App\Models\VipModel;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Illuminate\Support\Facades\DB;
use PHPUnit\Util\Exception;

class UserAuthForm extends Form implements LazyRenderable
{
    use LazyWidget;


    public function __construct($data = [], $key = null)
    {
        parent::__construct($data, $key);
    }

    public function handle(array $input)
    {
        DB::beginTransaction();
        try {
            $user_identify = UserIdentify::query()->find($input['id']);
            if($input['is_auth']>0){
                $user_identify->status = 1;
                $user_identify->save();

                $user = User::query()->find($user_identify->user_id);
                if(!$user){
                    throw new Exception(trans('user.help.refresh_error'));
                }
                $user->is_auth = $input['is_auth'];
                $user->save();
            }

            DB::commit();
        }catch (\Exception $exception){
            DB::rollBack();
            return $this->response()->error($exception->getMessage());
        }

        return $this->response()->success('success')->refresh();
    }


    public function form()
    {
        $this->hidden('id')->value($this->payload['id']);
        $this->radio('is_auth', trans('user.fields.is_auth'))->options([0=> trans('user.fields.unauthorized'),1=> trans('user.fields.organization'),2=> trans('user.fields.public_figure')])->default(0);
    }
}
