<?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['pointmaster_id'])){
	$pointDetail = $pointmasterObj->fromId($_REQUEST['pointmaster_id']);
    //print_r($pointDetail->pointmaster_id);exit;
}



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

    $data=array(
        'pointmaster_title' => trim($_POST['pointmaster_title']),
        'pointmaster_category_id'  => $_POST['pointmaster_category_id'],
        'pointmaster_domain_id'  => $_POST['pointmaster_domain_id'],
        'pointmaster_code'  => trim($_POST['pointmaster_code']),
        'pointmaster_created_by' => $_SESSION['admin_id'],
        'pointmaster_description'  => trim($_POST['pointmaster_description']),
        'pointmaster_status'  => '1',
        'pointmaster_created_at' => date('Y-m-d H:i:s'),
    );
    if($_POST['pointmaster_sender']!=""){
        $data['pointmaster_sender']=$_POST['pointmaster_sender'];
    }
    if($_POST['pointmaster_receiver']!=""){
        $data['pointmaster_receiver']=$_POST['pointmaster_receiver'];
    }
    // if($_POST['pointmaster_campaign_id']!=""){
    //     $data['pointmaster_campaign_id']=$_POST['pointmaster_campaign_id'];
    //     $messagecentercampaignObj = new messagecentercampaign();
    //     $tractingDetails =$messagecentercampaignObj->fromId($_POST['pointmaster_campaign_id']);
    //     if(!is_null($tractingDetails->tracking_id) && $tractingDetails->tracking_id!=""){
    //         $data['pointmaster_tracking_id']=$tractingDetails->tracking_id;
    //     }
    // }
    if($_POST['pointmaster_clinic_id']!=""){
        $data['pointmaster_clinic_id']=$_POST['pointmaster_clinic_id'];
    }
    if($_POST['pointmaster_frequency']!=""){
        $data['pointmaster_frequency']=$_POST['pointmaster_frequency'];
    }
    if($_POST['pointmaster_tracking_id']!=""){
        $data['pointmaster_tracking_id']=$_POST['pointmaster_tracking_id'];
    }
    if($_POST['pointmaster_unit_point']!=""){
        $data['pointmaster_unit_point']=$_POST['pointmaster_unit_point'];
    }
    if($_POST['pointmaster_total_points']!=""){
        $data['pointmaster_total_points']=$_POST['pointmaster_total_points'];
    }

    if($_POST['pointmaster_campaign_sdate']!=""){
        $data['pointmaster_campaign_sdate']=date("Y-m-d", strtotime($_POST['pointmaster_campaign_sdate']));
    }

    if($_POST['pointmaster_campaign_edate']!=""){
        $data['pointmaster_campaign_edate']=date("Y-m-d", strtotime($_POST['pointmaster_campaign_edate']));
    }

    if($_POST['pointmaster_end_date']!=""){
        $data['pointmaster_end_date']=date("Y-m-d", strtotime($_POST['pointmaster_end_date']));
    }
    
    if (isset($_REQUEST['pointmaster_id']) && $_REQUEST['pointmaster_id'] != ''){
        $id=$_REQUEST['pointmaster_id'];
        $rs=$pointmasterObj->updateRecords($data,$id);
        if ($rs){
            header('Location:pointlist.php');
            exit;
        }
    }else{
         $where = "pointmaster_status='1' AND pointmaster_category_id='".$_POST['pointmaster_category_id']."' ";
         if($_POST['pointmaster_clinic_id']!=""){
            $where .=" AND pointmaster_clinic_id='".$_POST['pointmaster_clinic_id']."'";
         }
         // if($_POST['pointmaster_tracking_id']!=""){
         //    $where .=" AND pointmaster_tracking_id='".$_POST['pointmaster_tracking_id']."'";
         // }
         $pointcategoryObj = new pointcategory();
         $pointcategoryDetails =$pointcategoryObj->fromId($_POST['pointmaster_category_id']);
         if($pointcategoryDetails && $pointcategoryDetails->pointcategory_title=="NEW XCARE APPOINTMENT"){
            $where .=" AND pointmaster_domain_id='".$_POST['pointmaster_domain_id']."'";
         }
         $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;
        }
    }
}
$domainObj = new domain();
$alldomain =$domainObj->getAllRecords(array());

$pointcategoryObj = new pointcategory();
$allcategory =$pointcategoryObj->getAllRecords(array());

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

?>