<?php
session_start();
if(!isset($_SESSION['user'])) {
	header("location: ../index.php");
	exit;
}

include '../dbconnect.php';

if ($_GET['act'] == 'crm_delete') {
   
	$del_id = $_GET['mfid'];
	$del_qry = mysql_query("DELETE FROM `crm_my_filter` WHERE `mf_id` = '$del_id'");
	$del_qry = mysql_query("DELETE FROM `crm_my_filter-values` WHERE `mf_id` = '$del_id'");
	

	/*if ($del_qry) {
		$_SESSION['errTyp'] = "success";
		$_SESSION['delMSG'] = "Record delete successfully.";
	} else {
		$_SESSION['errTyp'] = "danger";
		$_SESSION['delMSG'] = "Something went wrong...plz try agian!";
	}*/
	
}
/* Get SMS Result data */

if ($_GET['act'] == 'crm_res') {
	$res_id = $_GET['mfid'];

	$fqry = mysql_query("SELECT `mf_id`, `mf_name` FROM `crm_my_filter` WHERE `mf_id` = '$res_id'");
	$rslt = mysql_fetch_array($fqry);

	$fqry1 = mysql_query("SELECT `my_filter`, `my_filter_table_field`, `my_filter_operator`, `my_filter_options` FROM `crm_my_filter-values` WHERE `mf_id` = '$res_id'");
	$count = mysql_num_rows($fqry1);

	if ($count > 0) {
		$query = "SELECT count(distinct(goh.`GuestPhone`)) AS phone FROM `guest_order_history` AS goh INNER JOIN `homedeliveryitemdetails` AS hdid ON goh.`OrderId` = hdid.`OrderId` WHERE ";
	$j = 0;
	$i==0;
        $notfixdate = 0;
	while ($frow = mysql_fetch_array($fqry1)) {
		/* Unit ID*/
		if ($frow['my_filter_table_field'] == 'UnitId') {
			$query .= 'goh.'.$frow['my_filter_table_field'].' IN ('.$frow['my_filter_options'].') AND ';
		}

		/* Sale Date*/
		if ($frow['my_filter_table_field'] == 'SaleDate') {
                        if ($frow['my_filter'] == 'Sale Date Fixed') {

				$query .= 'goh.'.$frow['my_filter_table_field']." ".$frow['my_filter_operator']." ".$frow['my_filter_options']." AND ";

			} elseif ($frow['my_filter'] == 'Sale Date Dynamic') {

				$query .= 'goh.'.$frow['my_filter_table_field']." ".$frow['my_filter_operator']." ".$frow['my_filter_options']." AND ";

			}
                        elseif ($frow['my_filter'] == 'Sale Date Fixed Not') {
                                $notfixdate = 1;
                                if($i==0){
                                    $startDate = $frow['my_filter_options'];
                                }else{
                                    $endDate = $frow['my_filter_options'];
                                }
                                $fielName = $frow['my_filter_table_field'];
                                $i++;
			}
//			$query .= 'goh.'.$frow['my_filter_table_field']." ".$frow['my_filter_operator']." ".$frow['my_filter_options']." AND ";
		}

		/* Bill Value*/
		if ($frow['my_filter_table_field'] == 'TotalAmt') {
			$query .= 'goh.'.$frow['my_filter_table_field']." ".$frow['my_filter_operator']." '".$frow['my_filter_options']."' AND ";
		}

		/* Source Of Order*/
		if ($frow['my_filter'] == 'Order Source') {
			$query .= $frow['my_filter_table_field'].'  IN ('.$frow['my_filter_options'].') AND ';
			
			
		}
		if ($frow['my_filter'] == 'Order Source Not') {
			
				$query .= $frow['my_filter_table_field'].' NOT IN ('.$frow['my_filter_options'].') AND ';
			
		}

		/* GroupName/Itemname*/
		if ($frow['my_filter_table_field'] == 'GroupName') {
			$exp_grp = explode(',', $frow['my_filter_options']);
			$query .= '(';
			$ctr = 1;
			foreach ($exp_grp as $value) {
				if ($ctr == 1) {
					$query .= 'hdid.'.$frow['my_filter_table_field'].' = \''.$value.'\' ';
				} else {
					$query .= 'OR hdid.'.$frow['my_filter_table_field'].' = \''.$value.'\' ';
				}
				$ctr+=1;
			}
			$query .= ') AND ';
		} else if ($frow['my_filter_table_field'] == 'ItemName') {
			$exp_itm = explode(',', $frow['my_filter_options']);
			$fltr1 = 1;
			$query .= '(';
			foreach ($exp_itm as $value1) {
				if ($fltr1 == 1) {
					$query .= 'hdid.'.$frow['my_filter_table_field'].' = \''.$value1.'\' ';
				} else {
					$query .= 'OR hdid.'.$frow['my_filter_table_field'].' = \''.$value1.'\' ';
				}
				$fltr1 += 1;
			}
			$query .= ') AND ';
		}

	}
        if($notfixdate == 1){
            $query .= "goh.".$fielName." NOT BETWEEN ".$startDate." AND ".$endDate."  AND ";
        }
	 $query .= 'promo_approval=1 AND ';
	 $query .= '1=1';
	 //echo $query;
	 //exit;
	$con_qry = mysql_query($query);
	$result = mysql_fetch_array($con_qry);
	$phone = $result['phone'];
	}
	else {
		echo "<script>alert('Selected Filter is corrupt. Please delete and recreate this Filter.');</script>";
	}

	/* Get Email Result data 
	 Needs to be updated. Not in sync with SMS Filter query above - 09.07.2018*/
/*
	$fqry2 = mysql_query("SELECT `mf_id`, `mf_name` FROM `crm_my_filter` WHERE `mf_id` = '$res_id'");
	$rslt2 = mysql_fetch_array($fqry2);

	$fqry3 = mysql_query("SELECT `my_filter`, `my_filter_table_field`, `my_filter_operator`, `my_filter_options` FROM `crm_my_filter-values` WHERE `mf_id` = '$res_id'");
	$count1 = mysql_num_rows($fqry3);

	if ($count1 > 0) {
		$query1 = "SELECT count(distinct(gd.`Email`)) AS email FROM `guest_details` AS gd INNER JOIN `guest_order_history` AS goh ON gd.`GuestId` = goh.`GuestId` WHERE ";
	} else {
		$query1 = "SELECT * FROM `guest_order_history`";
	}
	
	while ($frow1 = mysql_fetch_array($fqry3)) {
		// Unit ID
		if ($frow1['my_filter_table_field'] == 'UnitId') {
			$query1 .= ' goh.'.$frow1['my_filter_table_field'].' IN ('.$frow1['my_filter_options'].') AND ';
		}

		// Sale Date
		if ($frow1['my_filter_table_field'] == 'SaleDate') {
			$query1 .= ' goh.'.$frow1['my_filter_table_field']." ".$frow1['my_filter_operator']." ".$frow1['my_filter_options']." AND ";
		}

		// Bill Value
		if ($frow1['my_filter_table_field'] == 'TotalAmt') {
			$query1 .= ' goh.'.$frow1['my_filter_table_field']." ".$frow1['my_filter_operator']." '".$frow1['my_filter_options']."' AND ";
		}

		// Source Of Order
		if ($frow1['my_filter_table_field'] == 'SourceId') {
			$query1 .= ' goh.'.$frow1['my_filter_table_field'].' IN ('.$frow1['my_filter_options'].') AND ';
		}

	}
	$query1 .= '1=1';
	// echo $query1;
	$con_qry1 = mysql_query($query1);
	$result1 = mysql_fetch_array($con_qry1);
	$email = $result1['email'];
	$data = array('phone' => $phone, 'email' => $email);
*/

	$data = array('phone' => $phone);
	echo json_encode($data);
}
	/* Phone data for Campaign Filter 
	 Needs to be merged with SMS Filter query above - 09.07.2018*/


if ($_GET['act'] == 'campfiltervalue') {
	$mf_id = $_GET['mf_id'];

	$fqry = mysql_query("SELECT `mf_id`, `mf_name` FROM `crm_my_filter` WHERE `mf_id` = '$mf_id'");
	$rslt = mysql_fetch_array($fqry);

	$fqry1 = mysql_query("SELECT `my_filter`, `my_filter_table_field`, `my_filter_operator`, `my_filter_options` FROM `crm_my_filter-values` WHERE `mf_id` = '$mf_id'");
	$count = mysql_num_rows($fqry1);

	if ($count > 0) {
		$query = "SELECT count(distinct(`GuestPhone`)) AS phone FROM `guest_order_history` WHERE ";
	} else {
		$query = "SELECT * FROM `guest_order_history`";
	}
	
	while ($frow = mysql_fetch_array($fqry1)) {
		/* Unit ID*/
		if ($frow['my_filter_table_field'] == 'UnitId') {
			$query .= $frow['my_filter_table_field'].' IN ('.$frow['my_filter_options'].') AND ';
		}

		/* Sale Date*/
		if ($frow['my_filter_table_field'] == 'SaleDate') {
			$query .= $frow['my_filter_table_field']." ".$frow['my_filter_operator']." ".$frow['my_filter_options']." AND ";
		}

		/* Bill Value*/
		if ($frow['my_filter_table_field'] == 'TotalAmt') {
			$query .= $frow['my_filter_table_field']." ".$frow['my_filter_operator']." '".$frow['my_filter_options']."' AND ";
		}

		/* Source Of Order*/
		if ($frow['my_filter_table_field'] == 'SourceId') {
			if($j == 0){
				$query .= $frow['my_filter_table_field'].'  IN ('.$frow['my_filter_options'].') AND ';
			}else{
				$query .= $frow['my_filter_table_field'].' NOT IN ('.$frow['my_filter_options'].') AND ';
			}
			$j++;
		}

	}
	$query .= '1=1';
	// echo $query;
	$con_qry = mysql_query($query);
	$result = mysql_fetch_array($con_qry);
	$phone = $result['phone'];

	$data = array('phone' => $phone);
	echo json_encode($data);
}