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

class contact extends MY_Controller{
	function __construct(){
        parent::__construct();
        $this->load->model('contact_model', '', TRUE);
        $this->load->model('category_model', '', TRUE);
        
    }
    function index(){
        $contactall = $this->contact_model->allcontact_details_withgroupby();
        // Genral A-z
        $html ='<div class="panel-group accordion">';
            $html .='<div class="panel panel-primary">';
                $html .='<div class="panel-heading" style="background:#7d9748;">';
                    $html .='<h4 class="panel-title">';
                        $html .='<a href="#accOneColOneall" style="color:#ffffff;font-weight:bold;">GENERAL (A-Z) CONTACT</a>';
                    $html .='</h4>';
                    $html .='<ul class="panel-controls  pull-right">
                                <h4><strong style="color:#ffffff;font-weight:bold;">TOTAL : '.count($contactall).'</strong></h4>
                            </ul>';
                $html .='</div>';
                $html .='<div class="panel-body panel-body-open" id="accOneColOneall">';
                    $html .='<table border="1" class="table datatable">';
                        $html .='<thead>';
                            $html .='<tr>';
                                $html .='<th><input type="checkbox" class="checkallcontact" id="checkAll" onclick="allcontactwise()">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 ($contactall as $contactkey => $contactvalue) {
                            $html .='<tr id="row'.$contactvalue['id'].'">';
                                    $html .='<td id="ch" ><input type="checkbox" class="catchdallcontact"   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'].'contact/update/'.$contactvalue['id'].'">
                                             <span class="btn btn-primary"><i class="fa fa-pencil"></i></span>
                                        </a>';
                                         $html .='<a href="'.$this->data['base_url'].'contact/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>';
        // Multiple 
        $multiplec = $this->contact_model->all_is_multiple();
        $html .='<div class="panel-group accordion">';
            $html .='<div class="panel panel-primary">';
                $html .='<div class="panel-heading" style="background:#7d9748;">';
                    $html .='<h4 class="panel-title">';
                        $html .='<a href="#multiplec" style="color:#ffffff;font-weight:bold;">MULTIPLE CONTACT</a>';
                    $html .='</h4>';
                    $html .='<ul class="panel-controls  pull-right">
                                <h4><strong style="color:#ffffff;font-weight:bold;">TOTAL : '.count($multiplec).'</strong></h4>
                            </ul>';
                $html .='</div>';
                $html .='<div class="panel-body" id="multiplec">';
                    $html .='<table border="1" class="table datatable">';
                        $html .='<thead>';
                            $html .='<tr>';
                                $html .='<th><input type="checkbox" class="checkmul" id="checkAll" onclick="multiplecontactwise()">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 ($multiplec as $contactkey => $contactvalue) {
                            $html .='<tr id="row'.$contactvalue['id'].'">';
                                    
                                    $html .='<td id="ch" ><input type="checkbox" class="catchdmul"  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'].'contact/update/'.$contactvalue['id'].'">
                                             <span class="btn btn-primary"><i class="fa fa-pencil"></i></span>
                                        </a>';
                                         $html .='<a href="'.$this->data['base_url'].'contact/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>';
        $html .='</div>'; 
        $this->data['contect'] = $html;
        $this->data['message'] = $this->session->flashdata('message');

        $this->data['breadcrumb']='New contact';
        $this->data['tpl_name']= "contact.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'] . 'contact/');
                }else{
                    $this->session->set_flashdata('message',"Contact Added Successfully.");
                    redirect($this->data['base_url'] . 'contact/');
                }
            }else{
                $this->session->set_flashdata('message',"Please Category Select and Re-enter Record.");
                redirect($this->data['base_url'] . 'contact/');
            }
        } 
        $this->data['category'] = $this->category_model->allcategory_details();
        $this->data['message'] = $this->session->flashdata('message');
        $this->data['breadcrumb']='New contact';
        $this->data['tpl_name']= "contact_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'] . 'contact/');
            }else{
                $this->session->set_flashdata('message',"Please Category Select and Re-enter Record.");
                redirect($this->data['base_url'] . 'contact/');
            }            
        } 
        $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']= "contact_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'] . 'contact/');
        }else{
             redirect($this->data['base_url'] . 'contact/');
        }
    }
    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;
    }

}
