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

class Admin_Edit extends CI_Controller {


	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
        
    }
    
    public function Admin_Data($username)
	{
        $this->load->model('Show_Data');

        if($this->Show_Data->User_data($username))
            {   
                if($this->Show_Data->LoginData($username))
                {
                    $all_data['Ldata']=$this->Show_Data->LoginData($username);
                }
                $all_data['data']=$this->Show_Data->User_data($username);
                /*echo " "."admin ".$data[0]->name." ".$data[0]->email." ".$data[0]->type;
                echo " "."admin ".$data[1]->name." ".$data[1]->email." ".$data[1]->type;*/

                $this->load->view('Edit_Details_Admin',$all_data);
                //echo " ".$all_data[0]->id;
            }

		//$this->load->view('Edit_Details_Client');
    }
    
    public function Update_Data()
	{
        $username=$this->input->post('mid');
        $type=$this->input->post('type');
        $password=$this->input->post('pwd');
        $name=$this->input->post('name');
        $email=$this->input->post('email');
        $mobile=$this->input->post('mobile');
        $state=$this->input->post('state');
        $city=$this->input->post('city');
        $pin=$this->input->post('pin');
        $address=$this->input->post('address');
        $gender=$this->input->post('gender');
        $office=$this->input->post('office');
        $mobile2=$this->input->post('mobile2');
        $fax=$this->input->post('fax');
        $email2=$this->input->post('email2');
        $residance=$this->input->post('residance');
        $designation=$this->input->post('designation');
        $dob=$this->input->post('dob');

        $edit_data=array();
        $edit_data2=array();

        if (!empty($username)) {
            $edit_data['username'] = $username;
            $edit_data2['username'] = $username;
        }

        if (!empty($type)) {
            $edit_data['type'] = $type;
        }

        if (!empty($password)) {
            $edit_data2['password'] = md5($password);
        }

        if (!empty($name)) {
            $edit_data['name'] = $name;
        }

        if (!empty($email)) {
            $edit_data['email'] = $email;
        }

        if (!empty($mobile)) {
            $edit_data['mobile'] = $mobile;
        }

        if (!empty($state)) {
            $edit_data['state'] = $state;
        }

        if (!empty($city)) {
            $edit_data['city'] = $city;
        }

        if (!empty($pin)) {
            $edit_data['pin'] = $pin;
        }

        if (!empty($address)) {
            $edit_data['address'] = $address;
        }

        if (!empty($gender)) {
            $edit_data['gender'] = $gender;
        }

        if (!empty($office)) {
            $edit_data['office'] = $office;
        }

        if (!empty($mobile2)) {
            
        }

        if (!empty($fax)) {
            $edit_data['fax'] = $fax;
        }

        if (!empty($dob)) {
            $edit_data['DOB'] = $dob;
        }

        if (!empty($residance)) {
            $edit_data['residance'] = $residance;
        }

        if (!empty($designation)) {
            $edit_data['designation'] = $designation;
        }

        if (!empty($email2)) {
            $edit_data['email2'] = $email2;
        }

        $this->load->model('Admin_Edit_Model');

        if($this->Admin_Edit_Model->Edit_Data_Client_Admin($edit_data,$this->uri->segment('3')))
            {   
               // $all_data['data']=$this->Client_Edit_Model->Edit_Data_Client($edit_data,$username);
                /*echo " "."admin ".$data[0]->name." ".$data[0]->email." ".$data[0]->type;
                echo " "."admin ".$data[1]->name." ".$data[1]->email." ".$data[1]->type;*/
                
                if(!empty($username)  || !empty($password))
                {
                    if($this->Admin_Edit_Model->Edit_Data_Client_Admin_Main($edit_data2,$this->uri->segment('3')))
                    {
                        $this->session->set_flashdata('error','data is updated');
                        redirect(base_url().'index.php/Admin_Edit/Admin_Data/'.$username);
                    }

                    else
                    {
                        //echo "<script>alert('update done')</script>";

                        $this->session->set_flashdata('error','data is updated');
                        redirect(base_url().'index.php/Admin_Edit/Admin_Data/'.$username);
                    }
                }

                $this->session->set_flashdata('error','data is updated');
                redirect(base_url().'index.php/Admin_Edit/Admin_Data/'.$username);
               
            }

            else
            {
                //echo $edit_data;
                echo "<script>alert('not updated')</script>"; 
                //echo $res;
                //echo "<br>";
            }

		//$this->load->view('Edit_Details_Client');
    }
    
    public function Delete_Data($username)
	{
        
        echo $username;
        
        $this->load->model('Admin_Edit_Model');

        if($this->Admin_Edit_Model->Delete_Data_Client_Admin($this->uri->segment('3')))
            {   
                if($this->Admin_Edit_Model->Delete_Data_Client_Admin_Main($this->uri->segment('3')))
                {   
                   echo "Deleted By admin";
                }
            }

		//$this->load->view('Edit_Details_Client');
    }
}

?>