<?php 
$form_name = "Application Settings";
$table_name = "custom_app_settings";
$primary_key = "app_stngs_id";

$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : "";
	
include_once("includes/config.php");
include_once("includes/common_functions.php");

$common = new General_Functions();
$css_class = "x-gray";
// $common->SetCssClass($css_class);

$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : "";
$act_type = isset($_REQUEST['act_type']) ? $_REQUEST['act_type'] : "";
$cancel_link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
$action = $_SERVER['SCRIPT_NAME']."?page=".$_REQUEST['page']."&mode=submit";

switch($mode)
{
    case "submit":
        $smtp_ssl = isset($_REQUEST['smtp_ssl']) ? $_REQUEST['smtp_ssl'] : 0;
        $smtp_auth = isset($_REQUEST['smtp_auth']) ? $_REQUEST['smtp_auth'] : 0;
		
		if(isset($_REQUEST['id']) && $_REQUEST['id'] != '')
		{
			$qry = "SELECT * FROM $table_name WHERE $primary_key = '".$_REQUEST['id']."'";
			$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
			$rec = $common->sql->record;
			$cnt = count($rec);
		}
		else
			$cnt = 0;
		
		$cupn_chk = isset($_REQUEST['cupn_chk']) ? $_REQUEST['cupn_chk'] : 'N';
		$incentive_chk = isset($_REQUEST['incentive_chk']) ? $_REQUEST['incentive_chk'] : 'N';
		$fy_chk = isset($_REQUEST['fy_chk']) ? $_REQUEST['fy_chk'] : 'N';
		$reimbsmnt_vchr_chk = isset($_REQUEST['reimbsmnt_vchr_chk']) ? $_REQUEST['reimbsmnt_vchr_chk'] : 'N';
		$location_chk = isset($_REQUEST['location_chk']) ? $_REQUEST['location_chk'] : 'N';
		$cupn_cde_chk = isset($_REQUEST['cupn_cde_chk']) ? $_REQUEST['cupn_cde_chk'] : 'N';
		$no_of_coupon_chk = isset($_REQUEST['no_of_coupon_chk']) ? $_REQUEST['no_of_coupon_chk'] : 'N';
		$cupn_prd_chk = isset($_REQUEST['cupn_prd_chk']) ? $_REQUEST['cupn_prd_chk'] : 'N';
		$incentive_amt_chk = isset($_REQUEST['incentive_amt_chk']) ? $_REQUEST['incentive_amt_chk'] : 'N';
		$incentive_vchr_chk = isset($_REQUEST['incentive_vchr_chk']) ? $_REQUEST['incentive_vchr_chk'] : 'N';
		$bio_ref_chk = isset($_REQUEST['bio_ref_chk']) ? $_REQUEST['bio_ref_chk'] : 'N';
		$sur_cmp_chk = isset($_REQUEST['sur_cmp_chk']) ? $_REQUEST['sur_cmp_chk'] : 'N';
		$lab_req_chk = isset($_REQUEST['lab_req_chk']) ? $_REQUEST['lab_req_chk'] : 'N';
		//added by Vidhya - 30/11/2017
		$var_show_chk = isset($_REQUEST['var_show_chk']) ? $_REQUEST['var_show_chk'] : 'N';
		$var_std_arm = isset($_REQUEST['var_std_arm']) ? $_REQUEST['var_std_arm'] : 'N';
        if($cnt == 0)
        {
            $_query = sprintf("insert into ".$table_name." values(0, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s','%s', 'Y', now(), '%s', 0, 0)",
                mysql_escape_string($_REQUEST['survey_id']),
                mysql_escape_string($cupn_chk),
                mysql_escape_string($incentive_chk),
                mysql_escape_string($_REQUEST['fy']),
                mysql_escape_string($fy_chk),
                mysql_escape_string($_REQUEST['reimbsmnt_vchr']),
                mysql_escape_string($reimbsmnt_vchr_chk),
                mysql_escape_string($_REQUEST['location']),
                mysql_escape_string($location_chk),
                mysql_escape_string($cupn_cde_chk),
                mysql_escape_string($_REQUEST['no_of_coupon']),
                mysql_escape_string($no_of_coupon_chk),
                mysql_escape_string($_REQUEST['cupn_prd']),
                mysql_escape_string($_REQUEST['prd_type']),
                mysql_escape_string($cupn_prd_chk),
                mysql_escape_string($_REQUEST['incentive_amt']),
                mysql_escape_string($incentive_amt_chk),
                mysql_escape_string($_REQUEST['incentive_vchr']),
                mysql_escape_string($incentive_vchr_chk),
                mysql_escape_string($bio_ref_chk),
                mysql_escape_string($sur_cmp_chk),
                mysql_escape_string($lab_req_chk),
            	mysql_escape_string($var_show_chk),
            	mysql_escape_string($var_std_arm),	
                mysql_escape_string(USR)
            );
            $rec = $common->sql->query($_query);
            $last_id = mysql_insert_id();
			$common->UserLog($form_name, $table_name, $last_id, 'Insert');
        }
        else
        {
            $query = "UPDATE ".$table_name." 	SET 	app_survey_id   			= '".mysql_escape_string($_REQUEST['survey_id'])."', 
                                                        app_coupons					= '".mysql_escape_string($cupn_chk)."', 
                                                        app_incentive   			= '".mysql_escape_string($incentive_chk)."', 
                                                        app_fin_yr					= '".mysql_escape_string($_REQUEST['fy'])."', 
                                                        app_fin_yr_fixed      		= '".mysql_escape_string($fy_chk)."', 
                                                        app_reimbsmnt_vchr			= '".mysql_escape_string($_REQUEST['reimbsmnt_vchr'])."', 
                                                        app_reimbsmnt_vchr_fixed	= '".mysql_escape_string($reimbsmnt_vchr_chk)."', 
                                                        app_location				= '".mysql_escape_string($_REQUEST['location'])."', 
                                                        app_location_fixed      	= '".mysql_escape_string($location_chk)."', 
                                                        app_cupn_cde_fixed			= '".mysql_escape_string($cupn_cde_chk)."', 
                                                        app_no_of_coupon      		= '".mysql_escape_string($_REQUEST['no_of_coupon'])."', 
                                                        app_no_of_coupon_fixed		= '".mysql_escape_string($no_of_coupon_chk)."', 
                                                        app_cupn_prd      			= '".mysql_escape_string($_REQUEST['cupn_prd'])."', 
                                                        app_cupn_prd_type			= '".mysql_escape_string($_REQUEST['prd_type'])."', 
                                                        app_cupn_prd_fixed      	= '".mysql_escape_string($cupn_prd_chk)."', 
                                                        app_incentive_amt			= '".mysql_escape_string($_REQUEST['incentive_amt'])."', 
                                                        app_incentive_amt_fixed 	= '".mysql_escape_string($incentive_amt_chk)."', 
                                                        app_incentive_vchr			= '".mysql_escape_string($_REQUEST['incentive_vchr'])."', 
                                                        app_incentive_vchr_fixed	= '".mysql_escape_string($incentive_vchr_chk)."', 
                                                        app_fing_req				= '".mysql_escape_string($bio_ref_chk)."', 
                                                        app_surcmp_req				= '".mysql_escape_string($sur_cmp_chk)."', 
                                                        app_labreq_req				= '".mysql_escape_string($lab_req_chk)."', 
                                                        app_cupn_showvar            = '".mysql_escape_string($var_show_chk)."', 		
                                                        app_std_arm                 = '".mysql_escape_string($var_std_arm)."',
                                                        update_time         		= now(), 
                                                        update_user         		= '".USR."' 
                                                WHERE 	".$primary_key."   			= '".$_REQUEST['id']."'";
            $rec = $common->sql->exeQuery($query);
			$common->UserLog($form_name, $table_name, $_REQUEST['id'], 'Update');
        }
        $_SESSION['msg'] = 'Settings has been saved Successfully';

        $link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
        header("location:".$link);
        exit;
    break;
	
	case "edit":
		$query = "Select * FROM $table_name WHERE $primary_key = '".$_REQUEST['id']."'";
		$common->sql->query($query, SQL_ALL, SQL_ASSOC);
		$rec = $common->sql->record;
                
		$id = $rec[0][$primary_key];
		$survey_id = $rec[0]['app_survey_id'];
		$cupn_chk = $rec[0]['app_coupons'];
		if($cupn_chk == "Y")
			$cupn_chk = "checked";
		else
			$cupn_chk = "";
		$incentive_chk = $rec[0]['app_incentive'];
		if($incentive_chk == "Y")
			$incentive_chk = "checked";
		else
			$incentive_chk = "";
		$fy = $rec[0]['app_fin_yr'];
		$fy_chk = $rec[0]['app_fin_yr_fixed'];
		if($fy_chk == "Y")
			$fy_chk = "checked";
		else
			$fy_chk = "";
		$reimbsmnt_vchr = $rec[0]['app_reimbsmnt_vchr'];
		$reimbsmnt_vchr_chk = $rec[0]['app_reimbsmnt_vchr_fixed'];
		if($reimbsmnt_vchr_chk == "Y")
			$reimbsmnt_vchr_chk = "checked";
		else
			$reimbsmnt_vchr_chk = "";
		$location = $rec[0]['app_location'];
		$location_chk = $rec[0]['app_location_fixed'];
		if($location_chk == "Y")
			$location_chk = "checked";
		else
			$location_chk = "";
		$cupn_cde_chk = $rec[0]['app_cupn_cde_fixed'];
		if($cupn_cde_chk == "Y")
			$cupn_cde_chk = "checked";
		else
			$cupn_cde_chk = "";
		$no_of_coupon = $rec[0]['app_no_of_coupon'];
		$no_of_coupon_chk = $rec[0]['app_no_of_coupon_fixed'];
		if($no_of_coupon_chk == "Y")
			$no_of_coupon_chk = "checked";
		else
			$no_of_coupon_chk = "";
		$cupn_prd = $rec[0]['app_cupn_prd'];
		$prd_type = $rec[0]['app_cupn_prd_type'];
		$cupn_prd_chk = $rec[0]['app_cupn_prd_fixed'];
		if($cupn_prd_chk == "Y")
			$cupn_prd_chk = "checked";
		else
			$cupn_prd_chk = "";
		$incentive_amt = $rec[0]['app_incentive_amt'];
		$incentive_amt_chk = $rec[0]['app_incentive_amt_fixed'];
		if($incentive_amt_chk == "Y")
			$incentive_amt_chk = "checked";
		else
			$incentive_amt_chk = "";
		$incentive_vchr = $rec[0]['app_incentive_vchr'];
		$incentive_vchr_chk = $rec[0]['app_incentive_vchr_fixed'];
		if($incentive_vchr_chk == "Y")
			$incentive_vchr_chk = "checked";
		else
			$incentive_vchr_chk = "";
		$bio_ref_chk = $rec[0]['app_fing_req'];
		if($bio_ref_chk == "Y")
			$bio_ref_chk = "checked";
		else
			$bio_ref_chk = "";
		$sur_cmp_chk = $rec[0]['app_surcmp_req'];
		if($sur_cmp_chk == "Y")
			$sur_cmp_chk = "checked";
		else
			$sur_cmp_chk = "";
		$lab_req_chk = $rec[0]['app_labreq_req'];
		if($lab_req_chk == "Y")
			$lab_req_chk = "checked";
		else
			$lab_req_chk = "";
		//added by vidhya - 30-11-2017
		$var_show_chk = $rec[0]['app_cupn_showvar'];
		if($var_show_chk == "Y")
			$var_show_chk = "checked";
		else
			$var_show_chk = "";
		
		//added by vidhya - 08-12-2017
		$var_std_arm = $rec[0]['app_std_arm'];
		if($var_std_arm == "Y")
			$var_std_arm = "checked";
		else
			$var_std_arm = "";
		include("edit.php");
	break;
	
	case "delete":
		if(isset($_REQUEST[$prefix.'id']))
			$id = $_REQUEST[$prefix.'id'];
		else
			$id = implode("', '", $_REQUEST[$primary_key]);

		$query = "DELETE FROM ".$table_name." WHERE $primary_key IN ('".$id."')";

		$rec = $common->sql->exeQuery($query);

		if($rec)
		{
			$_SESSION['msg'] = 'Selected record has been deleted successfully';
			if(isset($_REQUEST['id']))
				$common->UserLog($form_name, $table_name, $_REQUEST['id'], 'Delete');
			else
				for($i=0; $i<count($_REQUEST[$primary_key]); $i++)
					$common->UserLog($form_name, $table_name, $_REQUEST[$primary_key][$i], 'Delete');
		}

		$query_string = str_replace("&mode=delete","",$_SERVER['QUERY_STRING']);
		$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
		header("location:".$link);
	break;

    default:
        echo '<div class="content_bg">';
            include("add.php");
        echo '</div>';
    break;
}
?>