<?php

use app\libraries\Userstamp;

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/* Author: Jorge Torres
 * Description: Login model class
 */

class Referral_setting_model extends MY_Model
{
    public $database;
    public $short = "rsetting";
    public $table;
    public $tbl;
    const SETTING_ID = 1;

    public function __construct()
    {
        parent::__construct();
        $this->database = $this->getTenantNo();
        $this->table_prefix = '';
        $this->tbl = 'referral_setting';
        $this->table = $this->tbl . ' ' . $this->short;

        $this->db = $this->load->database($this->database, true);
    }

    public function save($params)
    {
        // if ($mode === 'c') {
        //     $this->db->insert('referral_scheme', Userstamp::create($params));
        // } else {

        // }
        $id = self::SETTING_ID;
        $this->db->update($this->table_prefix . $this->tbl, Userstamp::update($params), array('id' => $id));
        return $id;
    }

    public function referral_code_increment()
    {
        return $this->db
            ->where('id', self::SETTING_ID)
            ->set('code_counter_value', 'code_counter_value + 1', false)
            ->update($this->table_prefix . $this->tbl);
    }

    public function generate_template_message($template_message, $referral_code, $url)
    {
        return str_replace('{referral_code}', $referral_code, str_replace('{url}', $url, $template_message));
    }
}
