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

/*if ($_GET['del'] == 'crm_delete') {
	$del_id = $_GET['mf_id'];
	$del_qry = mysql_query("DELETE cmf, cmfv FROM `crm_my_filter` AS cmf INNER JOIN `crm_my_filter-values` AS cmfv ON cmf.`mf_id` = cmfv.`mf_id` WHERE cmf.`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!";
	}
}*/
?>

<style>
label.checkbox-inline {
    margin-right: 50px;
    font-size: 12px;
}
.col-lg-2 {
    flex: 0 0 20.66667%;
    max-width: 25.66667%;
}
.col {
    flex: 0 0 18.66667%;
    max-width: 14.66667%;
}
.operator{
	/*width:70%;*/
	padding-top: 5px;
}
.operator1{
	/*padding: .0rem 0rem;*/
}

.btn-info{
	color:white;
}
/*.col-md-2 {
    flex: 0 0 9.66667%;
    max-width: 9.66667%;
}*/
</style>
		<link rel="stylesheet" href="../assets/css/zomato.css">
		 
				<div class="row add-row-filter">
					<div class="col-lg-12">
						
						<div class="card">
							<?php
							$edit_id = $_GET['mf_id'];
							$eqry = mysql_query("SELECT `mf_id`, `mf_name`FROM `crm_my_filter` WHERE `mf_id` = '$edit_id'");
							$count = mysql_num_rows($eqry);
							$rslt = mysql_fetch_array($eqry);
							$visit_options = array();
							$eqry1 = mysql_query("SELECT `my_filter`, `my_filter_table_field`, `my_filter_operator`, `my_filter_options` FROM `crm_my_filter-values` WHERE `mf_id` = '$edit_id'");
							while ($rslt1 = mysql_fetch_array($eqry1)) {
								if ($rslt1['my_filter_table_field'] == 'UnitId') {
									$outlet_col = $rslt1['my_filter_table_field'];
									$outlet_opt = $rslt1['my_filter_operator'];
									$outlet_options = $rslt1['my_filter_options'];
									$outlet_exp = explode(',', $outlet_options);
								}
								
								if ($rslt1['my_filter_table_field'] == 'SaleDate') {
									if($rslt1['my_filter_operator']=='=' && $rslt1['my_filter']=='Sale Date Fixed'){
										$visit_myfilter = $rslt1['my_filter'];
										$visit_opt = $rslt1['my_filter_operator'];
										$visit_options[] = str_replace("'", "", $rslt1['my_filter_options']);;
									} elseif($rslt1['my_filter_operator']!='=' && $rslt1['my_filter']=='Sale Date Fixed') {
										$visit_myfilter = $rslt1['my_filter'];
										$visit_opt = $rslt1['my_filter_operator'];
										$visit_options[] = str_replace("'", "", $rslt1['my_filter_options']);
									} else {
										$visit_myfilter = $rslt1['my_filter'];
										$visit_opt = $rslt1['my_filter_operator'];
										$visit_options[] = $rslt1['my_filter_options'];
									}
								}

								if ($rslt1['my_filter_table_field'] == 'TotalAmt') {
									$bill_opt = $rslt1['my_filter_operator'];
									$bill_options = $rslt1['my_filter_options'];
								}

								if ($rslt1['my_filter_table_field'] == 'TotalOrder') {
									$totalvisit_opt = $rslt1['my_filter_operator'];
									$totalvisit_options = $rslt1['my_filter_options'];
								}

								if ($rslt1['my_filter_table_field'] == 'SourceId') {
									$source_col = $rslt1['my_filter_table_field'];
									$source_opt = $rslt1['my_filter_operator'];
									$source_options = $rslt1['my_filter_options'];
									$exp_ordsrc = explode(',', $source_options);
								}

								if ($rslt1['my_filter_table_field'] == 'GroupName') {
									$grp_col = $rslt1['my_filter_table_field'];
									$grp_options = $rslt1['my_filter_options'];
									$exp_grp = explode(',', $grp_options);
								} else if ($rslt1['my_filter_table_field'] == 'ItemName') {
									$item_col = $rslt1['my_filter_table_field'];
									$item_options = $rslt1['my_filter_options'];
									$exp_item = explode(',', $item_options);
								}
							}

							$visit_options = implode('#', $visit_options);

							if ($_GET['edit'] == 'crm_edit' && $rslt['mf_id'] == $edit_id) {
							   
								?>
								<div class="card-header admin-feed">
									UPDATE FILTER
								</div>
								<div class="card-body">
									
									<form class="form-horizontal" action="crm.php" method="post">
										
										<input type="hidden" name="mf_id" value="<?php echo $_GET['mf_id']; ?>">
										<div class="form-group row" >
											<div class="col-lg-2 col-md-2">
												<label for="filter_name"  style="font-weight:bold;">Filter Name :</label>
											</div>
											<div class="col-lg-2 col-md-2">
												<input name="filter_name" class="form-control" id="filter_name" type="text" value="<?php echo $rslt['mf_name']; ?>">
											</div>
										</div>
										
										<div class="form-group row" >
											<div class="col-lg-2 col-md-2">
												<label for="outlet_name" style="font-weight:bold; width: 100%; margin-bottom: 0;">Select Outlet</label>
												<!-- <span class="help-block">Leave blank for all</span> -->
											</div>
											<div class="col-lg-2 col-md-2">
												<select class="form-control operator form-control-filter" id="operator_outlet" name="operator_outlet">
													<option value="0" <?php if($outlet_opt == '0') { echo "selected"; } ?> >All</option>
													<option value="=" <?php if($outlet_opt == '=') { echo "selected"; } ?> >=</option>
												</select>
											</div>
											<div class="col-lg-2 col-md-2">
												<?php
												if ($outlet_col == 'UnitId' && $outlet_opt == '=') {
													?>
													<select id="outlet_name" name="outlet_name[]" class="form-control" size="5" multiple="">
														<?php
														$qry1 = mysql_query("SELECT `UnitId`, `UnitName` FROM `unit_master` WHERE `IsActive` = 1");
														while($row1 = mysql_fetch_array($qry1)) {
															if(in_array($row1["UnitId"], $outlet_exp)) {
																$str_otlt = "selected";
															} else {
																$str_otlt="";
															}
															?>
															<option value="<?php echo $row1['UnitId']; ?>" <?php echo $str_otlt; ?>><?php echo $row1['UnitName']; ?></option>
															<?php
														}
														?>
													</select>
													<span class="help-block help-block-outlet">Press Ctrl to select multiple outlets</span>
													<?php
												}
												?>
												<select id="outlet_name" name="outlet_name[]" class="form-control" size="5" multiple="" style="display: none;">
													<?php
													$qry2 = mysql_query("SELECT `UnitId`, `UnitName` FROM `unit_master` WHERE `IsActive` = 1");
													while($row2 = mysql_fetch_array($qry2)){
														?>
														<option value="<?php echo $row2['UnitId']; ?>"><?php echo $row2['UnitName']; ?></option>
														<?php
													}
													?>
												</select>
												<span class="help-block help-block-outlet" style="display: none;">Press Ctrl to select multiple outlets</span>
											</div>
										</div>

										<div class="form-group row" >
											<div class="col-lg-2 col-md-2">
												<label for="" style="font-weight:bold;" >Sale Date</label>
											</div>
											<div class="col-lg-2 col-md-2">
												<?php
												if ($visit_myfilter == 'Sale Date Dynamic' && $visit_opt != '=') {
													$visit_options = explode('#', $visit_options);
												}
												?>
												<select class="form-control operator form-control-filter" id="date-range" name="operator_last_visit">
													<option value="">Select operator</option>
													<option value="=" <?php if($visit_opt == '=') { echo "selected"; } ?> >=</option>
													<option value="between"  <?php if($visit_opt != '=' && $visit_opt != '') { echo "selected"; } ?> >Between</option>
													<option value="today" <?php if($visit_options == 'CURDATE()') { echo "selected"; } ?> >Today</option>
													<option value="yesterday" <?php if($visit_options == 'SUBDATE(CURDATE(), INTERVAL 1 DAY)') { echo "selected"; } ?> >Yesterday</option>
													<option value="last7days" <?php if($visit_options[0] == 'SUBDATE(CURDATE(), INTERVAL 6 DAY)') { echo "selected"; } ?> >Last 7 Days</option>
													<option value="last30days" <?php if($visit_options[0] == 'SUBDATE(CURDATE(), INTERVAL 29 DAYS)') { echo "selected"; } ?> >Last 30 Days</option>
													<option value="thismonth" <?php if($visit_options[0] == 'LAST_DAY( CURDATE( ) - INTERVAL 1 MONTH ) + INTERVAL 1 DAY') { echo "selected"; } ?> >This Month</option>
													<option value="lastmonth" <?php if($visit_options[0] == 'LAST_DAY( CURDATE( ) - INTERVAL 2 MONTH ) + INTERVAL 1 DAY') { echo "selected"; } ?> >Last Month</option>
												</select>
											</div>
											<div class="col-lg-2 col-md-2">
												<?php
												if ($visit_opt == '=' && $visit_myfilter == 'Sale Date Fixed') {
													?>
													<input type="text" name="visit_date" class="form-control single-date" id="single-date" value="<?php echo date('d/m/Y', strtotime($visit_options)); ?>">
													<?php
												} elseif ($visit_opt != '=' && $visit_myfilter == 'Sale Date Fixed') {
													?>
													<?php
													$visit_options = explode('#', $visit_options);
													$visit_options1 = date('d/m/Y', strtotime($visit_options[0]));
													$visit_options2 = date('d/m/Y', strtotime($visit_options[1]));
													$daterange = $visit_options1.' - '.$visit_options2;
													?>
													<input type="text" name="visit_date" class="form-control custom-daterange" id="custom-daterange" value="<?php echo $daterange; ?>">
                                                    <input type="text" name="visit_date" class="form-control custom-daterange" id="custom-daterange2" value="<?php echo $daterange; ?>">
													<?php
												}
												?>
												<input type="text" name="visit_date" class="form-control" id="custom-saledate" style="display: none;">
											</div>
										</div>

										<div class="form-group row" >
											<div class="col-lg-2 col-md-2">
												<label for="" style="font-weight:bold;" >Bill Value</label>
											</div>
											<div class="col-lg-2 col-md-2">
												<select class="form-control operator form-control-filter" name = "operator_spend">
													<option value="" >Select operator</option>
													<option value="=" <?php if($bill_opt == '=') { echo "selected"; } ?> >=</option>
													<option value="<" <?php if($bill_opt == '<') { echo "selected"; } ?> ><</option>
													<option value="<=" <?php if($bill_opt == '<=') { echo "selected"; } ?> ><=</option>
													<option value=">" <?php if($bill_opt == '>') { echo "selected"; } ?> >></option>
													<option value=">=" <?php if($bill_opt == '>=') { echo "selected"; } ?> >>=</option>
												</select>
											</div>
											<div class="col-lg-2 col-md-2">
												<?php
												if($bill_options == '1000' || $bill_options == '1500' || $bill_options == '2000') {
													?>
													<select class="form-control operator" id="idname3" name="avg_spend_value">
														
														<option value="" >Select Average Spend </option>
														<option value="1000" <?php if($bill_options == '1000') { echo "selected"; } ?> >1000</option>
														<option value="1500" <?php if($bill_options == '1500') { echo "selected"; } ?> >1500</option>
														<option value="2000" <?php if($bill_options == '2000') { echo "selected"; } ?> >2000</option>
														<option value="customSpend">Custom Input Value </option>
													</select>
													<?php
												} else {
													?>
													<input type="text" name="avg_spend_value1" class="form-control custom-input3" value="<?php echo $bill_options; ?>">
													<?php
												}
												?>
											</div>
											<button type="button" value="reset" class="btn btn-reset3 btn-info" style="display:none;" >Reset</button>
										</div>

										<div class="form-group row" >
											<div class="col-lg-2 col-md-2">
												<label for="" style="font-weight:bold;" >Visits</label>
											</div>
											<div class="col-lg-2 col-md-2">
												<select class="form-control operator form-control-filter" name = "operator_visits">
													<option value="" >Select operator</option>
													<option value="=" <?php if($totalvisit_opt == '=') { echo "selected"; } ?> >=</option>
													<option value="<" <?php if($totalvisit_opt == '<') { echo "selected"; } ?> ><</option>
													<option value="<=" <?php if($totalvisit_opt == '<=') { echo "selected"; } ?> ><=</option>
													<option value=">" <?php if($totalvisit_opt == '>') { echo "selected"; } ?> >></option>
													<option value=">=" <?php if($totalvisit_opt == '>=') { echo "selected"; } ?> >>=</option>
												</select>
											</div>
											<div class="col-lg-2 col-md-2">
												<input type="text" name="visits_value" class="form-control custom-input3" value="<?php echo $totalvisit_options; ?>">
											</div>
										</div>

										<div class="form-group row" >
											<div class="col-lg-2 col-md-2">
												<label for="" style="font-weight:bold; width: 100%; margin-bottom: 0;">Order Source</label>
												<!-- <span class="help-block">Leave blank for all</span> -->
											</div>
											<div class="col-lg-2 col-md-2">
												<select class="form-control operator form-control-filter" id="operator_ordsrc" name = "operator_ordsrc">
													<!-- <option value="">Select operator</option> -->
													<option value="0" <?php if($source_opt == '0') { echo "selected"; } ?> >All</option>
													<option value="=" <?php if($source_opt == '=') { echo "selected"; } ?> >=</option>
												</select>
											</div>
											<div class="col-lg-2 col-md-2">
												<?php
												if ($source_col == 'SourceId' && $source_opt == '=') {
													?>
													<select class="form-control operator1" id="order_source" name="order_source[]"  size="3" multiple="">
														<?php
														$os_qry1 = mysql_query("SELECT `SourceId`, `OrderSource` FROM `ordersource` WHERE `Active` = 1 ORDER BY `OrderSource`");
														while ($os_row1 = mysql_fetch_array($os_qry1)) {
															if (in_array($os_row1['SourceId'], $exp_ordsrc)) {
																$str_src = "selected";
															} else {
																$str_src = "";
															}
															?>
															<option value="<?php echo $os_row1['SourceId']; ?>" <?php echo $str_src; ?>><?php echo $os_row1['OrderSource']; ?></option>
															<?php
														}
														?>
													</select>
													<span class="help-block help-block-ordsrc">Press Ctrl to select multiple order source</span>
													<?php
												}
												?>
												
												<select class="form-control operator1" id="order_source" name="order_source[]"  size="3" multiple="" style="display: none;">
													<?php
													$os_qry2 = mysql_query("SELECT `SourceId`, `OrderSource` FROM `ordersource` WHERE `Active` = 1 ORDER BY `OrderSource`");
													while ($os_row2 = mysql_fetch_array($os_qry2)) {
														?>
														<option value="<?php echo $os_row2['SourceId']; ?>"><?php echo $os_row2['OrderSource']; ?></option>
														<?php
													}
													?>
												</select>
												<span class="help-block help-block-ordsrc" style="display: none;">Press Ctrl to select multiple order source</span>
											</div>
                                             <div class="col-lg-2 col-md-2">
												<?php
												if ($source_col == 'SourceId' && $source_opt == '=') {
													?>
													<select class="form-control operator1" id="order_source_not" name="order_source_not[]"  size="3" multiple="">
														<?php
														$os_qry1 = mysql_query("SELECT `SourceId`, `OrderSource` FROM `ordersource` WHERE `Active` = 1 ORDER BY `OrderSource`");
														while ($os_row1 = mysql_fetch_array($os_qry1)) {
															if (in_array($os_row1['SourceId'], $exp_ordsrc)) {
																$str_src = "selected";
															} else {
																$str_src = "";
															}
															?>
															<option value="<?php echo $os_row1['SourceId']; ?>" <?php echo $str_src; ?>><?php echo $os_row1['OrderSource']; ?></option>
															<?php
														}
														?>
													</select>
													<span class="help-block help-block-ordsrc">Press Ctrl to select multiple order source</span>
													<?php
												}
												?>
												
												<select class="form-control operator1" id="order_source_not" name="order_source_not[]"  size="3" multiple="" style="display: none;">
													<?php
													$os_qry2 = mysql_query("SELECT `SourceId`, `OrderSource` FROM `ordersource` WHERE `Active` = 1 ORDER BY `OrderSource`");
													while ($os_row2 = mysql_fetch_array($os_qry2)) {
														?>
														<option value="<?php echo $os_row2['SourceId']; ?>"><?php echo $os_row2['OrderSource']; ?></option>
														<?php
													}
													?>
												</select>
												<span class="help-block help-block-ordsrc" style="display: none;">Press Ctrl to select multiple order source</span>
											</div>
										</div>

										<div class="form-group row" >
											<div class="col-lg-2 col-md-2">
												<label for="" style="font-weight:bold;" >SKU Type</label>
											</div>
											<div class="col-lg-2 col-md-2">
												<select class="form-control operator form-control-filter" name = "item_group_name" id="item-group">
													<option value="">Select SKU Type</option>
													<option value="GroupName" <?php if($grp_col == 'GroupName') { echo "selected"; } ?>>GroupName</option>
													<option value="ItemName" <?php if($item_col == 'ItemName') { echo "selected"; } ?>>ItemName</option>
												</select>
											</div>
											<div class="col-lg-2 col-md-2">
												<?php
												if($grp_col == 'GroupName' && $grp_options != '') {
													?>
													<select class="form-control operator form-control-filter" name = "groupname[]" id="group-name" size="5" multiple="">
														<option value="">Select Groupname</option>
														<?php
														$gqry1 = mysql_query("SELECT DISTINCT(`GroupName`) FROM `cc_itemmaster` ORDER BY `GroupName`");
														while ($grow1 = mysql_fetch_assoc($gqry1)) {
															if(in_array($grow1["GroupName"], $exp_grp)) {
																$str_grp = "selected";
															} else {
																$str_grp="";
															}
															?>
															<option value="<?php echo $grow1['GroupName']; ?>" <?php echo $str_grp; ?>><?php echo $grow1['GroupName']; ?></option>
															<?php
														}
														?>
													</select>
													<?php
												}
												?>
												<select class="form-control operator form-control-filter" name = "groupname[]" id="group-name" size="5" multiple="" style="display: none;">
													<option value="">Select Groupname</option>
													<?php
													$gqry2 = mysql_query("SELECT DISTINCT(`GroupName`) FROM `cc_itemmaster` ORDER BY `GroupName`");
													while ($grow2 = mysql_fetch_assoc($gqry2)) {
														?>
														<option value="<?php echo $grow2['GroupName']; ?>"><?php echo $grow2['GroupName']; ?></option>
														<?php
													}
													?>
												</select>
												<?php
												
												if($item_col == 'ItemName' && $item_options != '') {
													?>
													<select class="form-control operator form-control-filter" name = "itemname[]" id="item-name" size="5" multiple="">
														<option value="" >Select Itemname</option>
														<?php
														$iqry1 = mysql_query("SELECT DISTINCT(`ItemName`) FROM `cc_itemmaster` ORDER BY `ItemName`");
														while ($irow1 = mysql_fetch_assoc($iqry1)) {
															if(in_array($irow1['ItemName'], $exp_item)) {
																$str_item = "selected";
															} else {
																$str_item="";
															}
															?>
															<option value="<?php echo $irow1['ItemName']; ?>" <?php echo $str_item; ?>><?php echo $irow1['ItemName']; ?></option>
															<?php
														}
														?>
													</select>
													<?php
												}
												?>
												<select class="form-control operator form-control-filter" name = "itemname[]" id="item-name" size="5" multiple="" style="display: none;">
													<option value="" >Select Itemname</option>
													<?php
													$iqry2 = mysql_query("SELECT DISTINCT(`ItemName`) FROM `cc_itemmaster` ORDER BY `ItemName`");
													while ($irow2 = mysql_fetch_assoc($iqry2)) {
														?>
														<option value="<?php echo $irow2['ItemName']; ?>"><?php echo $irow2['ItemName']; ?></option>
														<?php
													}
													?>
												</select>
												<?php
												
												?>
												<span class="help-block help-block-groupitem">Press Ctrl to select multiple values</span>
											</div>
										</div>
										
										<div class="form-group row">
											<div class="col-lg-2">
											</div>
											<div class="col-lg-3 save-setting">
												<button type="submit" name="updateSetting" class="btn btn-success btn-save-setting">Update Filter</button>
											</div>
										</div>
										
									</form>
									
								</div>
								<?php
							} else {
								?>
								<div class="card-header admin-feed">
									SETUP NEW FILTER
								</div>
								<div class="card-body">
									
									<form class="form-horizontal" action="crm.php" method="post">
										
										<?php
										if ( isset($_SESSION['msg']) ) {
											
											?>
											<div class="form-group">
												<div class="alert alert-<?php echo ($_SESSION['errTyp']=="success") ? "success" : $_SESSION['errTyp']; ?> alert-dismissible">
													<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
													<?php
													echo $_SESSION['msg'];
													unset($_SESSION['msg']);
													?>
												</div>
											</div>
											<?php
										}
										?>

										<?php
										if ( isset($_SESSION['update_msg']) ) {
											
											?>
											<div class="form-group">
												<div class="alert alert-<?php echo ($_SESSION['errTyp']=="success") ? "success" : $_SESSION['errTyp']; ?> alert-dismissible">
													<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
													<?php
													echo $_SESSION['update_msg'];
													unset($_SESSION['update_msg']);
													?>
												</div>
											</div>
											<?php
										}

										if ($_SESSION['error']) {
											?>
											<div class="form-group">
												<div class="alert alert-danger alert-dismissible">
													<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
													<center><?php
														echo $_SESSION["error"]["name_err"]."<br>";
														echo $_SESSION["error"]["opt_err"]."<br>";
														echo $_SESSION["error"]["outlet_err"]."<br>";
														echo $_SESSION["error"]["source_err"]."<br>";
														unset($_SESSION["error"]);
													?></center>
												</div>
											</div>
											<?php
										}
										?>

										<div class="form-group row" >
											<div class="col-lg-2 col-md-2">
												<label for="filter_name"  style="font-weight:bold;">Filter Name :</label>
											</div>
											<div class="col-lg-2 col-md-2">
												<input name="filter_name" class="form-control" id="filter_name" type="text" placeholder="Filter name">
											</div>
										</div>
										
										<div class="form-group row" >
											<div class="col-lg-2 col-md-2">
												<label for="outlet_name" style="font-weight:bold; width: 100%; margin-bottom: 0;">Select Outlet</label>
												<!-- <span class="help-block">Leave blank for all</span> -->
											</div>
											<div class="col-lg-2 col-md-2">
												<select class="form-control operator form-control-filter" id="operator_outlet" name="operator_outlet">
													<!-- <option value="">Select operator</option> -->
													<option value="0">All</option>
													<option value="=">=</option>
												</select>
											</div>
											<div class="col-lg-2 col-md-2">
												<select id="outlet_name" name="outlet_name[]" class="form-control" size="5" multiple="" style="display: none;">
													<?php
													$qry = mysql_query("SELECT `UnitId`, `UnitName` FROM `unit_master` WHERE `IsActive` = 1");
													while($row = mysql_fetch_array($qry)){
														?>
														<option value="<?php echo $row['UnitId']; ?>"><?php echo $row['UnitName']; ?></option>
														<?php
													}
													?>
												</select>
												<span class="help-block help-block-outlet" style="display: none;">Press Ctrl to select multiple outlets</span>
											</div>
										</div>

										<div class="form-group row" >
											<div class="col-lg-2 col-md-2">
												<label for="" style="font-weight:bold;" >Sale Date</label>
											</div>
											<div class="col-lg-2 col-md-2">
												<select class="form-control operator form-control-filter" id="iddate_range" name="operator_last_visit">
													<option value="">Select Date Range</option>
													<option value="=">=</option>
													<option value="between">Between</option>
													<option value="today">Today</option>
													<option value="yesterday">Yesterday</option>
													<option value="last7days">Last 7 Days</option>
													<option value="last30days">Last 30 Days</option>
													<option value="thismonth">This Month</option>
													<option value="lastmonth">Last Month</option>
												</select>
											</div>
											<div class="col-lg-2 col-md-2">
												<input type="text" name="visit_date" class="form-control custom-date" id="custom-sale-date" style="display: none;">
                                                                                               
											</div>
                                            <!-- <div class="col-lg-2 col-md-2">
												 <input type="text" name="not_visit_date" class="form-control custom-date" id="custom-sale-date2" style="display: none;">
											</div> -->
										</div>

										<div class="form-group row" >
											<div class="col-lg-2 col-md-2">
												<label for="" style="font-weight:bold;" >Bill Value</label>
											</div>
											<div class="col-lg-2 col-md-2">
												<select class="form-control operator form-control-filter" name = "operator_spend">
													<option value="" >Select operator</option>
													<option value="=">=</option>
													<option value="<"><</option>
													<option value="<="><=</option>
													<option value=">">></option>
													<option value=">=">>=</option>
												</select>
											</div>
											<div class="col-lg-2 col-md-2">
												<input type="text" name="avg_spend_value1" class="form-control custom-input3">
											</div>
										</div>

										<div class="form-group row" >
											<div class="col-lg-2 col-md-2">
												<label for="" style="font-weight:bold;" >Visits</label>
											</div>
											<div class="col-lg-2 col-md-2">
												<select class="form-control operator form-control-filter" name = "operator_visits">
													<option value="" >Select operator</option>
													<option value="=">=</option>
													<option value="<"><</option>
													<option value="<="><=</option>
													<option value=">">></option>
													<option value=">=">>=</option>
												</select>
											</div>
											<div class="col-lg-2 col-md-2">
												<input type="text" name="visits_value" class="form-control custom-input3">
											</div>
										</div>

										<div class="form-group row" >
											<div class="col-lg-2 col-md-2">
												<label for="" style="font-weight:bold; width: 100%; margin-bottom: 0;">Order Source</label>
												<!-- <span class="help-block">Leave blank for all</span> -->
											</div>
											<div class="col-lg-2 col-md-2">
												<select class="form-control operator form-control-filter" id="operator_ordsrc" name = "operator_ordsrc">
													<option value="0">All</option>
													<option value="=">=</option>
												</select>
											</div>
											<div class="col-lg-2 col-md-2">
												<select class="form-control operator1" id="order_source" name="order_source[]"  size="3" multiple="" style="display: none;">
													<?php
													$os_qry = mysql_query("SELECT `SourceId`, `OrderSource` FROM `ordersource` WHERE `Active` = 1 ORDER BY `OrderSource`");
													while ($os_row = mysql_fetch_array($os_qry)) {
														?>
														<option value="<?php echo $os_row['SourceId']; ?>"><?php echo $os_row['OrderSource']; ?></option>
														<?php
													}
													?>
												</select>
												<span class="help-block help-block-ordsrc" style="display: none;">Press Ctrl to select multiple order source</span>
											</div>
											<!-- <div class="col-lg-2 col-md-2">
												<select class="form-control operator1" id="order_source_not" name="order_source_not[]"  size="3" multiple="" style="display: none;">
													<?php
													$os_qry = mysql_query("SELECT `SourceId`, `OrderSource` FROM `ordersource` WHERE `Active` = 1 ORDER BY `OrderSource`");
													while ($os_row = mysql_fetch_array($os_qry)) {
														?>
														<option value="<?php echo $os_row['SourceId']; ?>"><?php echo $os_row['OrderSource']; ?></option>
														<?php
													}
													?>
												</select>
												<span class="help-block help-block-ordsrc" style="display: none;">Press Ctrl to select multiple order source</span>
											</div> -->
										</div>

										<div class="form-group row" >
											<div class="col-lg-2 col-md-2">
												<label for="" style="font-weight:bold;" >SKU Type</label>
											</div>
											<div class="col-lg-2 col-md-2">
												<select class="form-control operator form-control-filter" name = "item_group_name" id="item-group">
													<option value="">Select SKU Type</option>
													<option value="GroupName">GroupName</option>
													<option value="ItemName">ItemName</option>
												</select>
											</div>
											<div class="col-lg-2 col-md-2">
												<select class="form-control operator form-control-filter" name = "groupname[]" id="group-name" size="5" multiple="" style="display: none;">
													<option value="" >Select Groupname</option>
													<?php
													$gqry = mysql_query("SELECT DISTINCT(`GroupName`) FROM `cc_itemmaster` ORDER BY `GroupName`");
													while ($grow = mysql_fetch_assoc($gqry)) {
														?>
														<option value="<?php echo $grow['GroupName']; ?>"><?php echo $grow['GroupName']; ?></option>
														<?php
													}
													?>
												</select>

												<select class="form-control operator form-control-filter" name = "itemname[]" id="item-name" size="5" multiple="" style="display: none;">
													<option value="" >Select Itemname</option>
													<?php
													$iqry = mysql_query("SELECT DISTINCT(`ItemName`) FROM `cc_itemmaster` ORDER BY `ItemName`");
													while ($irow = mysql_fetch_assoc($iqry)) {
														?>
														<option value="<?php echo $irow['ItemName']; ?>"><?php echo $irow['ItemName']; ?></option>
														<?php
													}
													?>
												</select>
												<span class="help-block help-block-groupitem" style="display: none;">Press Ctrl to select multiple values</span>
											</div>
										</div>
										
										<div class="form-group row">
											<div class="col-lg-2">
											</div>
											<div class="col-lg-3 save-setting">
												<button type="submit" name="saveSetting" class="btn btn-success btn-save-setting" id="saveSetting">Save Filter</button>
											</div>
										</div>
										
									</form>
									
								</div>
								<?php
							}
							?>
							
						</div>
						
					</div>
				</div>

				<div class="row view-row-filter">
					<div class="col-lg-12">
						
						<div class="card">
							<div class="card-header admin-feed">
								VIEW FILTER
							</div>
							<div class="card-body">
								
								<?php
								if ( isset($_SESSION['delMSG']) ) {
									?>
									
									<div class="alert alert-<?php echo ($_SESSION['errTyp']=="success") ? "success" : $_SESSION['errTyp']; ?>">
										<?php
										echo $_SESSION['delMSG'];
										unset($_SESSION['delMSG']);
										?>
									</div>
									
									<?php
								}
								?>

								<table class="table table-striped">
									<thead>
									  	<tr>
											<th>Sr. No.</th>
											<th>Filter Name</th>
											<th>Status</th>
											<th>Action</th>
											<!-- <th>SMS</th>
											<th>Export</th> -->
									  	</tr>
									</thead>
									
									<tbody>
										<?php
										$i = 1;
										$qry = mysql_query("select * from  crm_my_filter");
										while($row= mysql_fetch_array($qry)) {
											?>
											<tr class="alert-disabled alert-disabled<?php echo $row['mf_id'];?> input-alert-disable<?php echo $row['mf_id']; ?>" style="text-transform: capitalize; color: <?php if($row['mf_active'] == 1) { echo "#000"; } else { echo "#ccc"; } ?>" >
												<td><?php echo $i;?></td>
												<td><?php echo $row['mf_name'];?></td>
												<td>
													<div class="btn" >
														<input type="hidden" name="" id="crm_id" value="<?php echo $row['mf_id']; ?>">
														<label class="switch switch-3d switch-success">
															<input type="checkbox" class="switch-input switch-input-tab<?php echo $row['mf_id']; ?>" name = "switch-tab<?php echo $row['mf_id']; ?>" <?php if($row['mf_active'] == 1) { echo "checked"; } ?>  onchange="changeCrmVieFilter(<?php echo $row['mf_id']; ?>)">
															<span class="switch-label">
															</span>
															<span class="switch-handle"></span>
														</label>
													</div>
												</td>
												<td>
													<a href = "manage_filter.php?edit=crm_edit&mf_id=<?php echo $row['mf_id']; ?>" class="btn btn-success btn-disable<?php echo $row['mf_id'];?> alert-disabled<?php echo $row['mf_id'];?>">Edit</a>&nbsp;
													
											        
													<button type="button" name="deletecrm" class="btn btn-danger btn-disable<?php echo $row['mf_id'];?> alert-disabled<?php echo $row['mf_id'];?>" onclick="deleteCRM('crm_delete', <?php echo $row['mf_id']; ?>, '<?php echo $row[mf_name];?>')">Delete</button>&nbsp;
													 <a href = "view_sql.php?sql=crm_sql&mf_id=<?php echo $row['mf_id']; ?>" target="_blank" class="btn btn-info">View Sql</a> &nbsp;
													<!-- <a href = "view_filter.php?res=crm_res&mf_id=<?php //echo $row['mf_id']; ?>" class="btn btn-primary">View Results</a> -->
													<button type="button" name="resultscrm" class="btn btn-primary btn-disable<?php echo $row['mf_id'];?> alert-disabled<?php echo $row['mf_id'];?>" onclick="viewCrmResults('crm_res', <?php echo $row['mf_id']; ?>)">Count Data</button>
													<div class="view-sms-filter<?php echo $row['mf_id']; ?>" style="display: none;">
														<div class="total-phone<?php echo $row['mf_id']; ?> sms-data"></div>
														<a href="exl_filter_data.php?mfid=<?php echo $row['mf_id']; ?>"  class="btn btn-primary">Export</a>
													</div>
												</td>
												<!-- <td><div class="total-phone<?php //echo $row['mf_id']; ?> sms-data"></div></td> -->
												<!-- <td><input type="text" name="phone" class="total-phone<?php //echo $row['mf_id']; ?>" value=""></td>
												<td><input type="text" name="email" class="total-email<?php //echo $row['mf_id']; ?>" value=""></td> -->
												<!-- <td><a href="exl_filter_data.php?mfid=<?php //echo $row['mf_id']; ?>"  class="btn btn-primary">Data</a></td> -->
											</tr>
											<?php
											$i++;
										} ?>
									 									  
									</tbody>
								</table>
							</div>
						</div>
						
					</div>
				</div>
	
	


	
	
	
<?php include"../footer.php"; ?>
<!-- Include Required Prerequisites -->
<script type="text/javascript" src="//cdn.jsdelivr.net/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="//cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
 
<!-- Include Date Range Picker -->
<script type="text/javascript" src="//cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.css" />
<script>
$(document).ready(function(){
	/* select multiple outlets on selection of according to '=' and 'all' operator */
	$('#operator_outlet').change(function(){
		var select_outlet = $('select#operator_outlet option:checked').val();
		if(select_outlet == '=') {
			$("#outlet_name").show();
			$(".help-block-outlet").show();
			// $("#all_outlet").hide();
		} else {
			// $("#all_outlet").show();
			$("#outlet_name").hide();
			$(".help-block-outlet").hide();
		}
	});

	/* select multiple order source on selection of according to '=' and 'all' operator */
	$('#operator_ordsrc').change(function(){
		var select_ordsrc = $('select#operator_ordsrc option:checked').val();
		if(select_ordsrc == '=') {
			$("#order_source").show();
                        $("#order_source_not").show();
			$(".help-block-ordsrc").show();
			// $("#all_ordsrc").hide();
		} else {
			// $("#all_ordsrc").show();
			$("#order_source").hide();
                        $("#order_source_not").hide();
			$(".help-block-ordsrc").hide();
		}
	});

	/* select multiple itemname/groupname on change of itemname and groupname from dropdown */
	$('#item-group').change(function(){
		var select_itmgrp = $('select#item-group option:checked').val();
		if(select_itmgrp == 'GroupName') {
			$("#group-name").show();
			$("#item-name").hide();
			$(".help-block-groupitem").show();
		} else if(select_itmgrp == 'ItemName') {
			$("#group-name").hide();
			$("#item-name").show();
			$(".help-block-groupitem").show();
		} else {
			$("#group-name").hide();
			$("#item-name").hide();
			$(".help-block-groupitem").hide();
		}
	});
});

/*$(document).ready(function(){
	$(window).load(function(){
		$(".help-block-groupitem").hide();
	});
});*/

/*on change of date range for sale date*/
$(document).ready(function(){
	$('#iddate_range').change(function(){
		var select = $('select#iddate_range option:checked').val();
		if(select == 'between') {
			$("#custom-sale-date").show();
                        $("#custom-sale-date2").show();
			$('.custom-date').daterangepicker({
				locale: {
					format: 'DD/MM/Y'
				},
				showDropdowns: true
			});
                        $('.custom-date').daterangepicker({
				locale: {
					format: 'DD/MM/Y'
				},
				showDropdowns: true
			});
		} else if(select == '='){
			$('.custom-date').daterangepicker({
				locale: {
					format: 'DD/MM/Y'
				},
				singleDatePicker: true,
				showDropdowns: true	
			});
			$("#custom-sale-date").show();
                        $("#custom-sale-date2").hide();
		} else {
			$("#custom-sale-date").hide();
                        $("#custom-sale-date2").hide();
		}
	});
});

$(document).ready(function(){
	$(window).load(function(){
		var select = $('select#date-range option:checked').val();
		//alert(select);
		if(select == 'between') {
			$(".custom-daterange").show();
			$(".single-date").hide();
			$('#custom-daterange').daterangepicker({
				locale: {
					format: 'DD/MM/Y'
				},
				showDropdowns: true
			});
                          $('#custom-saledate2').daterangepicker({
				locale: {
					format: 'DD/MM/Y'
				},
				showDropdowns: true
			});
		} else if(select == '='){
			$(".single-date").show();
			$(".custom-daterange").hide();
			$('#single-date').daterangepicker({
				locale: {
					format: 'DD/MM/Y'
				},
				singleDatePicker: true,
				showDropdowns: true	
			});
		} else {
			$(".custom-daterange").hide();
			$(".single-date").hide();
		}
	});
});

$(document).ready(function(){
	$('#date-range').change(function(){
		var select = $('select#date-range option:checked').val();
		if(select == 'between') {
			$("#custom-saledate").show();
			$(".single-date").hide();
			$(".custom-daterange").hide();
			$('#custom-saledate').daterangepicker({
				locale: {
					format: 'DD/MM/Y'
				},
				showDropdowns: true
			});
                        $('#custom-saledate2').daterangepicker({
				locale: {
					format: 'DD/MM/Y'
				},
				showDropdowns: true
			});
		} else if(select == '='){
			$("#custom-saledate").show();
			$(".single-date").hide();
			$(".custom-daterange").hide();
			$('#custom-saledate').daterangepicker({
				locale: {
					format: 'DD/MM/Y'
				},
				singleDatePicker: true,
				showDropdowns: true	
			});
		} else {
			$("#custom-saledate").hide();
			$(".single-date").hide();
			$(".custom-daterange").hide();
		}
	});
});
//reset button 
/*$(document).ready(function(){
	$('#idname1').change(function(){
		var select = $('select#idname1 option:checked').val();
		if(select == 'custom1') {
			$("#idname1").hide();
			$(".custom-input").show();
			$(".btn-reset1").show();	
		}
	});
	$('.btn-reset1').click(function(){
		$("#idname1").show();
		$(".custom-input").hide();
		$(".btn-reset1").hide();
	});
});

$(document).ready(function(){
	$('#idname2').change(function(){
		var select = $('select#idname2 option:checked').val();
		if(select == 'custom2') {
			$("#idname2").hide();
			$(".custom-input2").show();
			$(".btn-reset2").show();
		}
	});
	$('.btn-reset2').click(function(){
		$("#idname2").show();
		$(".custom-input2").hide();
		$(".btn-reset2").hide();
	});
});*/

/*$(document).ready(function(){
	$('#idname3').change(function(){
		var select1 = $('select#idname3 option:checked').val();
		if(select1 == 'customSpend') {
			$("#idname3").hide();
			$(".custom-input3").show();
			$(".btn-reset3").show();
		}
		$('.btn-reset3').click(function(){
			$("#idname3").show();
			$(".custom-input3").hide();
			$(".btn-reset3").hide();
		});
	});
});*/
/*$( window ).on( "load", function(){
	var select = $('select#iddate_range option:checked').val();
	if(select == '') {
		$("#custom-sale-date").show();
		$('.custom-date').daterangepicker({
			locale: {
				format: 'DD/MM/Y'
			},
			showDropdowns: true
		});
	} else if(select == 'between') {
		$("#custom-sale-date").show();
		$('.custom-date').daterangepicker({
			locale: {
				format: 'DD/MM/Y'
			},
			showDropdowns: true
		});
	} else if(select == '='){
		$('.custom-date').daterangepicker({
			locale: {
				format: 'DD/MM/Y'
			},
			singleDatePicker: true,
			showDropdowns: true	
		});
		$("#custom-sale-date").show();
	} else {
		$("#custom-sale-date").hide();
	}
});*/

/*$('.custom-date').daterangepicker({
	
	locale: {
		format: 'DD/MM/Y'
	},
	showDropdowns: true
});*/
</script>
<script src="../assets/js/jquery.toast.js"></script>
<script src="../assets/js/jquery-confirm.js"></script>
<script>
	
$( window ).on( "load", function(){
	var crm_id = $("#crm_id").val();


	// alert(crm_id);
    if ($("input[name=switch-tab"+crm_id+"]:checkbox").is(":checked")) {
    	$(".alert-disabled"+crm_id).removeAttr("disabled");
		$(".alert-disabled"+crm_id).removeAttr("style");
		$(".btn-disable"+crm_id).removeAttr("style");
    } else {
    	$(".alert-disabled"+crm_id).attr("disabled","disabled");
		$(".alert-disabled"+crm_id).css("color","#ccc");
		$(".btn-disable"+crm_id).css("cursor","not-allowed");
    }
});

/* js script for enable/disable of status of crm */
function changeCrmVieFilter(mf_id) {
	if ($("input[name=switch-tab"+mf_id+"]:checkbox").is(":checked")) {
		$(".alert-disabled"+mf_id).removeAttr("disabled");
		$(".alert-disabled"+mf_id+", btn-disable"+mf_id+", .input-alert-disable"+mf_id).removeAttr("style");
		$.ajax({
			url: 'crm.php?act=crm_enable&mf_id='+mf_id,
			success : function(data){
				console.log(data);
				// $(".switch-input-tab"+mf_id).hide();
				// $(".btn"+mf_id).css({"background-color":"#de4847", "color": "#fff"});
			}
		});
	}
	if($("input[name=switch-tab"+mf_id+"]:checkbox").is(":not(:checked)")) {
		$.confirm({
		    title: 'Confirm!',
	    	content: 'Are you sure you want disable?',
	    	type: 'green',
	    	closeIcon: true,
	    	closeIconClass: 'fa fa-close',
	    	buttons	: {

				'Yes'	: {
					'btnClass'	: 'blue',
					'action': function(){
						$(".alert-disabled"+mf_id).attr("disabled","disabled");
						$(".alert-disabled"+mf_id).css("color","#ccc");
						$(".input-alert-disable"+mf_id).css("background-color","");
						$(".btn-disable"+mf_id).css("cursor","not-allowed");
						
						$.ajax({
							url: 'crm.php?act=crm_disable&mf_id='+mf_id,
							success : function(data){
								console.log(data);
								// $(".switch-input-tab"+mf_id).hide();
								// $(".btn"+mf_id).css({"background-color":"#de4847", "color": "#fff"});
							}
						});
					}
				},
				'No'	: {
					'btnClass'	: 'gray',
					'action': function(){
						$(".switch-input-tab"+mf_id).prop("checked",true);
					}	// Nothing to do in this case. You can as well omit the action property.
				}
			}
		});

	}
}

/* js script for count of data on click of 'count data' button */
function viewCrmResults(res, mfid) {
	$.ajax({
		url: 'crm_data.php?act='+res+'&mfid='+mfid,
		success : function(data) {
			console.log(data);
			var data2 = JSON.parse(data);
			var phone = data2.phone;
			//var email = data2.email;
			$('.view-sms-filter'+mfid).show();
			$('.view-sms-filter'+mfid).css("display","-webkit-inline-box");
			$('.total-phone'+mfid).html('SMS - '+phone);
			// $('.total-email'+mfid).val(email);
		}
	});
}

/* js script for deleting of crm */
function deleteCRM(crmd, mfid, name) {
	// alert(mfid);
	$.confirm({
	    title: 'Confirm!',
    	content: 'Are you sure you want to delete ' +name +'?',
    	type: 'green',
    	closeIcon: true,
    	closeIconClass: 'fa fa-close',
    	buttons	: {
			'Yes'	: {
				'btnClass'	: 'blue',
				'action': function(){
					$.ajax({
						url: 'crm_data.php?act='+crmd+'&mfid='+mfid,
						success : function(data){
							console.log(data);
							$('.input-alert-disable'+mfid).remove();
							$.toast({
                                text: "CRM filter deleted", // Text that is to be shown in the toast
                                heading: 'CRM Filter', // Optional heading to be shown on the toast
                                icon: 'success', // Type of toast icon
                                showHideTransition: 'fade', // fade, slide or plain
                                allowToastClose: true, // Boolean value true or false
                                hideAfter: 3000, // false to make it sticky or number representing the miliseconds as time after which toast needs to be hidden
                                stack: 5, // false if there should be only one toast at a time or a number representing the maximum number of toasts to be shown at a time
                                position: 'top-right', // bottom-left or bottom-right or bottom-center or top-left or top-right or top-center or mid-center or an object representing the left, right, top, bottom values
                                
                                
                                
                                textAlign: 'left',  // Text alignment i.e. left, right or center
                                loader: true,  // Whether to show loader or not. True by default
                                loaderBg: '#9EC600',  // Background color of the toast loader
                                beforeShow: function () {}, // will be triggered before the toast is shown
                                afterShown: function () {}, // will be triggered after the toat has been shown
                                beforeHide: function () {}, // will be triggered before the toast gets hidden
                                afterHidden: function () {}  // will be triggered after the toast has been hidden
                            });							
						}
					});
				}
			},
			'No'	: {
				'btnClass'	: 'gray',
				'action': function(){}	// Nothing to do in this case. You can as well omit the action property.
			}
		}
	});
}
</script>
