<?php

/*
 * Author       : Jai K
 * Purpose      : The Controller for Sending SMS
 * Created On   : 2016-10-18 10:46
 */

class Billing_SMS extends REF_Controller {
    public $controller;
    
    function __construct() {
        parent::__construct();
        
        $segments = $this->uri->segments;
        $this->controller = $segments[1];
        
        $this->load->model('billing/sms_model');
        
    }
    
    function list_all() {
        $data = array(
            'title'          => 'SMS',
            'controller'     => $this->controller,
            'add_btn'        => 'Send New',
            
            'success'        => $this->session->flashdata('success'),
            'error'          => $this->session->flashdata('error'),
        );
        
        
        // Load Views
        $this->render( $data, 'billing/sms/list' );
    }
    
    function send() {
        $user = $this->user_model->get_by_id();
        
        $permission = unserialize( $user->permissions );
        if( empty( $permission['sms'][0] ) ) :
            show_403_error();
        endif;
        
        $this->manipulate();
    }
    
    function manipulate() {
        
        $post = $this->input->post();
        $session_data = $this->session->userdata(TBL_PREFIX . 'user_logged_in');
        $token = $this->session->userdata( 'rtoken-'.$session_data['user_id'] );
        
        $data = array(
            'title'             => 'Send - SMS',
            'controller'        => $this->controller,
            'form_action'       => base_url() . $this->controller .'/'.$page.'/'.$id,
            'list_link'         => base_url() . $this->controller .'/list_all/',
            'success'           => $this->session->flashdata('success'),
            'error'             => $this->session->flashdata('error'),
            
            'sms_types'         => $this->sms_model->get(TBL_SMS_TYPES),
        );
        
        $this->render( $data, 'billing/sms/send' );
        
    }
    
    function ajax() {
        $post = $this->input->post();
        $results = array();
        
        switch( $post['act'] ) :
            
            case 'contacts' :
                // Get Contacts
                $results = $this->sms_model->get_contacts();
            break;    
        
            case 'sent' :
                $results  = $this->sms_model->send_sms();
            break;
            
        endswitch;
        
        echo json_encode( $results );
    }
    
}