<?php

/* 
 * Author       : Jai K
 * Purpose      : Model for Billing Abishegam
 * Created On   : 2016-07-30 18:40
 */

class Bill_Settings_Model extends CI_Model {
    
    function __construct() {
        parent::__construct();
        
    }
    
    function get($tbl_name, $args = array()){
        $where = $args['where'];
        
        if( !empty( $args['select'] ) ) :
            $this->db->select( $args['select'] );
        endif;
        
        if( !empty( $args['order_by'] ) ) :
            $this->db->order_by( $args['order_by'] );
        endif;
        
        $q = $this->db->get_where( $tbl_name, $where );
        return $q->result_array();
    }
    
    function manipulate(){
        $now  = date("Y-m-d H:i:s");
        $user_data = get_user_data();
        $post = $this->input->post();
          
        $general = get_general_setting();
        
        //echo '<pre>'; print_r( $post ); die;
        //echo '<pre>Files: '; print_r( $_FILES ); die;
        
        /*
         * Basics
         */
        
        $slugs = array( 'rec_msg', 'ack_sms_msg', 'ack_mail_msg', 'video_url');
        
        $this->db->select('id, slug');
        $this->db->where_in( 'slug', $slugs );
        $q = $this->db->get_where( TBL_BILL_SETT, array('status !=' => 2));
        $basics = $q->result_array();
        
        $slugs = array_flip($slugs);
        foreach($basics as $k => $basic) :
            $slugs[$basic['slug']] = $basic['id'];
        endforeach;
        
        
        foreach($slugs as $slug => $id) :
            
            if( empty( $id ) ) :
                // Record not exists
                $this->db->insert( TBL_BILL_SETT, array('slug' => $slug, 'created_on' => $now, 'created_by' => $user_data['user_id']) );
            endif;
            
            $args = array(
                'status'    => 1
            );
            
            if( $slug == 'rec_msg' || $slug == 'ack_sms_msg' || $slug == 'ack_mail_msg' ) :
                /*
                * Messages
                */
                $value = !empty( $post['swap_inp_'.$slug] ) ? $post['swap_inp_'.$slug] . htmlspecialchars( $post[$slug] ) : 'ENG' . $post[$slug];
            else :
                if( $slug == 'video_url' ) :
                    $args['status'] = $post['video_chk'] == 'on' ? '1' : '0';
                endif;
                
                $value = $post[$slug];
            endif;
            
            $args['value'] = $value;

            $this->db->update( TBL_BILL_SETT, $args, array('slug' => $slug) );
            
        endforeach;
        
        
        /*
         * Events
         */
         
        if($general->scan_availability == 1) :
            $now_time = date("H:i:s");
            foreach( $post['event_name'] as $k => $evt_name ) :
                if( empty( $evt_name ) ) :
                    continue;
                endif;
                
                $lang = !empty( $post['swap_inp_event_name-'.$k.'-'] ) ? $post['swap_inp_event_name-'.$k.'-'] : 'ENG';
                $args = array(
                    'name'          => $lang. $evt_name,
                    /*'value'         => json_encode(array(
                            'start' => $post['event_start_date'][$k],
                            'end'   => $post['event_end_date'][$k],
                            'period'=> $post['event_period'][$k],
                        )),*/
                    
                    'value'         => $post['event_period'][$k],
                );
                
                $e  = explode( '-', $post['event_start_date'][$k] );
                $args['created_on'] = $e[2] . '-' . $e[1] .'-'. $e[0].' '. $now_time;
                
                $e  = explode( '-', $post['event_end_date'][$k] );
                $args['updated_on'] = $e[2] . '-' . $e[1] .'-'. $e[0].' ' .( $args['value'] == '1' ? '00:00:01' : '12:00:01' );
            
                if( empty( $post['event_id'][$k] ) ) :
                    // Insert
                    $args['slug'] = 'event';
                    $args['created_by'] = $user_data['user_id'];

                    $this->db->insert( TBL_BILL_SETT, $args );
                else :
                    // Update
                    $args['status'] = $post['event_chk'][$k] == 'on' ? 1 : 0;
                    $args['updated_by'] = $user_data['user_id'];
                    
                    $this->db->update( TBL_BILL_SETT, $args, array('id' => $post['event_id'][$k] ) );
                endif;
                
                
            endforeach;
            
            /*
             * Advertisements
             */
            if( !empty( $_FILES ) ) :
                
                $upload_data = uploads(array(
                    'field'         => 'ad_file',
                    'upload_path'   => DIR_ADVERTISEMENT,
                ));
            
            endif;
                
            foreach( $post['ad_name'] as $k => $ad_name ) :
                if( empty( $ad_name ) ) :
                    continue;
                endif;
                
                $args = array(
                    'name'  => $ad_name,
                );

                $value = $upload_data['ad_file']['file_arr'][$k];
                if( !empty( $value ) ) :
                    $args['value'] = $value;
                    
                    // Remove prev. file
                    unlink( DIR_ADVERTISEMENT . $post['ad_old'][$k] );
                endif;

                if( empty( $post['ad_id'][$k] ) ) :
                    // Insert
                    $args['slug']       = 'advertisement';
                    $args['created_on'] = $now;
                    $args['created_by'] = $user_data['user_id'];

                    $this->db->insert( TBL_BILL_SETT, $args );
                else :
                    // Update
                    $args['status'] = $post['ad_chk'][$k] == 'on' ? 1 : 0;
                    $args['updated_on'] = $now;
                    $args['updated_by'] = $user_data['user_id'];

                    $this->db->update( TBL_BILL_SETT, $args, array('id' => $post['ad_id'][$k] ) );
                endif;

            endforeach;
                
            
            
            if( !empty( $post['ad_rm'] ) ) :
                $args = array(
                    'status'    => 2,
                    'updated_on'=> $now,
                    'updated_by'=> $user_data['user_id']    
                );
            
                foreach( $post['ad_rm'] as $ad_id => $file_name ) :
                    // Remove File
                    unlink( DIR_ADVERTISEMENT . $file_name );
                    
                    $this->db->update( TBL_BILL_SETT, $args, array('id' => $ad_id ) );
                endforeach;
                        
            endif;
            
            /*
             * Update Records for all deleted rows
             */
            if( !empty( $post['rm_id'] ) ) : // Comma separated id's
                $args = array(
                    'status'    => 2,
                    'updated_by'=> $user_data['user_id']    
                );
            
                $this->db->where_in('id', $post['rm_id'] ); 
                $this->db->update( TBL_BILL_SETT, $args, array() );
                
            endif;
        endif;
  }
    
    function ajax(){
        $post = $this->input->post();
        $return = array();
        
        switch( $post['act'] ) :
            case 'events':
                // Get all Events
                $return = $this->get(TBL_BILL_SETT, array('where' => array( 'status !=' => 2, 'slug' => 'event' ) ) );
                
            break;    
        endswitch;
        
        return $return;
    }
    
}