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 = '')
	{

		$isActiveFilter = 1 ;  // default flag for active data
		$showArchive    = 0 ; //  default flag for inactive data

		if ($this->request->getMethod() === 'POST') {

			$ConfigID = $this->request->getPost('ConfigID');
			$showArchive = $this->request->getPost('showArchive');
			//reversing condition that when user wants deleted data (archived)to be shown , then we are making active filter 0
				if($showArchive){ 
					$isActiveFilter = 0;
				}
		} 

		if ($this->request->getMethod() === 'GET') {
			$ConfigID = $this->request->getVar('ConfigID');
		} 

		$data['showArchive'] = $showArchive;

		$data['master'] = $this->configmodel->GetConfigCenterMaster($ConfigID);

		$data['child'] = $this->configmodel->GetConfigCenterDetails($ConfigID,$isActiveFilter);
		$data['ConfigID'] = $ConfigID;
		
		$this->global['pageTitle'] = 'Edit Config';
		$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 = str_replace(" ","",$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');
		$ConfigID = $this->request->getVar('ConfigID');
		$where = ['isActive'=> $value];
	    $result = $this->configmodel->activeInactiveConfigValue($key, $where);
		
		return redirect()->to(base_url('configurationctrl/editconfig?ConfigID=' . $ConfigID));


	 }
	 public function getPONOcreatedBasedOnConfig(){
		$data = $this->request->getPost();
		$key_id    = $data['key_id'];
		$result = $this->configmodel->getPONOcreatedBasedOnConfig($key_id);
		return $this->response->setJSON($result);
		// return $result;

	 }
	 

	 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();
	} 

	public function saveConfigValue(){
		
		$data = $this->request->getPost();
		$configId = $data['ConfigID'];
		$configValue = $data['ConfigValue'];
		$key         = $data['key']??'';

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

		if($result){
			return $this->response->setJSON(['status'=>true]);
		}else{
			return $this->response->setJSON(['status'=>true]);
		}
	}

	



}