load->library('form_validation'); $this->configmodel = new Config_model(); $this->session = session(); $this->isLoggedIn(); } public function index() { $searchText = $this->request->getPost('searchText'); $data['searchText'] = $searchText; $data['userRecords'] = $this->configmodel->Configlisting(); $this->global['pageTitle'] = 'Config Listing'; $this->loadViews("configlisting", $this->global, $data, NULL); } function addconfig() { $this->global['pageTitle'] = 'Add New Config'; $this->loadViews("addconfig", $this->global, [], NULL); } function saveconfig() { // echo "
";
		// dd($this->request->getPost());
		$ConfigName = $this->request->getPost('configurationname');
		$Rowcount =  $this->request->getPost('txtRowCount');
		$Comments = $this->request->getPost('Comments');
		$chked =  $this->request->getPost('IsActive');
		
		$config = array('ConfigName' => $ConfigName, 'Comments' => $Comments);
		$id = $this->configmodel->addconfigmaster($config);		
		
		if($id){
			for ($i = 1; $i <= $Rowcount; $i++) {
					$ConfigValue = $this->request->getPost('ConfigValue' . $i);
					$configvalue = array('Config_ID' => $id, 'ConfigValue' => $ConfigValue);
					$result = $this->configmodel->addconfigdetails($configvalue);
			}
			$this->session->setFlashdata('success', 'New Configuration Created successfully!');
		}else{
			$this->session->setFlashdata('error', 'Configuration Record Not Created!');	

		}
        return redirect()->route('configlisting');        
	}

	function editconfig($ConfigID = '')
	{
		$ConfigID = $this->request->getVar('ConfigID'); 
		$data['master'] = $this->configmodel->GetConfigCenterMaster($ConfigID);
		$data['child'] = $this->configmodel->GetConfigCenterDetails($ConfigID);
		
		$this->global['pageTitle'] = 'Edit Config';
		// echo "
";
		// print_r($data);die;
		$this->loadViews("editconfig", $this->global, $data, NULL);
	
	}

	function updateconfig()
	{
		// dd($this->request->getPost());
		$ConfigId = $this->request->getPost('ConfigId');
		$ConfigName = $this->request->getPost('ConfigName');
		$Comments = $this->request->getPost('Remarks');
		$Rowcount =  $this->request->getPost('txtRowCount');

		$config = array('Config_ID' => $ConfigId, 'ConfigName' => $ConfigName, 'Comments' => $Comments);
		$result = $this->configmodel->updateconfig($config, $ConfigId);

		$child_result = false ;
		
		for ($i = 1; $i <= $Rowcount; $i++) {	
			$child_primary_key = $this->request->getPost('DbKey' . $i);
			$config_value = $this->request->getPost('configVal' . $i);
			$child_arr = array('ConfigValue' => $config_value);
			if((int)$child_primary_key != 0){	
				$child_result = $this->configmodel->updateconfigdetails($child_arr, $child_primary_key);	
			}
			if((int)$child_primary_key == 0){
				$child_arr['Config_ID'] = $ConfigId;
				$child_result = $this->configmodel->addconfigdetails($child_arr);
			}
		}

			$this->session->setFlashdata('success', 'Configuration Updated successfully!');
		
            return redirect()->route('configlisting');        
	}


	function deleteConfig(){

		$ConfigID = $this->request->getVar('ConfigID'); 
		$result = $this->configmodel->updateconfig(['isActive' => 0],$ConfigID);

		if ($result > 0) {
			$this->session->setFlashdata('success', 'Configuration Deleted successfully!');
		} else {
			$this->session->setFlashdata('error', 'Configuration Record Not Deleted!');
        }

		return redirect()->route('configlisting'); 
	
	 }
	
	 function reActivateConfig(){

		$ConfigID = $this->request->getVar('ConfigID'); 
		$result = $this->configmodel->updateconfig(['isActive' => 1],$ConfigID);

		if ($result > 0) {
			$this->session->setFlashdata('success', 'Configuration Re Activated successfully!');
		} else {		
			$this->session->setFlashdata('error', 'Configuration Record Not Re Activated!');
        }

		return redirect()->route('configlisting'); 

	 }

	 public function configNameCheck(){

		$data = $this->request->getPost();
		$configName = str_replace(" ","",$data['configName']);
		$configId   = $data['configId'];

		$result = $this->configmodel->configNameCheck($configId,$configName);

	//	dd($result);

		return $this->response->setJSON($result);

	 }

	 public function configValueCheck(){

		$data = $this->request->getPost();
		$configId    = $data['configId'];
		$configValue = $data['ConfigValue'];

		$result = $this->configmodel->configValueCheck($configId, $configValue);

		return $this->response->setJSON($result);

	 }

	 public function activeInactiveConfigValue(){

		$key = $this->request->getVar('key');
		$value = $this->request->getVar('value');
		$where = ['isActive'=> $value];
	    $result = $this->configmodel->activeInactiveConfigValue($key, $where);
        return redirect()->back();
	 }
	 

	 public function deleteConfigValue(){

		$configId = $this->request->getVar('ConfigID');
		$configValue = $this->request->getVar('ConfigValue');

		$result = $this->configmodel->deleteConfigValue($configId, $configValue);

        return redirect()->back();


	 }

	public function reActivateConfigValue(){

		$configId = $this->request->getVar('ConfigID');
		$configValue = $this->request->getVar('ConfigValue');

		$result = $this->configmodel->reActivateConfigValue($configId, $configValue);

        return redirect()->back();
	} 



}