<?php 
// Include main file
include_once('../libs/main.php');
// Check xcare_offer Login
include_once('checkLogin.php');



$xcare_offerObj =  new  xcare_offer();
$ext = array('image/png','image/jpg','image/jpeg');
$adminlogObj = new admin_log();
$modulepagesObj = new module_pages();

$filename=basename($_SERVER['SCRIPT_FILENAME']);
if($filename == 'offeradd.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['xcare_offer_id']))
{
    $xcare_offerDetail = $xcare_offerObj->fromId($_REQUEST['xcare_offer_id']);
}

if(isset($_POST['submit'])){
//if(isset($_REQUEST['act']) && $_REQUEST['act']=='load' && isset($_REQUEST['offer_title']) && $_REQUEST['offer_title'] != "")
//{
       
    	$offerData=array(
          'xcare_offer_title'=>trim($_POST['offer_title']),
        //   'xcare_offer_link' =>trim($_POST['offer_link']),
          'xcare_offer_desc' =>trim($_POST['offer_desc']),
          'xcare_offer_button' =>trim($_POST['offer_button']),
          'xcare_offer_expired_at' =>date("Y-m-d", strtotime(trim($_POST['offer_expire']))),
          'xcare_offer_domain_id' =>trim($_POST['offer_domain_id']),
          'xcare_offer_state' =>trim($_POST['state']),
          'xcare_offer_tnc' =>trim($_POST['tnc']),
          'xcare_offer_city' =>trim($_POST['city']),
          'xcare_offer_postalcode' =>trim($_POST['zipcode']),
          'xcare_offer_country' =>trim($_POST['country']),
          'xcare_offer_points' =>trim($_POST['offer_points']),
         );
        
        
        if(isset($_POST['offer_status']) && $_POST['offer_status'] == 'on'){
            $offerData['xcare_offer_status']           = '1';
        }else{
            $offerData['xcare_offer_status']           = '0';
        } 
    
        if(isset($_FILES['offer_image']) && $_FILES['offer_image']['name']!='')
        {
            $xcare_offerDetail = $xcare_offerObj->fromId($_REQUEST['xcare_offer_id']);
            if($xcare_offerDetail->xcare_offer_image != '')
            {
                
                $data = json_decode($xcare_offerDetail->xcare_offer_image,1);
                $size = w::imageSizeArray("xcare_offer");

                if(count($size)>0)
                {
                    foreach ($size as $key => $value) {
                        $width = $value['width'];
                        $height = $value['height'];
                        if($width=='max'){
                            $byx = "-original";
                        }else{
                            $byx = "-".$width."x".$height;
                        }
                        if(file_exists(DIR_UPLOAD_OFFER.$data['name'].$byx.".".$data['type'])){
                            unlink(DIR_UPLOAD_OFFER.$data['name'].$byx.".".$data['type']);
                        }
                    }       
                }
            }
            
        
            $temp = $_FILES['offer_image']['tmp_name'];
            $mime = getimagesize($temp);
            
            if(in_array($mime['mime'], $ext))
            {
                
                $image=$_FILES['offer_image']['name'];
                $filename = w::getImageName('xcare_offer');
                $extension=explode("/", $_FILES['offer_image']['type'])[1];
                $uploadPath  = DIR_UPLOAD_OFFER.$filename;
                $differentSize = w::imageSizeArray('xcare_offer');
                w::imageCropAndUpload($temp,$uploadPath,$differentSize,$mime);
                $photoImage = json_encode(array("name"=>$filename,'type'=>$extension));
                $offerData['xcare_offer_image'] = $photoImage;
            }

        }
        
    if (isset($_POST['xcare_offer_id'])){
		$id=$_POST['xcare_offer_id'];
        $offerData['xcare_offer_updated_at'] = date("Y-m-d H:i:s");

        $xcare_offerDts = $xcare_offerObj->fromId($_POST['xcare_offer_id']);
        
        if($xcare_offerDts->xcare_offer_title != $_POST['offer_title'])
        {
            $offer_detail .= " <b>offer title</b>: "." ".$xcare_offerDts->xcare_offer_title." <b>To</b> ".$_POST['offer_title']." and ";
            
            $chages_data = true;
        }

        // if($xcare_offerDts->xcare_offer_link != $_POST['offer_link'])
        // {
        //     $offer_detail .= " <b>offer link</b>: "." ".$xcare_offerDts->xcare_offer_link." <b>To</b> ".$_POST['offer_link']." and ";
            
        //     $chages_data = true;
        // }
        if(!empty($offerData['xcare_offer_image']))
        {
            $xcare_offerimage = json_decode($xcare_offerDts->xcare_offer_image,1);
            if($xcare_offerimage['name'] != $filename)
            {
                $offer_detail .= " <b>offer Image</b>: "." ".$xcare_offerDts->xcare_offer_image." <b>To</b> ".$photoImage." and ";
            
                $chages_data = true;
            }
        }
        if($xcare_offerDts->xcare_offer_status != $_POST['offer_link'])
        {
            if($xcare_offerDts->xcare_offer_status == '1')
            {
                $old_status = 'Active';
            }else{
                $old_status = 'Inactive';
            }

            if($offerData['xcare_offer_status'] == '1')
            {
                $new_status = 'Active';
            }else{
                $new_status = 'Inactive';
            }

            $offer_detail .= " <b>offer status</b>: "." ".$old_status." To ".$new_status." and ";
            
            $chages_data = true;
        }
		$rs=$xcare_offerObj->updateRecords($offerData,$id);
		if ($rs){
            
            if($chages_data == true)
            {
                $roffer_detail = substr($offer_detail, 0, -4);
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$roffer_detail);
            }
            header('Location:offerlist.php');
		    exit;
		}
	}else{
        
        $sub_offer = "<b>Add Record</b> ";
        $sub_offer.= " <b>Offer Data</b>: ".json_encode($offerData);

        $rs = $xcare_offerObj->addRecords($offerData);
        if($rs){
            
            $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$sub_offer);

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

$domainObj = new domain();
$allDomain =$domainObj->getAllRecords(array());

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

?>