<?php

if (!defined('BASEPATH'))

	exit('No direct script access allowed');



class Signup extends MY_Controller {





    public $_list_data = array();



	public function __construct() {



		global $config;





		parent::__construct();

        $this->dt_params['dt_headings'] = "signup_id, signup_firstname, signup_lastname, signup_email, signup_tax_document, signup_status";

        //$this->dt_params['searchable'] = array("signup_id","signup_firstname","signup_status");

        $this->dt_params['action'] = array(

                                        "hide_add_button" => false ,

                                        "hide_upload_button" => true ,

                                        "hide" => false ,

                                        "show_delete" => true ,

                                        "show_edit" => true ,

                                        "order_field" => false ,

                                        "show_view" => false ,

                                        "extra" => array() ,

                                      );



/*

        $this->_list_data['signup_status'] = array(

                                        STATUS_INACTIVE => "<span class=\"label label-danger\">Inactive</span>",

                                        STATUS_ACTIVE =>  "<span class=\"label label-primary\">Active</span>"

                                    );*/



        $config['js_config']['paginate'] = $this->dt_params['paginate'];



        /*$this->_list_data['signup_category'] = $this->model_category->find_all_list_active(array(),"category_name");

        $this->_list_data['signup_level'] = $this->model_item->get_fields('item_level')['list_data'];

        $this->_list_data['signup_item_type'] = $this->model_item->get_fields('item_type')['list_data'];*/



		$_POST = $this->input->post(NULL, false);

	}
	
    public function get_user_applied_states($id)
    {
        $all_states = $this->model_state->find_all_list_active(array(),'state_name');
        $userData = $this->model_signup->find_by_pk($id);
        $applied_ids = explode(',',unserialize($userData['signup_tax_exempted_states']));
        
        $param001['where']['tax_exempted_signup_id'] = $id;
        $approved = $this->model_tax_exempted_state->find_all($param001);
        // debug($approved);
        // debug($applied_ids);
        // debug($all_states);
        $userAppliedStates = array();
        foreach($applied_ids as $key=>$value):
            foreach($all_states as $key1=>$value1):
                if($key1 == $value):
                    $userAppliedStates[$key1] = $all_states[$key1];
                    break;
                endif;
            endforeach;
        endforeach;
        foreach($approved as $key=>$value):
            foreach($all_states as $key1=>$value1):
                if($key1 == $value['tax_exempted_state']):
                    $userAppliedStates[$key1] = $all_states[$key1];
                    break;
                endif;
            endforeach;
        endforeach;
        return $userAppliedStates;
    }
    
    public function update_tax_exempted_states($userId, $signup_tax_states)
    {
        $this->db->query("DELETE FROM `fb_tax_exempted_state` WHERE `tax_exempted_signup_id` = {$userId} ");
        // echo $this->db->last_query();
        // exit();
        $userData = $this->model_signup->find_by_pk($userId);
        $insertParam['tax_exempted_signup_id'] = $userId;
        // debug($signup_tax_states,1);
        foreach($signup_tax_states as $key=>$value):
            $insertParam['tax_exempted_state'] = $value;
            $this->model_tax_exempted_state->insert_record($insertParam);
            // debug($insertParam,1);
        endforeach;
    }



	public function add($id='', $data=array())

	{
	    
	    $this->_list_data['signup_tax_states'] = $this->get_user_applied_states($id);
        
	    if((isset($_POST)) && (count($_POST)>0) && (!empty($id)))
	    {
            $this->update_tax_exempted_states($id, $_POST['signup']['signup_tax_states']);
	        unset($_POST['signup']['signup_password']);
	       // unset($_POST['signup']['signup_tax_states']);
	       $_POST['signup']['signup_tax_states'] = serialize($_POST['signup']['signup_tax_states']);
        }


		parent::add($id, $data);

	}



    // update_password

    public function update_password(){

        $data = $this->input->post('signup');

        if((count($_POST) > 0) && (isset($data['signup_password'])) && (!empty($data['signup_password']))) {

            $param['signup_password'] = md5($data['signup_password']);

            $status = $this->model_signup->update_by_pk($data['signup_id'],$param);

            if($status){

                $msg = 'Password changed successfully.';

                redirect($this->admin_path."?msgtype=success&msg=$msg", 'refresh');

            }

            else{

                $msg = "Unable to change password. Please user different password";

                redirect($this->admin_path."?msgtype=error&msg=$msg", 'refresh');

            }

        }

        else{

            $msg = "Record not updated.";

            redirect($this->admin_path."?msgtype=error&msg=$msg", 'refresh');

        }

    }



}



/* End of file welcome.php */

/* Location: ./application/controllers/welcome.php */

