<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Nshistoric extends MAIN_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->init('nshistoric');
    }

    public function changeStatus(){

        $this->load->model("nshistoric/notification_history_model","mHistoric");


        $id = intval(RequestInput::get("id"));
        $status = intval(RequestInput::get("status"));

        //id & status

        $data =  $this->mHistoric->changeStatus($id,$status);

        if($status == 1){

            $notification = $this->mHistoric->getNotification($id);
            if($notification != NULL){
                ActionsManager::add_action('nshistoric','read_notification',$notification);
            }

        }


        echo json_encode($data);return;

    }


    public function onLoad()
    {
        parent::onLoad(); // TODO: Change the autogenerated stub

        $this->load->model('nshistoric/notification_history_model');
        $this->load->helper('nshistoric/nshistoric');

    }

    public function onCommitted($isEnabled)
    {
        parent::onCommitted($isEnabled); // TODO: Change the autogenerated stub


        $this->register_modules();

    }


    private function register_modules(){

        ActionsManager::register("store","onDelete",function ($id){


            if(is_array($id) && isset($id['id'])){
                $id = $id['id'];
            }

            $this->db->where("module","store");
            $this->db->where("module_id",$id);
            $this->db->delete("nsh_notifications");

        });

        ActionsManager::register("store","storeRemoved",function ($id){

            $this->db->where("module","store");
            $this->db->where("module_id",$id);
            $this->db->delete("nsh_notifications");

        });

        ActionsManager::register("product","productRemoved",function ($id){

            $this->db->where("module","product");
            $this->db->where("module_id",$id);
            $this->db->delete("nsh_notifications");

            $this->db->where("module","offer");
            $this->db->where("module_id",$id);
            $this->db->delete("nsh_notifications");

        });

    }

    public function onInstall()
    {
        parent::onInstall(); // TODO: Change the autogenerated stub
        $this->notification_history_model->createTable();

        return TRUE;
    }


    public function onUpgrade()
    {
        parent::onUpgrade(); // TODO: Change the autogenerated stub
        $this->notification_history_model->createTable();


        return TRUE;
    }

    public function onEnable()
    {
        return TRUE;
    }

    public function onUninstall()
    {
        parent::onUninstall(); // TODO: Change the autogenerated stub


        return TRUE;
    }


}