<?php
// +----------------------------------------------------------------------
// | onezan-admin 快速开发的管理平台
// +----------------------------------------------------------------------
// | 官方网址：https://www.onezan.cn
// +----------------------------------------------------------------------
// | onezan团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: 543885307@qq.com
// +----------------------------------------------------------------------
namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\model\AdminUser;


/**
 * Reset Admin Password
 * @package app\command
 */
class ResetAdminPassword extends Command
{

    /**
     * 配置重置管理员密码命令
     * @return void
     */
    protected function configure()
    {

        $this->setName('reset:admin:password')
            ->setDescription('Reset admin password to 123456');
    }


    /**
     * 执行命令
     * @param Input $input
     * @param Output $output
     */
    protected function execute(Input $input, Output $output)
    {
        $username = 'admin';
        $password = '123123';

        $user = AdminUser::where('username', $username)->find();

        if (!$user) {
            $user = new AdminUser();
            $user->username = $username;
            $user->nickname = '';
            $user->avatar = '';
            $user->status = 1;
            $output->writeln("Create new admin user...");
        } else {
            $output->writeln("Found admin user (ID: {$user->id})...");
        }

        $user->password = password_hash($password, PASSWORD_DEFAULT);
        $user->save();

        $output->writeln("Admin password has been reset to: {$password}");
    }
}