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



$giftcardObj =  new  giftcard();

$adminlogObj = new admin_log();
$modulepagesObj = new module_pages();

$filename=basename($_SERVER['SCRIPT_FILENAME']);
if($filename == 'giftcardadd.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['giftcard_id'])){
    $giftcardDetail = $giftcardObj->fromId($_REQUEST['giftcard_id']);
}
if(isset($_REQUEST['act']) && $_REQUEST['act']=='validate_code'){
    
        $select=array('giftcard_code');
        $where =array('giftcard_code='=>$_REQUEST['code']);

        if(isset($_REQUEST['giftcard_id']) && $_REQUEST['giftcard_id']!=""){
            $where['giftcard_id!='] =$_REQUEST['giftcard_id'];
        }
        $data =array('fields'=>$select,'where'=>$where);        

        if(isset($_REQUEST['giftcard_id']) && $giftcardObj->fromFields($data)){
            echo "Gift card already exist";
            exit;
        }else if(!isset($_REQUEST['giftcard_id']) && $giftcardObj->fromFields($data)){
            echo "Gift card already exist";
            exit;
        }
    return true;
}

if(isset($_POST['submit'])){
       // Add Client information
    	$giftcardData=array(
          'giftcard_title'=>trim($_POST['giftcard_title']),
          'giftcard_code'=>trim($_POST['giftcard_code']),
          'giftcard_cost'=>trim($_POST['giftcard_cost'])
         );
        $card = array( 
            "tangoCardId" =>$_POST['giftcard_tangoCardId'] 
        );
        // $giftcardData['giftcard_status'] = json_encode($card);

        if(isset($_POST['giftcard_status'])){
            $giftcardData['giftcard_status']           = '1';
        }else{
            $giftcardData['giftcard_status']           = '0';
        } 

        if(isset($_FILES['giftcard_image']) && $_FILES['giftcard_image']['name']!=''){
            $image=$_FILES['giftcard_image']['name'];
            $image=time().'_'.str_replace(" ", "-", $image);
            move_uploaded_file($_FILES['giftcard_image']['tmp_name'], DIR_UPLOAD_GIFT.$image);
            $giftcardData['giftcard_image'] = $image;
        }

        $giftcardDetail = $giftcardObj->fromId($_REQUEST['giftcard_id']);
        if(!empty($giftcardDetail))
        {
            $giftdata = "<b>Edit Record</b> ";
            if($giftcardDetail->giftcard_title != trim($_POST['giftcard_title']))
            {   

                $giftdata .= " <b>Title</b>: "." ".$giftcardDetail->giftcard_title." <b>To</b> ".$_POST['giftcard_title']." and ";
            
                $chages_data = true;
            }

            if($giftcardDetail->giftcard_code != trim($_POST['giftcard_code']))
            {   

                $giftdata .= " <b>Code</b>: "." ".$giftcardDetail->giftcard_code." <b>To</b> ".$_POST['giftcard_code']." and ";
            
                $chages_data = true;
            }

            if($giftcardDetail->giftcard_cost != trim($_POST['giftcard_cost']))
            {   

                $giftdata .= " <b>Cost</b>: "." ".$giftcardDetail->giftcard_cost." <b>To</b> ".$_POST['giftcard_cost']." and ";
            
                $chages_data = true;
            }
            
            if(!empty($image))
            {
                if($giftcardDetail->giftcard_image != trim($image))
                {   

                    $giftdata .= " <b>Image</b>: "." ".$giftcardDetail->giftcard_image." <b>To</b> ".$image." and ";
                
                    $chages_data = true;
                }    
            }

            if($giftcardDetail->giftcard_status != $giftcardData['giftcard_status'])
            {
                
                if($giftcardData['giftcard_status'] == '1')
                {
                    $new_status = 'Active';
                }else{
                    $new_status = 'Inactive';
                }

                if($giftcardDetail->giftcard_status == '1')
                {
                    $old_status = 'Active';
                }else{
                    $old_status = 'Inactive';
                }

                $giftdata .= " <b>Status</b>: "." ".$old_status." To ".$new_status." and ";
            
                $chages_data = true;
            } 
            
        }

    if (isset($_POST['giftcard_id'])){
		$id=$_POST['giftcard_id'];
        $giftcardData['giftcard_updated_at'] = date("Y-m-d H:i:s");
		$rs=$giftcardObj->updateRecords($giftcardData,$id);
		if ($rs){

            if($chages_data == true)
            { 
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,rtrim($giftdata," and"));
            }
            $successMsg = base64_encode(RECORD_UPDATE);			
            //header('Location:giftcardadd.php?act=edit&giftcard_id='.$_POST['giftcard_id'].'&successMsg='.$successMsg);

            header('Location:giftcardlist.php');
		    exit;
		}
	}else{
        $giftcardData['giftcard_created_at'] = date("Y-m-d H:i:s");

        $giftdata = "<b>Add Record</b> ";
        $giftdata.= " <b>Gift Data</b>: ".json_encode($giftcardData);

        $rs = $giftcardObj->addRecords($giftcardData);
        if($rs){
            $successMsg = base64_encode(RECORD_ADD);

            $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$giftdata);

            //header('Location:giftcardadd.php?act=edit&giftcard_id='.$rs.'&successMsg='.$successMsg);
            header('Location:giftcardlist.php');
            exit;
        }
    }
}

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

?>