<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    public $timestamps = FALSE;
    
    protected $primaryKey = "uid";
    
    protected $table = "papa_sdk_user";
    
    public static function findByAccount($account)
    {
        return self::where("account", $account)
            ->select(['uid', 'account', 'nick_name', 'real_name', 'id_card', 'mobile', 'reg_type', 'password'])
            ->first();
    }
    
    public static function findByMobile($phone)
    {
        return self::where("mobile", $phone)
            ->select(['uid', 'account', 'nick_name', 'real_name', 'id_card', 'mobile', 'reg_type', 'password'])
            ->first();
    }
    
    public static function attemptLogin($account, $password) 
    {
        $dbPasswd = self::where("account", $account)->value("password");
        if ($dbPasswd != self::encryptPassword($password)) {
            return FALSE;
        }
        return TRUE;
    }
    
    
    public static function resetPasswordByPhone($phone, $new_password)
    {
        return self::where("mobile", $phone)->update(['password' => self::encryptPassword($new_password)]);
    }
    
    public static function changePassword($uid, $new_password)
    {
        return self::where("uid", $uid)->update(["password" => self::encryptPassword($new_password)]);
    }
        
    public static function encryptPassword($password) 
    {
        return md5($password);
    }
    

}
