<?php

namespace App\Http\Rules\Admin;

use Auth;
use App\Models\Admin;

use Illuminate\Contracts\Validation\Rule;

class LoginRule implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */

    protected $password;

    public function __construct($password = '')
    {
        $this->password = $password;
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        $email = $value;
        $password = $this->password;

        if (Auth::guard('admin')->attempt(['email' => $email, 'password' => $password])) {
            $data = Admin::
                    where('email', $email)
                    ->whereIn('status', [1])
                    ->count();
                if (!$data) {
                    return false;
                }
            return true;
        }

        return false;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return config('error_code.login_error');
    }
}
