<?php 

// Include main file
include_once('../libs/main.php');

// Check admin Login
include_once('checkLogin.php');

// Logical section of page
$pointmasterObj =  new  pointmaster();

/*$adminlogObj = new admin_log();
$modulepagesObj = new module_pages();
$filename=basename($_SERVER['SCRIPT_FILENAME']);

if($filename == 'adminadd.php')
{
    $module_id = $adminlogObj->getModuleId($filename);
    $page_id = $adminlogObj->getPageId($filename);
    $allpages = $modulepagesObj->fromId($page_id);
    $pagename = $allpages->page;
}*/

if(isset($_REQUEST['act']) && $_REQUEST['act']=='edit' && isset($_REQUEST['point_id'])){
	$pointDetail = $pointmasterObj->fromId($_REQUEST['point_id']);
    //print_r($pointDetail->pointmaster_id);exit;
}



if(isset($_REQUEST['act']) && $_REQUEST['act']=='load' && isset($_REQUEST['point_type']) && $_REQUEST['point_type'] != "")
{

    
    
    $data=array(
        'pointmaster_title' => trim($_POST['point_title']),
        'pointmaster_type'  => $_POST['point_type'],
        'pointmaster_code'  => trim($_POST['point_code']),
        'pointmaster_sender'  => trim($_POST['point_sender']),
        'pointmaster_receiver'  => trim($_POST['point_receiver']),
        'pointmaster_created_by' => $_SESSION['admin_id'],
        'pointmaster_status'  => '1',
        'pointmaster_created_at' => date('Y-m-d H:i:s'),
    );

    
    if (isset($_REQUEST['point_id']) && $_REQUEST['point_id'] != '')
    {
    
        $id=$_REQUEST['point_id'];
        
        $rs=$pointmasterObj->updateRecords($data,$id);
        if ($rs){
            
            
            header('Location:pointlist.php');
            exit;
        }
        
        
    }else{

         $where = "pointmaster_status='1' AND pointmaster_type='".$_POST['point_type']."' ";
         $data_chk = array("where"=>$where);
         $pointexist = $pointmasterObj->fromFields($data_chk);
        
         if($pointexist)
         {

            $data_up=array(
            'pointmaster_status'=>'0',
            'pointmaster_inactive_at'=>date('Y-m-d H:i:s'),
            'pointmaster_updated_at'=>date('Y-m-d H:i:s'),
            );
            $id = $pointexist->pointmaster_id;
            
            $point_upd=$pointmasterObj->updateRecords($data_up,$id);
            if($point_upd)
            {
                $point_add  = $pointmasterObj->addRecords($data);
            }
            

         }else{
            $point_add  = $pointmasterObj->addRecords($data);
         }
        
            
        
        if($point_add){

            header('Location:pointlist.php');
            exit;
        }
    }
}


// Include Template 
include_once('../libs/routs.php')

?>