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

class Login_controller extends CI_Controller {

    var $rest_api_base_url = 'https://neha.webip.lk/lumen/public';
        public function __construct() {
        parent::__construct();
        
         $this->load->model('Product_model');
         $this->load->model('Common_model');
      
         
        }
        
	public function index()
	{
	    
	    if(isset($this->session->userdata['login_data']['username'])){
	       // $data['content'] = ('pages/dashboard');                       
                //    $this->load->view('template',$data);
                    redirect('dashboard');
	    }else{
	            $er['ss'] = "";
		        $data['content'] = ('login');                       
                $this->load->view('login',$er);
	    }
		
	}
	
	//bill book
	
	function bill_view(){
    	            
    	            //$id = $this->input->post('id');
    	             $this->db->from('mes_user');
                    $this->db->select("*");
                   $this->db->where('user_id', $this->input->post('id'));
                    $deta = $this->db->get();
                    $repname = $deta->result();
    	            
    	            
    	            $this->db->from('billbook');
                    $this->db->select("*");
                    $this->db->join( "mes_user", "billbook.bill_cuser = mes_user.user_id", 'left');
                   $this->db->where('bill_repid', $this->input->post('id'));
                    $deta = $this->db->get();
                    $databatch = $deta->result();
    	            
    	            
    	             //$det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_product' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel' data-backdrop='static' data-keyboard='false'>Add New Product</button></br>";
            //$det[0]="";
            $det[0] ="
                                        <thead class='thead-dark' >
            
                                 <tr>
														<th scope='col'>ID</th>
														<th >Book Code</th>
														<th >From Number</th>
														<th>To Number</th>
														<th>User</th>
													    <th>Date</th> 
													    <th>Stetus</th>  
													    <th>Action</th>                                         
																											
													</tr>
													
													</thead>
                                
                               
                             
           ";
           
           $det[1]="";
           $det[2]="";
           
           $i = 1;
                for($i=0; $i<count($databatch); $i++){
                    
                    if($databatch[$i]->bill_stetus==0){$sts = "<td style='color:Green; font-size:25px; text-align: center;'>&#9679;</td>";}else{$sts = "<td style='color:red; font-size:25px; text-align: center;'>&#9679;</td>";}
                    
                     $x = $i+1;
                    $dat =  "
                    
                              <tr><td>".$databatch[$i]->bill_id."</td>
                               <td>".$databatch[$i]->bill_bookid."</td>
                               <td>".$databatch[$i]->bill_from."</td>
                                <td>".$databatch[$i]->bill_to."</td>
                                 <td>".$databatch[$i]->user_name."</td>
                                <td>".$databatch[$i]->bill_cdate."</td>
                                 $sts
                                 <td><button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_bill/".$databatch[$i]->bill_id."' onclick='edit_bill(this.id)' data-target='#Modal_edit'>Edit</button></td>
                              </tr>
                              ";
                              $x++;
                              $det[1] =$det[1].$dat;
                              
                        $det[2]=$repname[0]->user_fname ." ".$repname[0]->user_lname;
                          
                }
                
                
                    
 
            	
           echo json_encode($det);
    	            
    	    
                        	
                       
    	}
  
    function bill_save(){
      
                    $this->form_validation->set_error_delimiters('', ''); 
              
                    $this->form_validation->set_rules('billfnum', 'From Number', 'required|trim');
                    $this->form_validation->set_rules('billtnum', 'To Number', 'required|trim');
                    //$this->form_validation->set_rules('connum', 'Contact Numbers', 'required');
                    
                     if ($this->form_validation->run() == FALSE){
                    $ret[0] = validation_errors();
                    $ret[2] = "error";
    	   }else{
    	       
    	      
    	       $data = array( 
                'bill_from'  => $this->input->post('billfnum'),
                'bill_to'  => $this->input->post('billtnum'),
                'bill_bookid'  => $this->input->post('billbid'),
                'bill_repid'  => $this->input->post('usid'),
                'bill_cdate'  => date('Y-m-d'),
                'bill_cuser'  => $this->session->userdata['login_data']['user_id']
             
            );
            $result = $this->db->insert('billbook',$data);
                if($result){
                    $ret = "New Bill Book  Saved";
                }else{
                    $ret = "Please Re enter all details";
                }
    	   }
                        
                	
               echo json_encode($ret);	
        }
        
    function bill_edit(){
            
          $id = $this->input->post('id');
    	    
    	    $this->db->from('billbook'); 
            $this->db->select("*");
            //$this->db->join( "mes_user_lvl", "mes_user.user_lvl = mes_user_lvl.user_lvl_val", 'left');
            //$this->db->join( "mes_depot", "mes_user.user_depoid =  mes_depot.dep_id", 'left');
            $this->db->where('bill_id',$id);
            $detail = $this->db->get();
            $data = $detail->result();
            
             $act = $inact = "";
           if($data[0]->bill_stetus=="0"){$act = "selected";}else{$inact = "selected";}
            
            
            $det="
             <div class='form-group row'>
               
               <input type='hidden' name='table' id='table' class='form-control' readonly='' value='".$data[0]->bill_id."'>
                                <label class='col-md-2 col-form-label'>From Number</label>
                                <div class='col-md-10'>
                                 <input type='text' name='editfnum' id='editfnum' class='form-control' placeholder='' value = '".$data[0]->bill_from."'>
                                 <input type='hidden' name='editofnum' id='editofnum' class='form-control' placeholder='' value = '".$data[0]->bill_from."'>
                                 </div>
                            </div>
                            <div class='form-group row'>
                             <label class='col-md-2 col-form-label'>To Number</label>
                                <div class='col-md-10'>
                                  <input type='text' name='edittnum' id='edittnum' class='form-control' placeholder='' value = '".$data[0]->bill_to."'>
                                  <input type='hidden' name='editotnum' id='editotnum' class='form-control' placeholder='' value = '".$data[0]->bill_to."'>
                                 </div>
                            </div>
                            
                              <div class='form-group row'>
                                <label class='col-md-2 col-form-label'>Status</label>
                                <div class='col-md-10'>
                                      <select class='form-control' id='status' name='status'>
                                            <option value='0' $act>Active</option>
                                            <option value='1' $inact>Inactive</option>
                                        </select>
                                 </div>
                            </div> 
                        
                        
              ";
                        
                	
               echo json_encode($det);	
            
        }
        
    function bill_update(){
            
                $this->form_validation->set_error_delimiters('', ''); 
                $this->form_validation->set_rules('editfnum', 'From Number', 'required|trim');
                $this->form_validation->set_rules('edittnum', 'To Number', 'required|trim');
                
               // $this->form_validation->set_rules('tc_eCat', 'Level', 'required');
               
                if ($this->form_validation->run() == FALSE){
                    $ret[0] = validation_errors();
    	   }else{
    	       
    	       
    	        $this->db->set('bill_from', $this->input->post('editfnum'));
                $this->db->set('bill_to', $this->input->post('edittnum'));
                $this->db->set('bill_stetus', $this->input->post('status'));
                $this->db->set('bill_euser', $this->session->userdata['login_data']['user_id']);
                $this->db->set('bill_edate', date('Y-m-d'));
                
                $this->db->where('bill_id ', $this->input->post('table'));
                $result=$this->db->update('billbook');
                
                if($result){
                    
                $data = array( 
                    'billlog_billid'  => $this->input->post('table'),
                    'billlog_from'  => $this->input->post('editfnum'),
                    'billlog_oldfrom'  => $this->input->post('editofnum'),
                    'billlog_to'  => $this->input->post('edittnum'),
                    'billlog_oldto'  => $this->input->post('editotnum'),
                    'billlog_stetus'  => $this->input->post('status'),
                    'billlog_date'  => date('Y-m-d'),
                    'billlog_user'  => $this->session->userdata['login_data']['user_id']
             
                    );
                    
                $this->db->insert('billbook_log',$data);
                    
                    $ret[0] = "Bill Book Details Updated";
                    
                    
                }else{
                    $ret[0] = "Please Re enter all details";
                }
    	       
    	       
    	   }
    	   
    	   $this->db->from('billbook');
            $this->db->where('bill_id ', $this->input->post('table'));
            $this->db->select("*");
            $detail = $this->db->get();
            $data = $detail->result();
    	   
    	   
    	   $ret[1] = $data[0]->bill_repid;
    	   echo json_encode($ret);	
        }
    	
    	// user
	
	function dlogin(){
	    
	            //$data['content'] = ('login');                       
                $this->load->view('ds'); 
	}
	
	function userman(){
	    
	    $this->db->from('mes_user');
            $this->db->select("*");
            $this->db->where('user_lvl',20);
            $detail = $this->db->get();
            $data['data'] = $detail->result();
            
            $this->db->from('cities');
            $this->db->select("*");
            $detail = $this->db->get();
            $data['city'] = $detail->result();
	    
	    $data['content'] = ('pages/user'); 
		$this->load->view('template',$data);
	}
	
	public function log(){
            
            $this->form_validation->set_rules('gent', 'Username','trim|required');
            $this->form_validation->set_rules('genwrd', 'Password','trim|required');
            $this->form_validation->set_error_delimiters('<div class="social-or-login center"><span class="bigger-110">', '</span></div>');
            $this->form_validation->set_rules('field_name', 'Field Label', 'rule1|rule2|rule3', array('rule2' => 'Error Message on rule2 for this field_name'));
            
           // echo $x = $this->General->test("admin");
              // $this->load->view('login');
            
            if($this->form_validation->run() == FALSE){
               
                $this->index();
                
            }else{
                
        /*  $this->db->from('mes_user');
        $this->db->where('user_name', $this->input->post('gent'));
        $this->db->where('user_password', hash("SHA256", $this->input->post('genwrd')));
        $this->db->where('user_status', 1);
        
        $this->db->select("user_fname","user_name","user_lvl","user_id");
        $hasil=$this->db->get();
                 $logresult = $hasil->result();  */
                 
             $data = array(
                'name' => $this->input->post('gent'),
                'password' => $this->input->post('genwrd')
               );    
                 
                $rest_api_base_url = 'https://neha.webip.lk/lumen/public';
                $post_endpoint = '/login_web';
                $request_data = $data;
                $response = perform_http_request('POST', $rest_api_base_url . $post_endpoint, $request_data);    
                 $det = json_decode($response); 
                 
                 if(isset($det->token)){
                     
                     $session_data = array(
                          'username' => $det->user[0]->user_name,
                          'userlevel' => $det->user[0]->user_lvl,
                          'firstname' => $det->user[0]->user_fname,
                          'depot' => $det->user[0]->user_depoid,
                          'user_id' => $det->user[0]->user_id,
                          'token' => $det->token
                                                 
                     ); 
                     
                     $this->session->set_userdata('login_data',$session_data);  
                     $_SESSION["log_key"] = $det->token;
                    // $this->Dashboard_model->dashboard();
                    
                    if(($det->user[0]->user_lvl<6)||($det->user[0]->user_lvl==15)){
                        
            $product = $this->Common_model->productcount();
         $data['item'] = $product;
         
         $product = $this->Common_model->getsalseman();
         $data['rep'] = $product;
         
         $product = $this->Common_model->getcus();
         $data['cus'] = $product;
         
         $product = $this->Common_model->getdepo();
         $data['depo'] = $product;
         
         $product = $this->Common_model->todayinv();
         $data['invtot'] = $product;
         
         $product = $this->Common_model->todayinvcount();
         $data['invtotqty'] = $product;
         
         $product = $this->Common_model->todaypay();
         $data['paytot'] = $product;
         
         $product = $this->Common_model->todaypaycount();
         $data['payqty'] = $product;
         
                        redirect('dashboard');
                    }else if ($det->user[0]->user_lvl == 9){
                        $data['content'] = "pages/stodashboard";        
                        $this->load->view('template',$data);
                    }else if($det->user[0]->user_lvl<21){
                        redirect('/refdashboard');
                        $data['content'] = "pages/refdashboard";        
                        $this->load->view('template',$data);
                    }else{
                                   $this->db->select("mes_user.*,mes_user.user_fname as fnme,mes_user.user_lname as lnme");
                                $this->db->from('mes_user');
                                //$this->db->join( "mes_user", "mes_user.user_id = mes_customer.cust_salseperson", 'left');
                                $this->db->where('user_name',$det->user[0]->user_name);
                               // $this->db->where('cust_country',$this->input->post('esup_country'));
                                $detail = $this->db->get();
                                $custd = $detail->result();
                                
                               $data["custdata"] = $custd;
    	 
    	    
                            $er['ss'] = "";
            		        $data['content'] = ('login');                       
                            $this->load->view('login',$er);
                    }
                    
                    
                     
                 }else{
                     
                      $this->session->set_flashdata('error',"Incorrect Details");
                       $this->index();
                 }
            }
											
											
        }
        
    public function logout(){
            $this->session->unset_userdata('login_data'); 
            $this->index();
        }
        
    function user_view(){
            
            
            $this->db->from('mes_user');
            $this->db->select("mes_user.*, mes_user_lvl.*,mes_depot.*");
            $this->db->join( "mes_user_lvl", "mes_user_lvl.user_lvl_val = mes_user.user_lvl", 'left');
            $this->db->join( "mes_depot", "mes_depot.dep_id = mes_user.user_depoid", 'left');
            $this->db->where('user_lvl!=', 20);
            $this->db->where('user_lvl!=', 0);
            $this->db->order_by('user_status', 'desc');
             //$this->db->from('mes_country');
           // $this->db->select("*");
            $detail = $this->db->get();
            $data = $detail->result();
            
             $det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_user' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel'>Add New User</button>
             </br>";
            
            $det[1] ="<tr>
														<th scope='col'>ID</th>
														<th>Full Name</th>
														<th>User Name</th>
														<th>Level</th>
														<th>Depot</th>
														<th>Status</th>
														<th>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
          
                for($i=0; $i<count($data); $i++){
                    if($data[$i]->user_status==1){$sts = "<td style='color:Green; font-size:25px; text-align: center;'>&#9679;</td>";}else{$sts = "<td style='color:red; font-size:25px; text-align: center;'>&#9679;</td>";}
                    $dat =  "
                              <tr scope='row'>
                                <td>".$data[$i]->user_code.$data[$i]->user_id."</td>
                                <td>".$data[$i]->user_fname." ".$data[$i]->user_lname."</td>
                                <td>".$data[$i]->user_name."</td>
                                <td>".$data[$i]->user_lvl_name."</td>
                                <td>".$data[$i]->dep_name."</td>
                                $sts
                                
                               <td style='text-align: right;'>
                               
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_user/".$data[$i]->user_id."' onclick='edit_view(this.id)' data-target='#Modal_edit'>Edit</button>
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }      
            	
           echo json_encode($det);
        }
        
    function user_add(){
              
            $this->db->from('mes_user_lvl');
            $this->db->select("*");
            $this->db->where('user_lvl_val!=',0);
            $detail = $this->db->get();
            $data = $detail->result();
            
            
           
            $datad = $this->Product_model->dpo_list();
            
            
            $det="
             <div class='form-group row'>
               
               <input type='hidden' name='table' id='table' class='form-control' readonly='' value='save_user'>
                                <label class='col-md-2 col-form-label'>Frist Name</label>
                                <div class='col-md-10'>
                                  <input type='text' name='f_name' id='f_name' class='form-control' placeholder=''>
                                 </div>
                            </div>
                            <div class='form-group row'>
                             <label class='col-md-2 col-form-label'>Last Name</label>
                                <div class='col-md-10'>
                                  <input type='text' name='l_name' id='l_name' class='form-control' placeholder=''>
                                 </div>
                            </div>
                          <div class='form-group row'>
                                <label class='col-md-2 col-form-label'>User Name</label>
                                <div class='col-md-10'>
                                  <input type='text' name='usname' id='usname' class='form-control' placeholder=''>
                                 </div>
                            </div>
                            
                            <div class='form-group row'>
                                <label class='col-md-2 col-form-label'>User Contact</label>
                                <div class='col-md-10'>
                                  <input type='text' name='ucont' id='ucont' class='form-control' placeholder=''>
                                 </div>
                            </div>
                            <div class='form-group row'>
                                <label class='col-md-2 col-form-label'>Password</label>
                                <div class='col-md-10'>
                                  <input type='password' name='psword' id='psword' class='form-control' placeholder=''>
                                 </div>
                            </div>
                            
                            <div class='form-group row'>
                                <label class='col-md-2 col-form-label'>Re Password</label>
                                <div class='col-md-10'>
                                  <input type='password' name='repsword' id='repsword' class='form-control' placeholder=''>
                                 </div>
                            </div>
                            
                             <div class='form-group row'>
                                <label class='col-md-2 col-form-label'>User Level</label>
                                <div class='col-md-10'>
                                  <select class='form-control' id='ulvl' name='ulvl'>
                                  <option value=''>Select Level</option>
                                ";
                                  
                                     foreach($data as $value) {
                                                $det.='<option value="'.$value->user_lvl_val.'">'.$value->user_lvl_name.'</option>';
                                            }     
                                  
            $det.="                      
                                        </select>
                                 </div>
                            </div>
                            
                            <div class='form-group row'>
                                <label class='col-md-2 col-form-label'>Depot</label>
                                <div class='col-md-10'>
                                  <select class='form-control' id='udpo' name='udpo'>
                                  <option value=''>Select Depot</option>
                                ";
                                  
                                     foreach($datad as $value) {
                                                $det.='<option value="'.$value->dep_id.'">'.$value->dep_name.'</option>';
                                            }     
                                  
            $det.="                      
                                        </select>
                                 </div>
                            </div>
                            
                             <div class='form-group row'>
                                <label class='col-md-2 col-form-label'>Department</label>
                                <div class='col-md-10'>
                                  <select class='form-control' id='udpm' name='udpm'>
                                  <option value=''>Select Department</option>
                                  <option value='Marketing'>Marketing</option>
                                  <option value='Stores'>Stores</option>
                                  <option value='Head Office'>Head Office</option>
                                  
                                  
                                        
                                        </select>
                                 </div>
                            </div>
                        
              ";
                        
                	
               echo json_encode($det);	
        }
        
    function user_save(){
           // $name =  $this->input->post('tc_cat');
    	  // $des =  $this->input->post('tax_value');
    	//  if( $this->session->userdata['login_data']['username']!="gayanath"){
    	      
    	  
    	   
                         $this->form_validation->set_error_delimiters('', ''); 
        
                            $this->form_validation->set_rules('f_name', 'First Name', 'required');
                            $this->form_validation->set_rules('l_name', 'Last Name', 'required');
                            $this->form_validation->set_rules('usname', 'Username', 'trim|required|is_unique[mes_user.user_name]');
                            $this->form_validation->set_rules('usname', 'Username', 'trim|required|is_unique[users.name]');
                            $this->form_validation->set_rules('psword', 'Password', 'trim|required');
                            $this->form_validation->set_rules('repsword', 'Password Not Match', 'trim|required|matches[psword]');
                            $this->form_validation->set_rules('udpo', 'Depot', 'required');
                            $this->form_validation->set_rules('udpm', 'Department', 'required');
                            $this->form_validation->set_rules('ulvl', 'User Level', 'required');
                            if($this->input->post('ulvl')==20){
                            $this->form_validation->set_rules('ucont', 'Contact', 'required');
                            }
            	   
            	   if ($this->form_validation->run() == FALSE){
                            $ret[0] = validation_errors();
                            $ret[2] = "error";
            	   }else{
            	       
            	      $data = array(
                        'fname' => $this->input->post('f_name') ,
                        'lname' => $this->input->post('l_name'),
                        'name' => $this->input->post('usname'),
                        'user_mobnum' => $this->input->post('ucont'),
                        'password' => $this->input->post('psword'),
                        'lvl' => $this->input->post('ulvl'),
                        'depo' => $this->input->post('udpo'),
                        'depart' => $this->input->post('udpm'),
                        'cuser' => $this->session->userdata['login_data']['username'],
                        'cdate' => (date('Y-m-d'))
                       );
                    
                        $rest_api_base_url = 'https://neha.webip.lk/lumen/public';
                        $post_endpoint = '/register';
                        $request_data = $data;
                        $response = perform_http_request('POST', $rest_api_base_url . $post_endpoint, $request_data);
                        
                          if($response!=""){
                            $ret[0] = "New User Created";
                        }else{
                            $ret[0] = "Please Re enter all details";
                        } 
                        
                       
            	   }
    	   
    //	  }else{
    	//      $ret[0] = "";
    	//  }
    	   
    	        $ret[1] = "showalluser";
                echo json_encode($ret);	
        }
        
     
        
    function user_edit(){
    	    $id = $this->input->post('id');
    	    
    	    $this->db->from('mes_user'); 
            $this->db->select("*");
            $this->db->join( "mes_user_lvl", "mes_user.user_lvl = mes_user_lvl.user_lvl_val", 'left');
            $this->db->join( "mes_depot", "mes_user.user_depoid =  mes_depot.dep_id", 'left');
            $this->db->where('user_id',$id);
            $detail = $this->db->get();
            $data = $detail->result();
            
            $this->db->from('mes_user_lvl');
            $this->db->select("*");
            $detailtax = $this->db->get();
            $data1 = $detailtax->result();
            
           $datam = $this->Product_model->dpo_list();
            
            $act = $inact = "";
           if($data[0]->user_status=="1"){$act = "selected";}else{$inact = "selected";}
    	    
              $det='
              <input type="hidden" name="data_id" id="data_id" class="form-control" readonly="" value="'.$data[0]->user_id.'">
               <input type="hidden" name="edit_table" id="edit_table" class="form-control" readonly="" value="update_user">
               
               
            
                <div class="form-group row">
                                <label class="col-md-2 col-form-label">Frist Name</label>
                                <div class="col-md-10">
                                 <input type="text" name="ef_name" id="ef_name" class="form-control"  value="'.$data[0]->user_fname.'">
                                 </div>
                            </div>
                            
                              <div class="form-group row">
                                <label class="col-md-2 col-form-label">Last Name</label>
                                <div class="col-md-10">
                                 <input type="text" name="el_name" id="el_name" class="form-control"  value="'.$data[0]->user_lname.'">
                                 </div>
                            </div>
                            
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">User Contact</label>
                                <div class="col-md-10">
                                 <input type="text" name="emobnum" id="emobnum" class="form-control"  value="'.$data[0]->user_mobnum.'">
                                 </div>
                            </div>
              <div class="form-group row">
                                <label class="col-md-2 col-form-label">Level</label>
                                <div class="col-md-10">
                                  <select class="form-control" id="tc_eCat" name="tc_eCat">';
                                  
                                  $det.='<option value="'.$data[0]->user_lvl_val.'" selected>'.$data[0]->user_lvl_name.'</option>';
                                  
                                     foreach($data1 as $value) {
                                                $det.='<option value="'.$value->user_lvl_val.'">'.$value->user_lvl_name.'</option>';
                                            }     
                                  
            $det.='                      
                                        </select>
                                 </div>
                            </div>
                            
                                    <div class="form-group row">
                                <label class="col-md-2 col-form-label">Depot</label>
                                <div class="col-md-10">
                                  <select class="form-control" id="udpo" name="udpo">';
                                   $det.='<option value="'.$data[0]->dep_id.'" selected>'.$data[0]->dep_name.'</option>';
                               
                                  
                                     foreach($datam as $value) {
                                                $det.='<option value="'.$value->dep_id.'">'.$value->dep_name.'</option>';
                                            }     
                                  
            $det.='                      
                                        </select>
                                 </div>
                            </div>
                            
                             <div class="form-group row">
                                <label class="col-md-2 col-form-label">Department</label>
                                <div class="col-md-10">
                                  <select class="form-control" id="udpm" name="udpm">
                                   <option value="'.$data[0]->user_departmentid.'" selected>'.$data[0]->user_departmentid.'</option>
                                  <option value="">Select Department</option>
                                  <option value="Marketing">Marketing</option>
                                  <option value="Stores">Stores</option>
                                  <option value="Head Office">Head Office</option>
                                  
                                  
                                        
                                        </select>
                                 </div>
                            </div>
                            
                             <div class="form-group row">
                                <label class="col-md-2 col-form-label">New Password</label>
                                <div class="col-md-10">
                                 <input type="password" name="npwdt" id="npwdt" class="form-control"  value="">
                                 </div>
                            </div>
                            
                             <div class="form-group row">
                                <label class="col-md-2 col-form-label">Re Password</label>
                                <div class="col-md-10">
                                 <input type="password" name="rnpwdt" id="rnpwdt" class="form-control"  value="">
                                 </div>
                            </div>
                            
                          
                            
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Status</label>
                                <div class="col-md-10">
                                      <select class="form-control" id="status" name="status">
                                            <option value="1" '.$act.'>Active</option>
                                            <option value="0" '.$inact.'>Inactive</option>
                                        </select>
                                 </div>
                            </div> 
                
                            
                            
              ';
                        
                	
               echo json_encode($det);	
    	}
    	
    function user_updt(){
        
      //  if( $this->session->userdata['login_data']['user_id']==$this->input->post('data_id')){
            
        
    	    
    	    $ret[2]="";
            
                $this->form_validation->set_error_delimiters('', ''); 
                $this->form_validation->set_rules('ef_name', 'Frist Name', 'required');
                $this->form_validation->set_rules('el_name', 'Last Name', 'required');
                $this->form_validation->set_rules('tc_eCat', 'Level', 'required');
                $this->form_validation->set_rules('udpo', 'Deport', 'required');
                $this->form_validation->set_rules('udpm', 'Department', 'required');
                if($this->input->post('tc_eCat')==20){
                    $this->form_validation->set_rules('emobnum', 'Contact Number', 'required|trim');
                }
                
                
                if(trim($this->input->post('npwdt'))!=""){
                    
                $this->form_validation->set_rules('npwdt', 'New Password', 'trim');
                $this->form_validation->set_rules('rnpwdt', 'Password Not Match', 'trim|required|matches[npwdt]');
                
                }
                
                //$this->form_validation->set_rules('rnpwdt', 'New Password', 'trim');
                
                
                if ($this->form_validation->run() == FALSE){
                    $ret[0] = validation_errors();
    	   }else{
    	       
    	    $this->db->from('mes_user'); 
            $this->db->select("*");
            $this->db->where('user_id',$this->input->post('data_id'));
            $detail = $this->db->get();
            $dt_u = $detail->result();
    	       
    	       
    	       
    	       if(trim($this->input->post('npwdt'))!=""){
    	         //  $this->db->set('password', hash("SHA256", $this->input->post('npwdt')));
                 //   $this->db->where('name', $dt_u[0]->user_name);
                 //   $result=$this->db->update('users');
    	           
    	          //$this->db->set('user_password', hash("SHA256", $this->input->post('npwdt'))); 
    	          
    	          if($this->session->userdata['login_data']['username']=="gayanath"){
    	              
    	                 $data = array(
                            'password' => $this->input->post('npwdt'),
                            'name' => $dt_u[0]->user_name
                           );
                        
                            $rest_api_base_url = 'https://neha.webip.lk/lumen/public';
                            $post_endpoint = '/update';
                            $request_data = $data;
                            $response = perform_http_request('POST', $rest_api_base_url . $post_endpoint, $request_data);
                            
                              if($response!=""){
                                $ret[0] = "Updated";
                                
                                  $data = array(
                                    'uedit_userid' => $dt_u[0]->user_name, 
                                    'uedit_details'  => "Password Change",
                                    'uedit_user'  => $this->session->userdata['login_data']['username']
                                    
                                        );
                                $this->db->insert('user_eidt_log',$data);
                            }else{
                                $ret[0] = "Please Re enter Password";
                            } 
    	          
    	              
    	          }elseif(($this->session->userdata['login_data']['username']=="chaminda")&&($this->session->userdata['login_data']['username']==$dt_u[0]->user_name)){
    	              $data = array(
                            'password' => $this->input->post('npwdt'),
                            'name' => $dt_u[0]->user_name
                           );
                        
                            $rest_api_base_url = 'https://neha.webip.lk/lumen/public';
                            $post_endpoint = '/update';
                            $request_data = $data;
                            $response = perform_http_request('POST', $rest_api_base_url . $post_endpoint, $request_data);
                            
                              if($response!=""){
                                $ret[0] = "Updated";
                                
                                  $data = array(
                                    'uedit_userid' => $dt_u[0]->user_name, 
                                    'uedit_details'  => "Password Change",
                                    'uedit_user'  => $this->session->userdata['login_data']['username']
                                    
                                        );
                                $this->db->insert('user_eidt_log',$data);
                            }else{
                                $ret[0] = "Please Re enter Password";
                            } 
    	           
    	       }elseif(($this->session->userdata['login_data']['username']!="")&&($this->session->userdata['login_data']['userlevel'] < 2)){
    	              $data = array(
                            'password' => $this->input->post('npwdt'),
                            'name' => $dt_u[0]->user_name
                           );
                        
                            $rest_api_base_url = 'https://neha.webip.lk/lumen/public';
                            $post_endpoint = '/update';
                            $request_data = $data;
                            $response = perform_http_request('POST', $rest_api_base_url . $post_endpoint, $request_data);
                            
                              if($response!=""){
                                $ret[0] = "Updated";
                                
                                  $data = array(
                                    'uedit_userid' => $dt_u[0]->user_name, 
                                    'uedit_details'  => "Password Change",
                                    'uedit_user'  => $this->session->userdata['login_data']['username']
                                    
                                        );
                                $this->db->insert('user_eidt_log',$data);
                            }else{
                                $ret[0] = "Please Re enter Password";
                            } 
    	           
    	       }else{  $ret[2] = "Unauthorized username to change password";}
    	          
        	             
    	       }
    	       
    	       if($dt_u[0]->user_depoid != $this->input->post('udpo')){
    	           
    	           
    	           
    	            $this->db->from('temp_qto_items'); 
                    $this->db->select("*");
                    $this->db->where('po_user',$dt_u[0]->user_id);
                    $detail = $this->db->get();
                    $dt_u1 = $detail->result();
                    
                   if(count($dt_u1) > 0) {
                        $ret[2] = "Can't Change Depot, Pennding Invoice Available";
                        
                $this->db->set('user_fname', $this->input->post('ef_name'));
                $this->db->set('user_lname', $this->input->post('el_name'));
                $this->db->set('user_status', $this->input->post('status'));
                //$this->db->set('user_mobnum', $this->input->post('status'));
                $this->db->set('user_lvl', $this->input->post('tc_eCat'));
                $this->db->set('user_mobnum', $this->input->post('emobnum'));
                $this->db->set('user_departmentid', $this->input->post('udpm'));
                $this->db->set('user_euser', $this->session->userdata['login_data']['username']);
                $this->db->set('user_edate', date('Y-m-d'));
                
                $this->db->where('user_id', $this->input->post('data_id'));
                $result=$this->db->update('mes_user');
                
                if($result){
                    $ret[0] = "User Details Updated";
                }else{
                    $ret[0] = "Please Re enter all details";
                }
                    }else{
                        
                            $this->db->set('user_fname', $this->input->post('ef_name'));
                            $this->db->set('user_lname', $this->input->post('el_name'));
                            $this->db->set('user_status', $this->input->post('status'));
                            $this->db->set('user_depoid', $this->input->post('udpo'));
                            $this->db->set('user_lvl', $this->input->post('tc_eCat'));
                            $this->db->set('user_mobnum', $this->input->post('emobnum'));
                            $this->db->set('user_departmentid', $this->input->post('udpm'));
                            $this->db->set('user_euser', $this->session->userdata['login_data']['username']);
                            $this->db->set('user_edate', date('Y-m-d'));
                            
                            $this->db->where('user_id', $this->input->post('data_id'));
                            $result=$this->db->update('mes_user');
                            
                            if($result){
                                $ret[0] = "User Details Updated";
                            }else{
                                $ret[0] = "Please Re enter all details";
                            }
                        
                    }
    	           
    	       }else{
    	           
    	           
    	        $this->db->set('user_fname', $this->input->post('ef_name'));
                $this->db->set('user_lname', $this->input->post('el_name'));
                $this->db->set('user_status', $this->input->post('status'));
                $this->db->set('user_depoid', $this->input->post('udpo'));
                $this->db->set('user_lvl', $this->input->post('tc_eCat'));
                $this->db->set('user_mobnum', $this->input->post('emobnum'));
                $this->db->set('user_departmentid', $this->input->post('udpm'));
                $this->db->set('user_euser', $this->session->userdata['login_data']['username']);
                $this->db->set('user_edate', date('Y-m-d'));
                
                $this->db->where('user_id', $this->input->post('data_id'));
                $result=$this->db->update('mes_user');
                
                if($result){
                    $ret[0] = "User Details Updated";
                }else{
                    $ret[0] = "Please Re enter all details";
                } 
    	       }
    	     
    	   }
    	   
       // }else{
       //     $ret[0] = "";
       // }
        
    	    $ret[1] = "showalluser";
    	    echo json_encode($ret);	
    	}
    	
    function userlvl_view(){
            
            $this->db->from('mes_user_lvl');
            $this->db->select("*");
            $detail = $this->db->get();
            $data = $detail->result();
            
           
            
             $det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_userlvl' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel'>Add New User Level</button></br>";
            
            $det[1] ="<tr>
														<th scope='col'>ID</th>
														<th>Level Name</th>
														<th>Level Value</th>
														<th>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
          
                for($i=0; $i<count($data); $i++){
                   // if($data[$i]->user_status==1){$sts = "<td style='color:Green; font-size:25px; text-align: center;'>&#9679;</td>";}else{$sts = "<td style='color:red; font-size:25px; text-align: center;'>&#9679;</td>";}
                   $x = $i + 1;
                    $dat =  "
                              <tr scope='row'>
                              <td>$x</td>
                                <td>".$data[$i]->user_lvl_name."</td>
                                <td>".$data[$i]->user_lvl_val."</td>
                                
                               
                                
                               <td style='text-align: right;'>
                               
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_userlvl/".$data[$i]->user_lvl_id."' onclick='edit_view(this.id)' data-target='#Modal_edit'>Edit</button>
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }      
            	
           echo json_encode($det);
        }
        
    function userlvl_new(){
              
    
            
            
            $det="
             <div class='form-group row'>
               
               <input type='hidden' name='table' id='table' class='form-control' readonly='' value='save_userlvl'>
                                <label class='col-md-2 col-form-label'>Name</label>
                                <div class='col-md-10'>
                                  <input type='text' name='f_name' id='f_name' class='form-control' placeholder=''>
                                 </div>
                            </div>
                           
                         
                          
                            </div>
                            
                            <div class='form-group row'>
               
              
                                <label class='col-md-2 col-form-label'>Level</label>
                                <div class='col-md-10'>
                                  <input type='text' name='lvl' id='lvl' class='form-control' placeholder=''>
                                 </div>
                            </div>
                           
                         
                          
                            </div>
                        
              ";
                        
                	
               echo json_encode($det);	
        }
        
    function userlvl_save(){
            
              $this->form_validation->set_error_delimiters('', ''); 
              
                    $this->form_validation->set_rules('f_name', 'User Level Name', 'required|is_unique[mes_user_lvl.user_lvl_name]');
                    $this->form_validation->set_rules('lvl', 'User Level Value', 'trim|required');
                   
                    
                     if ($this->form_validation->run() == FALSE){
                    $ret[0] = validation_errors();
                    $ret[2] = "error";
    	   }else{
    	       
    	     $data = array(
                'user_lvl_name' => $this->input->post('f_name'), 
                'user_lvl_val'  => $this->input->post('lvl')
             
            );
            $result = $this->db->insert('mes_user_lvl',$data);
                if($result){
                    $ret[0] = "New User Level  Saved";
                }else{
                    $ret[0] = "Please Re enter all details";
                }
    	   }
                        
                	
               echo json_encode($ret);
        }
    	
  //profile data
  
  function pro_view(){
      
            $this->db->from('mes_profile');
            $this->db->select("*");
            $this->db->where('pro_id', 1);
            $detail = $this->db->get();
            $prodata = $detail->result();
      
        $data['content'] = ('pages/profile'); 
        $data['pro'] = $prodata; 
		$this->load->view('template',$data);
      
  }
  
  function pro_save(){
      
                    $this->form_validation->set_error_delimiters('', ''); 
              
                    $this->form_validation->set_rules('cname', 'Company Name', 'required');
                    $this->form_validation->set_rules('madd', 'Company Address', 'required');
                    $this->form_validation->set_rules('connum', 'Contact Numbers', 'required');
                    
                     if ($this->form_validation->run() == FALSE){
                    $ret[0] = validation_errors();
                    $ret[2] = "error";
    	   }else{
    	       
    	      
    	      $this->db->set('pro_name', $this->input->post('cname'));
    	      $this->db->set('pro_add', $this->input->post('madd'));
    	      $this->db->set('pro_num', $this->input->post('connum'));
    	      $this->db->set('pro_web', $this->input->post('web'));
    	      $this->db->set('pro_email', $this->input->post('email'));
    	      $this->db->set('pro_reg', $this->input->post('rnum'));
    	      $this->db->set('pro_deladd', $this->input->post('dadd'));
    	      $this->db->set('pro_heslogans', $this->input->post('hsolg'));
    	      $this->db->set('pro_fslogans', $this->input->post('fslog'));
    	      $this->db->set('pro_sfslogans', $this->input->post('sfslog'));
    	      $this->db->set('pro_bank', $this->input->post('bank'));
    	      
    	      $this->db->set('pro_cuser', 'admin');
    	      $this->db->set('pro_cdate', date('Y-m-d'));
    	     
                $this->db->where('pro_id', 1);
                $result=$this->db->update('mes_profile');
          
                if($result){
                    $ret[0] = "Detail Saved";
                }else{
                    $ret[0] = "Error";
                }
    	   }
                        
                	
               echo json_encode($ret);	
        }
        
  function uploadsig(){
            
                if(isset($_FILES["sign"]["name"]))  
           {  
                $config['upload_path'] = './assets/images/';  
                $config['allowed_types'] = 'jpg|jpeg|png|gif';  
                $config['max_size']      = 150;
                $this->load->library('upload', $config);  
                if(!$this->upload->do_upload('sign'))  
                {  
                     echo $this->upload->display_errors();  
                     $arr[0]="Error";
                     
                }  
                else  
                {  
                     $data = $this->upload->data();  
                     $arr[0] ='<img src="'.base_url().'assets/images/'.$data["file_name"].'" width="300" height="225" class="img-thumbnail" />';
                     //$arr[0]="ok";
                    
                }  
                
                echo json_encode($arr);
           }  
           
          
        }
        
  function view_sales(){
            
            $this->db->from('mes_user');
            $this->db->select("mes_user.*, mes_user_lvl.*,mes_depot.*");
            $this->db->join( "mes_user_lvl", "mes_user_lvl.user_lvl_val = mes_user.user_lvl", 'left');
            $this->db->join( "mes_depot", "mes_depot.dep_id = mes_user.user_depoid", 'left');
            $this->db->where('user_lvl',20);
            $this->db->order_by('user_status', 'desc'); 
           // $this->db->select("*");
            $detail = $this->db->get();
            $data = $detail->result();
            
             $datad = $this->Product_model->dpo_list();
            
          
            
            if($data[0]->all_inactive == 0){ $inctivebtn = "<button type='button' class='btn btn-danger' type='submit' id='inactive_user' onclick='inactive_data(this.id)' >Inactive All</button>";}
            else{$inctivebtn = " <button type='button' class='btn btn-danger' type='submit' id='inactive_user' onclick='inactive_data(this.id)' >Active All</button>";}
            
             $det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_user' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel'>Add New Sales Person</button>
            $inctivebtn</br>";
            
            $det[1] ="<tr>
														<th scope='col'>ID</th>
														<th>Full Name</th>
														<th>User Name</th>
														
													<th>Contact</th>
														<th>Level</th>
														<th>Depot</th>
														<th>Status</th>
														<th>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
          
                for($i=0; $i<count($data); $i++){
                    if($data[$i]->user_status==1 || $data[$i]->all_inactive==1){$sts = "<td style='color:Green; font-size:25px; text-align: center;'>&#9679;</td>";}else{$sts = "<td style='color:red; font-size:25px; text-align: center;'>&#9679;</td>";}
                    $dat =  "
                              <tr scope='row'>
                                <td>".$data[$i]->user_code.$data[$i]->user_id."</td>
                                <td>".$data[$i]->user_fname." ".$data[$i]->user_lname."</td>
                                <td>".$data[$i]->user_name."</td>
                                <td>".$data[$i]->user_mobnum."</td>
                                <td>".$data[$i]->user_lvl_name."</td>
                                <td> ".$data[$i]->dep_name."</td>
                                $sts
                                
                               <td style='text-align: right;'>
                               
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_user/".$data[$i]->user_id."' onclick='edit_view(this.id)' data-target='#Modal_edit'>Edit</button>
                                <!--button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->user_id."' onclick='bill_view(this.id)' data-target='#Modal_bill'>Bill</button>
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->user_id."' onclick='new_bill(this.id)' data-target='#Modal_bill'>New Bill</button-->
                                
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }      
            	
           echo json_encode($det);
            
        }
        
  function all_inactive(){
        
         if(isset($this->session->userdata['login_data']['username'])){
             
	               if($this->session->userdata['login_data']['userlevel'] < 2) {
	                   
	                   
	                    $this->db->from('mes_user');
                        $this->db->select("*");
                        $this->db->where('user_lvl', 20);
                        $detail = $this->db->get();
                        $prodata = $detail->result();
                        
                        if($prodata[0]->all_inactive == 0){
                            
                        $this->db->set('all_inactive', 1);
	                   $this->db->where('user_lvl', 20);
	                   $result=$this->db->update('mes_user');
	                   
	                    if($result){
                            $ret = "All Salse Persons Inactive";
                           // $ret[1] = "Active All";
                            
                         }else{
                            $ret = "Please Re Inactive";
                                } 
                        }else{
                            
                             $this->db->set('all_inactive', 0);
	                   $this->db->where('user_lvl', 20);
	                   $result=$this->db->update('mes_user');
	                   
	                    if($result){
                            $ret = "All Salse Persons Active";
                           // $ret[1] = "Active All";
                            
                         }else{
                            $ret = "Please Re Active";
                                } 
                        }
	                   
	                  
	                   
	                   //$ret[0] = "Unauthorized User";
	               }else{
	                   $ret = "Unauthorized User";
	               }
	    }else{
	        $er['ss'] = "";
		        $data['content'] = ('login');                       
                $this->load->view('login',$er);
	    }
	    
	    echo json_encode($ret);	
    }
    
    //Route 
  function view_route(){
      
            $this->db->from('mes_route');
            $this->db->select("*");
            $this->db->join( "mes_user", "mes_user.user_id = mes_route.ref_id", 'left');
            $detail = $this->db->get();
            $data = $detail->result();
          
            
            $det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_route' onclick='dis_route()'>Add Route</button>
            <!--button type='button' class='btn btn-outline-danger' type='submit' id='new_route' onclick='cus_route()'>Customer Route</button-->
             <a href='https://neha.webip.lk/change_route'>    <button class='btn btn-outline-danger'>Change Route</button>   </a> </br>";
            
            $det[1] ="<tr>
														<th scope='col'>ID</th>
														<th>Ref</th>
														<th>Root</th>
														<th style='width: 600px;'>Cities</th>
													    <th style='width: 60px;'>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
          
                for($i=0; $i<count($data); $i++){
                    //$data[$i]->cities;
                         $rt =  explode(",",$data[$i]->cities);
                  $city ="";  
                    for($j=0; $j<count($rt); $j++){
                        if($rt[$j]!=""){
                            $this->db->from('cities');
                            $this->db->select("name_en");
                            $this->db->where('id', $rt[$j]);
                            $detail = $this->db->get();
                            $ct = $detail->result();
                        $city = $city.",".$ct[0]->name_en;
                        }
                                  
                    }
                    
                    if($data[$i]->status==1){$sts = "<td style='color:Green; font-size:25px; text-align: center;'>&#9679;</td>";}else{$sts = "<td style='color:red; font-size:25px; text-align: center;'>&#9679;</td>";}
                    $dat =  "
                              <tr scope='row'>
                                <td>".$data[$i]->route_code.$data[$i]->route_id."</td>
                                <td> ".$data[$i]->user_fname." ".$data[$i]->user_lname."</td>
                                <td> ".$data[$i]->route_name."</td>
                                <td style='word-wrap:break-word; width:300px'> ".$city."</td>
                                
                               <td style='text-align: right;'>
                               
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->route_id."' onclick='rtedit_view(this.id)'>Edit</button>
                                
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }      
            	
           echo json_encode($det);
  }
  
  function save_rt(){
      if(!empty($city)){
          $city = implode(',', $this->input->post('city'));
      }else{
          $city = "";
      }
        
        if($this->input->post('cus')!=""){
            $cus = implode(',', $this->input->post('cus'));
        }else{
            $cus = "";
        }
        
        
        $data = array( 
                'route_name'  => $this->input->post('rt_name'),
                'ref_id'  => $this->input->post('ref'),
                'cities'  => $city,
                'cust'  => $cus,
                'created_on'  => date('Y-m-d'),
                'created_by'  => $this->session->userdata['login_data']['username']
             
            );
            $result = $this->db->insert('mes_route',$data);
                if($result){
                    $ret = "New User Route  Saved";
                }else{
                    $ret = "Please Re enter all details";
                }
        echo json_encode($ret);	       
  }
  
  function edt_rt(){
                        $this->db->from('mes_route');
                            $this->db->select("*");
                            $this->db->where('route_id', $this->input->post('id'));
                            $detail = $this->db->get();
                            $rt = $detail->result();
                            
                        $inv = array();   
         $all = explode(",",$rt[0]->cities);
                 for($i=0; $i<count($all); $i++){
                     if($all[$i]!=""){
                         $this->db->select("*");    
                    $this->db->from('cities');
                    $this->db->where('id',$all[$i]);
                    $query = $this->db->get();
                    $cty =$query->result();    
                    
                    $inv[] = array($cty[0]->id,$cty[0]->name_en);
                     }
                     
                 }                 
                 
                 $cus = array();   
         $allc = explode(",",$rt[0]->cust);
                 for($i=0; $i<count($allc); $i++){
                     if($allc[$i]){
                         $this->db->select("cust_company,cust_id");    
                    $this->db->from('mes_customer');
                    $this->db->where('cust_id',$allc[$i]);
                    $query = $this->db->get();
                    $cty =$query->result();    
                    
                    $cus[] = array($cty[0]->cust_id,$cty[0]->cust_company);
                     }
                     
                 } 
             
                $this->db->select("cust_company,cust_id");    
                    $this->db->from('mes_customer');
                    $this->db->where('cust_salseperson',$rt[0]->ref_id);
                    $query = $this->db->get();
                    $allcus =$query->result(); 
                 
                 $data['det'] = $inv; 
                 $data['cust'] = $cus; 
                 $data['allcus'] = $allcus; 
                 
                 $data['rt'] = $rt;     
      echo json_encode($data);
  }
  
  function updt_rt(){
      
     // echo($this->input->post('previouse')) ;
      if(!empty($this->input->post('edcity'))){
          $city = implode(',', $this->input->post('edcity'));
          $allcity = $this->input->post('previouse').$city;
      }else{
          $allcity = $this->input->post('previouse');
      }
      
      if(!empty($this->input->post('edcus'))){
          $custt = implode(',', $this->input->post('edcus'));
          $cust = $this->input->post('precity').$custt;
      }else{
          $cust = $this->input->post('precity');
      }
      
      
      
                    $this->db->set('route_name', $this->input->post('edtrt_name'));
                    $this->db->set('ref_id', $this->input->post('edt_ref'));
                    $this->db->set('cities', $allcity);
                    $this->db->set('cust', $cust);
	                   $this->db->where('route_id',$this->input->post('edtrt_id'));
	                   $result=$this->db->update('mes_route');
      if($result){
          $data = "Success";
      }else{
          $data = "Please re enter all data";
      }
      
      echo json_encode($data);
  }
  
  function chng_rt(){
      
   $rep = $this->Product_model->salserep_list();
   
   $data['ref'] = $rep;
      
        $data['content'] = ('pages/changeroute'); 
        $this->load->view('template',$data);
  }
  
  
        
}

