<?php

class authModel extends mainModel {

    private $db;
    private $setting_arr = array();

    public function __construct() {
        $registry = Registry::getInstance();
        $registry->person = new Person;
        $this->db = DBConnectorFactory::loadDriver();
        //
        $query = $this->db->query("SELECT setting_name,setting_value  FROM tbl_default_settings 
            WHERE setting_name in ('is_production', 'notification_days_before_expiry','activate_pin_code_login') ");
        while ($row = $this->db->assoc($query))
            $this->setting_arr[$row['setting_name']] = $row['setting_value'];
    }

    public function get_companies() {
        $data = array();
        $query = $this->db->query('select * from companies');
        while ($row = $this->db->assoc($query)) {
            $data[] = $row;
        }
        return $data;
    }

    public function get_is_production() {
        $is_production = (!empty($this->setting_arr['is_production']) && $this->setting_arr['is_production'] == 1);
        return $is_production;
    }
    public function get_is_pin_code_login() {
        $is_pin_code_login = (!empty($this->setting_arr['activate_pin_code_login']) && $this->setting_arr['activate_pin_code_login'] == 1);
        return $is_pin_code_login;
    }

    public function get_remain_expiry_date() {
        $ntfctn_bfr_exp_days = empty($this->setting_arr['notification_days_before_expiry']) ? null : $this->setting_arr['notification_days_before_expiry'];
        if (empty($ntfctn_bfr_exp_days)) //null or zero
            return null;
        $registry = Registry::getInstance();
        $exp_date = $registry->person->getExpiryDate();
        if (empty($exp_date))
            return null;
        if($exp_date < date('Y-m-d'))
            return -1;  //already expire
        $start_date = new DateTime($exp_date);
        $since_start = $start_date->diff(new DateTime(date('Y-m-d')));
        $remain_days = $since_start->days;
        //$remain_days = ((strtotime($exp_date) - strtotime(date('Y-m-d'))) / (60 * 60 * 24));  //problem when dayligh offset like from month 2 to month 9 example
        if ($remain_days <= $ntfctn_bfr_exp_days)
            return $remain_days;
    }
    public function open_product_page() {
        $data = array();
        $query = $this->db->query('select * from tbl_default_settings where setting_name ="enable_product_barcode_page" ');
        $setting_value = $this->db->assoc($query)['setting_value'];
        if($setting_value == '1')
            return true;
        else return false;
    }
    
    public function open_customer_points_page() {
        $data = array();
        $query = $this->db->query('select * from tbl_default_settings where setting_name ="enable_customer_points_page" ');
        $setting_value = $this->db->assoc($query)['setting_value'];
        if($setting_value == '1')
            return true;
        else return false;
    }

    

}
