<?php

namespace App\Console\Commands;

use App\Facades\CommonService;
use App\Models\Admin;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;

class CreateAdmin extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'admin:create';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'create an administrator account';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $name = $this->ask("enter account name");
        while (true) {
            $account = $this->ask('enter account number');
            if (!CommonService::isMobile($account)) {
                $this->info("[error] please enter a phone number");
                continue;
            }
            $is_exist = Admin::where("mobile", $account)->exists();
            if (!$is_exist) break;
            $this->info("[error] account exists");
        }
        while (true) {
            $password = $this->secret("enter the password");
            if (strlen($password) < 6) {
                $this->info('[error] password`s length less than 6');
                continue;
            }
            $re_password = $this->secret("enter the password again");
            if ($password == $re_password) break;
            $this->info("[error] password not the same");
        }

        Admin::create([
            'name'=>$name?:"Admin",
            'mobile'=>$account,
            'password'=>Hash::make($password),
            'desc'=>"系统管理员"
        ]);
        return Command::SUCCESS;
    }
}
