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

class categorycontact extends MY_Controller{
	function __construct(){
        parent::__construct();
        $this->load->model('contact_model', '', TRUE);
        $this->load->model('category_model', '', TRUE);
    }
    function index(){
        $category = $this->category_model->allcategory_details();
        // Category wise
        $html .='<div class="panel-group accordion">';
        foreach ($category as $catkey => $catval) {
            if($catkey==0){ $active = 'panel-body-open'; }else{ $active='';}
            $contact = $this->contact_model->categorycontact_details($catval['id']);
            $html .='<div class="panel panel-primary">';
                $html .='<div class="panel-heading">';
                    $html .='<h4 class="panel-title">';
                        $html .='<a href="#accOneColOne'.$catval['id'].'">'.strtoupper($catval['name']).'</a>';
                    $html .='</h4>';
                    $html .='<ul class="panel-controls  pull-right">
                                <h4><strong>Total : '.count($contact).'</strong></h4>
                            </ul>';
                $html .='</div>                                ';
                //$html .='<div class="panel-body '.$active.'" id="accOneColOne'.$catval['id'].'">';
                $html .='<div class="panel-body" id="accOneColOne'.$catval['id'].'">';
                    $html .='<table border="1" class="table datatable">';
                        $html .='<thead>';
                            $html .='<tr>';
                                $html .='<th><input type="checkbox" class="check'.$catval['id'].'" id="checkAll" name="check_all[]" value="'.$catval['id'].'" onclick="categorywise('.$catval['id'].')">All</th>';
                                $html .='<th>Name</th>';
                                $html .='<th>Address</th>';
                                $html .='<th>City</th>';
                                $html .='<th>Number</th>';
                                $html .='<th>Category</th>';
                                $html .='<th>Action</th>';
                            $html .='</tr>';
                        $html .='</thead>';
                            foreach ($contact as $contactkey => $contactvalue) {
                                $html .='<tr id="row'.$contactvalue['id'].'">';
                                        $html .='<td id="ch" ><input type="checkbox" class="catchd'.$catval['id'].'"  name="contact_id[]" id="checkid" value="'.$contactvalue['id'].'" /></td>';
                                        $html .='<td id="name">'.$contactvalue['name'].'</td>';
                                        $html .='<td>'.nl2br($contactvalue['address']).'</td>';
                                        $html .='<td>'.$contactvalue['city'].'</td>';
                                        $html .='<td>'.$contactvalue['mobile'].'</td>';
                                       if($contactvalue['categoryid']!=''){
                                            $html .='<td>';
                                            $category_details = $this->contact_model->get_category($contactvalue['categoryid']);
                                            foreach ($category_details as $cdkey => $cdvalue) {
                                                $html .=strtoupper($cdvalue['name'])."<br>";
                                            }
                                            $html .='</td>';
                                        }else{
                                            $html .='<td></td>';
                                        }
                                        $html .='<td>';
                                            $html .='<a href="'.$this->data['base_url'].'categorycontact/update/'.$contactvalue['id'].'">
                                                 <span class="btn btn-primary"><i class="fa fa-pencil"></i></span>
                                            </a>';
                                             $html .='<a href="'.$this->data['base_url'].'categorycontact/delete/'.$contactvalue['id'].'">
                                                <span class="btn btn-danger"><i class="fa fa-trash-o"></i></span>
                                            </a>';
                                        $html .='</td>';
                                $html .='</tr>';
                               
                            }
                       
                    $html .='</table>';     
                $html .='</div>';
             $html .='</div>';
            
        }
        $html .='</div>'; 
        $this->data['contect'] = $html;
        $this->data['message'] = $this->session->flashdata('message');
        $this->data['breadcrumb']='New contact';
        $this->data['tpl_name']= "categorycontact.tpl";
        $this->smarty->assign('data', $this->data);
        $this->smarty->view('template.tpl');  
    }
    function create(){
        if($this->input->post()){ 
            $category = $this->input->post('categoryid'); 
            $data = $this->input->post('data'); 
            if(!empty($category)){
                $data['categoryid']=implode(',',$category);
                $data['status']='1';
                $data['is_delete']='0';
                $data['create_datetime']=date('Y-m-d h:i:s');
                $data['created_ip']=$_SERVER['REMOTE_ADDR'];
                //print_r($data);exit;
                $contactdata = $this->contact_model->addcontact($data);

                if($contactdata['msg']=='alreadyexist'){
                    $this->session->set_flashdata('message',"Contact already exist.");
                    redirect($this->data['base_url'] . 'categorycontact/');
                }else{
                    $this->session->set_flashdata('message',"Contact Added Successfully.");
                    redirect($this->data['base_url'] . 'categorycontact/');
                }
            }else{
                $this->session->set_flashdata('message',"Please Category Select and Re-enter Record.");
                redirect($this->data['base_url'] . 'categorycontact/');
            }
        } 
        $this->data['category'] = $this->category_model->allcategory_details();
        $this->data['message'] = $this->session->flashdata('message');
        $this->data['breadcrumb']='New contact';
        $this->data['tpl_name']= "categorycontact_create.tpl";
        $this->smarty->assign('data', $this->data);
        $this->smarty->view('template.tpl');  
    }  
    function update(){
        if($this->input->post()){            
            $category = $this->input->post('categoryid'); 
            $data = $this->input->post('data'); 
            if(!empty($category)){
                $data['categoryid']=implode(',',$category);
                $data['status']='1';
                $data['is_delete']='0';
                $data['create_datetime']=date('Y-m-d h:i:s');
                $data['created_ip']=$_SERVER['REMOTE_ADDR'];
                $contactdata = $this->contact_model->editcontact($data);
                $this->session->set_flashdata('message',"Contact Update Successfully.");
                redirect($this->data['base_url'] . 'categorycontact/');
            }else{
                $this->session->set_flashdata('message',"Please Category Select and Re-enter Record.");
                redirect($this->data['base_url'] . 'categorycontact/');
            }            
        } 
        $contactid = $this->uri->segment(3);
        $this->data['category'] = $this->category_model->allcategory_details();
        $this->data['contact_details'] = $this->contact_model->single_contact_details($contactid);
        $this->data['contact'] = $this->contact_model->allcontact_details();
        $this->data['cat_select'] = explode(',',$this->data['contact_details']['categoryid']);

        $this->data['message'] = $this->session->flashdata('message');
        $this->data['breadcrumb']='Edit contact';
        $this->data['tpl_name']= "categorycontact_create.tpl";
        $this->smarty->assign('data', $this->data);
        $this->smarty->view('template.tpl');  
    }
    function delete(){
        $contactid = $this->uri->segment(3);
        if($contactid!=''){            
            $data['is_delete'] = '1';
            $data['id'] = $contactid;
            $editcontact = $this->contact_model->editcontact($data);
            $this->session->set_flashdata('message',"Contact delete Successfully.");
            redirect($this->data['base_url'] . 'categorycontact/');
        }else{
             redirect($this->data['base_url'] . 'categorycontact/');
        }
    }
    function getcontactname(){
        if (isset($_GET['term'])){
          $q = strtolower($_GET['term']);
          $this->contact_model->getcontact($q);
        }
    }
    function getcontactdetails(){
        $name = $this->input->post('name');
        $data['name']=$name;
        $contact_details = $this->contact_model->get_contact_details($data);
        echo json_encode($contact_details);exit;
    }
}
