<?php namespace App\Models;

use CodeIgniter\Model;

class Member_model extends Model
{
    protected $table      = 'member';
    protected $primaryKey = 'id';
	
	protected $allowedFields = ['name', 'last_name' ,'gender', 'mobile','email','username', 'password', 'address' , 'city' , 'state' , 'country' , 'pincode' , 'is_email_verified' , 'verification_code' , 'is_active' , 'created_by','updated_by','country_code'];
	
	protected $beforeInsert = ['beforeInsert'];
	protected $beforeUpdate = ['beforeUpdate'];
	
	protected function beforeInsert(array $data){
		$data= $this->passwordHash($data);
		return $data;
	}
	
	protected function beforeUpdate(array $data){
		$data= $this->passwordHash($data);
		return $data;
	}
	
	protected function passwordHash(array $data)
	{
		if(isset($data['data']['password']))
		$data['data']['password'] = password_hash($data['data']['password'],PASSWORD_DEFAULT);
		return $data;
	}
	public function verifyEmail($email)
	{
		
		$builder = $this->db->table('member');
		$builder -> select("id,mobile,email,password,is_email_verified,name,is_active");
		$builder -> where('email',$email);
		$result = $builder->get();
		if(count($result->getResultArray()) ==1)
		{
			return $result->getRowArray();
		}else{
			return false;
		}
	}
	public function getDefaultAddress($MemberId)
    {
        return $this->db->table('member_address')
            ->where('member_id', $MemberId)
			->where('address_type', 'default')
            ->get()
            ->getRow();
    }
	public function getAdditionalAddress($MemberId)
    {
        return $this->db->table('member_address')
            ->where('member_id', $MemberId)
			->where('address_type', 'additional')
            ->get()
            ->getResult();
    }
	
}