<?php include('techsyn_header.php'); ?>
<!-- Main Content Container for side bar and body-->
<div class="main-container ace-save-state" id="main-container">
	<script type="text/javascript">
		try {
			ace.settings.loadState('main-container')
		} catch (e) {}
	</script>
	<?php include('techsyn_sidebar.php'); ?>


	<!--breadcrumb-->
	<div class="main-content">
		<div class="main-content-inner">
			<div class="breadcrumbs ace-save-state" id="breadcrumbs">
				<ul class="breadcrumb">

					<li class="#">Data Setup</li>
					<li class="#">Masters</li>
					<li class="active">Rule Generation</li>
				</ul>
			</div>
			<!-- End of breadcrumb -->

			<div class="page-content">
				<div class="box box-primary" style="padding: 10px; margin: 2px 0px 50px 5px; width: 99.5%; ">

					<form name="rulegen" method="post" id="rulegen">
						<!-- box-body-->
						<div class="box-body" id="reload_rule">
							<!-- <div class="row">
								<div class="form-group col-sm-12">
									<label for="firm_code">Select Section Name</label>
									<select name="checkup_section_id" id="checkup_section_id" onchange="selectpar(this.value);" class="form-control">
										<option value="" disabled selected>Select the checkup section</option>
										<?php echo generateOption('checkup_form_section', 'section_name', 'section_id', '', '');
										?>
									</select>
								</div>
							</div> -->
							<div class="row">
								<div class="col-sm-12">
									<table id="myTable" class="table table-bordered order-list">
										<thead>

											<tr>
												<th>Joiner</th>
												<th>open bracket</th>
												<th>checkup parameter</th>
												<th>Condition</th>
												<th>Value</th>
												<th>close bracket</th>
												<th>Add</th>
											</tr>
										</thead>

										<tbody>
											<tr>
												<td>

												</td>
												<td>
													<input type="text" name="op_br0" id="op_br0" style="width:100%" />
												</td>

												<td>
													<select name="checkup_parameter_id0" id="checkup_parameter_id0" class="form-control">
														<option value="" disabled selected>Select Paramter</option>
														<?php
														$query = "select * from checkup_parameter group by key_health_map_name";

														$result = mysqli_query($conn, $query);
														while ($row = mysqli_fetch_assoc($result)) {
															$key_param = getFieldFromTable('key_param_name', 'key_health_reportable_parameter_master', 'key_param_id', $row['key_health_map_name']);
														?>
															<option value="<?= $row['key_health_map_name'] ?>"><?= $key_param ?></option>
														<?
														}
														?>

													</select>
												</td>
												<td><select id="rule_condition_id0" name="rule_condition_id0" class=form-control select2 style="width:100%;">
														<option disabled selected>Select Condition</option> <?php echo generateOption('rule_condition', 'condition_sy', 'rule_condition_id', '', ''); ?>
													</select></td>
												<td><input type="text" name="value0" id="value0" style="width:100%;"></td>
												<td>
													<input type="text" name="cl_br0" id="cl_br0" style="width:100%" />
												</td>
												<td>
													<input type="button" class="btn btn-sm btn-block " id="addrow" value="join Condition" />
												</td>
											</tr>

											<input type="hidden" name="count_items" id="count_items" value="1" />

										</tbody>


										<tfoot>
											<tr>
												<td></td>
												<!-- <td> -->
												<!-- <label for="result_parameter">RESULT PARAMETER</label>
													<select name="result" id="result" class="form-control">
														<option value="" disabled selected>select paramter</option>
													</select> -->
												<!-- </td> -->
												<td style="width:20%"><label>Age start</label><input type="number" name="rule_age_start" id="rule_age_start" style="width: 100%"></td>
												<td style="width: 20%;"><label>Age end</label><input type="number" name="rule_age_end" id="rule_age_end" style="width: 100%"></td>
												<td><label>Gender</label><select id="rule_gender" name="rule_gender" style="width: 100%">
														<option disabled="" selected="" value="">Select Gender </option>
														<option value="M">Male</option>
														<option value="F">Female </option>
													</select></td>
												<td text-align="center"><label for="result">RESULT</label>
													<input type="text" name="result_disp" id="result_disp" style="width:100%;" />
												</td>
												<td>
													<label for="abnormality">Abnormality</label>
													<select name="abnormality" id="abnormality" class="form-control">
														<option value="">Select</option>
														<?php echo generateOption('abnormality', 'abnormality_name', 'abnormality_id', '', '') ?>
													</select>
												</td>

												<td width="20%">
													<label for="result_param">Result Field</label>
													<select name="result" id="result" class="form-control select2">
														<optgroup label="With Result Field Value">
															<option value="">Select</option>
															<?php echo generate_options('select concat(c.parameter_name," SECTION ==> ",s.section_name) as name,c.checkup_parameter_id from checkup_parameter c left join checkup_form_section s on c.checkup_form_section_id=s.section_id where s.section_name != "" and c.input_type IN ("number","text")', $rowl1['result'], 'checkup_parameter_id', 'name', '', '');
															?>
														</optgroup>
														
													</select>
												</td>

											</tr>

											<tr>
												<td></td>
												<td><label for="firm_code">Risk</label>
													<select multiple="" class="select2 form-control" id="risk" name="risk[]" data-placeholder="">
														<?php echo generateOption('health_risk', 'health_risk_name', 'health_risk_id', '', ''); ?>
													</select>
												</td>
												<!-- <td>
													<label for="firm_code">More Risk</label>
													<select multiple="" class="select2 form-control" id="checkup_more_risk" name="checkup_more_risk[]" data-placeholder="">
														<?php echo generateOption('health_risk', 'health_risk_name', 'health_risk_id', '', ''); ?>
													</select>
												</td> -->
												<td>
													<label class="control-label" for="health_advice_name">Advice</label>
													<select multiple="" class="select2 form-control" id="advice" name="advice[]" data-placeholder="">
														<?php echo generateOption('health_advice', 'health_advice_name', 'health_advice_id', '', ''); ?>
													</select>
												</td>
												<!-- <td>
													<label class="control-label" for="health_advice_name">More Advice</label>
													<select multiple="" class="select2 form-control" id="checkup_more_advice" name="checkup_more_advice[]" data-placeholder="">
														<?php echo generateOption('health_advice', 'health_advice_name', 'health_advice_id', '', ''); ?>
													</select>
												</td> -->

												<td>
													<label for="color">Choose Color</label>
													<input type="color" name="color" id="color" class="form-control">
												</td>

												<td>
													<label for="range-type">Range Type</label>
													<select class="form-control" name="range_type" id="range_type">
														<option value="" selected disabled>--select--</option>
														<option value="high">High</option>
														<option value="low">Low</option>
														<option value="lb">Lower Boundary</option>
														<option value="ub">Upper Boundary</option>
													</select>
												</td>

												<td>
													<label for="is_string_rule">Text Comparison Rule?</label>
													<select name="is_string_rule" id="is_string_rule" class="form-control">
														<option value="">--select--</option>
														<option value="0">No</option>
														<option value="1">Yes</option>
													</select>
												</td>
											</tr>
										</tfoot>


									</table>

								</div>

							</div>

						</div>
						<!-- End box-body-->
						<!--box-footer-->
						<?php
						//change on using module
						//$hasWriteAccess = isAccessible($_SESSION['RoleId'], $menu_key, 'W');
						$hasWriteAccess = 1;
						?>
						<?php if ($hasWriteAccess) { ?>
							<div class="box-footer" style="text-align: center;">
								<button class="btn btn-info save_button" type="button" id="save_button" onclick="validate();">
									<i class="ace-icon fa fa-floppy-o bigger-110"></i>
									Save
								</button>

								&nbsp; &nbsp; &nbsp;
								<button class="btn btn-warning" type="reset" onclick="refr();">
									<i class="ace-icon fa fa-undo bigger-110"></i>
									Reset
								</button>
							</div>
						<?php  } ?>

						<!--End box-footer-->
					</form>

				</div>
			</div>
		</div><!-- /.page-content -->
	</div>
</div><!-- /.main-content -->
<style>
	.select2-selection__choice {
		white-space: break-spaces;
	}
</style>

<link rel="stylesheet" href="css/select2.min.css">
<script src="js/select2.full.min.js"></script>
<script type="text/javascript">
	jQuery(function($) {
		$('.select2').select2()
		$('#id-disable-check').on('click', function() {
			var inp = $('#form-input-readonly').get(0);
			if (inp.hasAttribute('disabled')) {
				inp.setAttribute('readonly', 'true');
				inp.removeAttribute('disabled');
				inp.value = "This text field is readonly!";
			} else {
				inp.setAttribute('disabled', 'disabled');
				inp.removeAttribute('readonly');
				inp.value = "This text field is disabled!";
			}
		});
		// if (!ace.vars['touch']) {
		// 	$('.chosen-select').chosen({
		// 		allow_single_deselect: true
		// 	});
		// 	$(window)
		// 		.off('resize.chosen')
		// 		.on('resize.chosen', function() {
		// 			$('.chosen-select').each(function() {
		// 				var $this = $(this);
		// 				$this.next().css({
		// 					'width': $this.parent().width()
		// 				});
		// 			})
		// 		}).trigger('resize.chosen');
		// 	//resize chosen on sidebar collapse/expand
		// 	$(document).on('settings.ace.chosen', function(e, event_name, event_val) {
		// 		if (event_name != 'sidebar_collapsed') return;
		// 		$('.chosen-select').each(function() {
		// 			var $this = $(this);
		// 			$this.next().css({
		// 				'width': $this.parent().width()
		// 			});
		// 		})
		// 	});
		// 	$('#chosen-multiple-style .btn').on('click', function(e) {
		// 		var target = $(this).find('input[type=radio]');
		// 		var which = parseInt(target.val());
		// 		if (which == 2) $('#form-field-select-4').addClass('tag-input-style');
		// 		else $('#form-field-select-4').removeClass('tag-input-style');
		// 	});
		// }
		$('[data-rel=tooltip]').tooltip({
			container: 'body'
		});
		$('[data-rel=popover]').popover({
			container: 'body'
		});
		// autosize($('textarea[class*=autosize]'));
		// $('textarea.limited').inputlimiter({
		// 	remText: '%n character%s remaining...',
		// 	limitText: 'max allowed : %n.'
		// });
		//"jQuery UI Slider"
		//range slider tooltip example
		$("#slider-range").css('height', '200px').slider({
			orientation: "vertical",
			range: true,
			min: 0,
			max: 100,
			values: [17, 67],
			slide: function(event, ui) {
				var val = ui.values[$(ui.handle).index() - 1] + "";
				if (!ui.handle.firstChild) {
					$("<div class='tooltip right in' style='display:none;left:16px;top:-6px;'><div class='tooltip-arrow'></div><div class='tooltip-inner'></div></div>")
						.prependTo(ui.handle);
				}
				$(ui.handle.firstChild).show().children().eq(1).text(val);
			}
		}).find('span.ui-slider-handle').on('blur', function() {
			$(this.firstChild).hide();
		});
		$("#slider-range-max").slider({
			range: "max",
			min: 1,
			max: 10,
			value: 2
		});
		$("#slider-eq > span").css({
			width: '90%',
			'float': 'left',
			margin: '15px'
		}).each(function() {
			// read initial values from markup and remove that
			var value = parseInt($(this).text(), 10);
			$(this).empty().slider({
				value: value,
				range: "min",
				animate: true
			});
		});
		$("#slider-eq > span.ui-slider-purple").slider('disable'); //disable third item
		$('#id-input-file-1 , #id-input-file-2').ace_file_input({
			no_file: 'No File ...',
			btn_choose: 'Choose',
			btn_change: 'Change',
			droppable: false,
			onchange: null,
			thumbnail: false //| true | large
			//whitelist:'gif|png|jpg|jpeg'
			//blacklist:'exe|php'
			//onchange:''
			//
		});
		//pre-show a file name, for example a previously selected file
		//$('#id-input-file-1').ace_file_input('show_file_list', ['myfile.txt'])
		//datepicker plugin
		//link
		$('.date-picker').datepicker({
				autoclose: true,
				todayHighlight: true
			})
			//show datepicker when clicking on the icon
			.next().on(ace.click_event, function() {
				$(this).prev().focus();
			});
		//chosen plugin inside a modal will have a zero width because the select element is originally hidden
		//and its width cannot be determined.
		//so we set the width after modal is show
		$('#modal-form').on('shown.bs.modal', function() {
			if (!ace.vars['touch']) {
				$(this).find('.chosen-container').each(function() {
					$(this).find('a:first-child').css('width', '210px');
					$(this).find('.chosen-drop').css('width', '210px');
					$(this).find('.chosen-search input').css('width', '200px');
				});
			}
		})
		/**
		//or you can activate the chosen plugin after modal is shown
		//this way select element becomes visible with dimensions and chosen works as expected
		$('#modal-form').on('shown', function () {
			$(this).find('.modal-chosen').chosen();
		})
		*/
		$(document).one('ajaxloadstart.page', function(e) {
			autosize.destroy('textarea[class*=autosize]')
			$('.limiterBox,.autosizejs').remove();
			$('.daterangepicker.dropdown-menu,.colorpicker.dropdown-menu,.bootstrap-datetimepicker-widget.dropdown-menu').remove();
		});
	});
</script>

<script type="text/javascript">
	var itemOptions1 = "<?php echo generateOption('rule_joiner', 'joiner_sy', 'rule_joiner_id', '', ''); ?>";
	var itemOptions2 = "<?php echo generateOption('rule_condition', 'condition_sy', 'rule_condition_id', '', ''); ?>";

	$(document).ready(function() {
		var counter = $("#count_items").val();

		$("#addrow").on("click", function() {
			// var checkup_section_id = $('#checkup_section_id').val();
			// if (checkup_section_id == null || checkup_section_id == "") {
			// BootstrapDialog.alert("Please select checkup section");
			// } else {
			selectpar1(counter);
			var itemOptions = "";
			var newRow = $("<tr>");
			var cols = "";
			cols += "<td><select ";
			cols += "id=\"rule_joiner_id" + counter + "\"name=\"rule_joiner_id" + counter + "\" class=\"form-control select2\" style=\"width:100%;\"";
			cols += ">";
			cols += "<option disabled selected>select the joiner</option>";
			cols += itemOptions1;
			cols += "</select></td>";
			cols += '<td><input type="text" name="op_br' + counter + '" id="op_br' + counter + '" style="width:100%"/></td>';
			cols += "<td><select ";
			cols += "id=\"checkup_parameter_id" + counter + "\"name=\"checkup_parameter_id" + counter + "\" class=\"form-control select2\" style=\"width:100%;\"";
			cols += ">";
			cols += itemOptions;
			cols += "</select></td>";
			cols += "<td><select ";
			cols += "id=\"rule_condition_id" + counter + "\"name=\"rule_condition_id" + counter + "\" class=\"form-control select2\" style=\"width:100%;\"";
			cols += ">";
			cols += "<option disabled selected>select the condition</option>";
			cols += itemOptions2;
			cols += "</select></td>";
			cols += '<td><input type="text" name="value' + counter + '" id="value' + counter + '" style="width:100%;"></td>';
			cols += '<td><input type="text" name="cl_br' + counter + '" id="cl_br' + counter + '" style="width:100%" /></td>';
			cols += '<td align="center"><a href="#" class="btn-lg" data-toggle="tooltip" id="deletebtn" title="Delete"><span class="glyphicon glyphicon-trash"></span></a></td>';
			newRow.append(cols);
			$("#myTable").append(newRow);
			counter++;
			$("#count_items").val(counter);
			// }

		});
		$("#myTable").on("click", "#deletebtn", function(event) {
			$(this).closest("tr").remove();
			counter -= 1
			$("#count_items").val(counter);
		});

	});
</script>
<script>
	// function selectpar(checkup_section_id) {
	// 	$("#checkup_section_id").val();
	// 	$.ajax({
	// 		type: "POST",
	// 		url: "get_checkup_parameter.php",
	// 		data: {
	// 			'checkup_section_id': checkup_section_id
	// 		},
	// 		success: function(data) {
	// 			$("#checkup_parameter_id0").html(data);
	// 			$("#result").html(data);


	// 		}
	// 	});
	// }

	function selectpar1(sid) {
		$.ajax({
			type: "POST",
			url: "get_key_checkup_param_name.php",
			data: {
				sid: sid
			},
			success: function(data) {
				// alert(data);
				$("#checkup_parameter_id" + sid).html(data);

			}
		});
	}
</script>
<script>
	function validate() {
		
		let stringRule = $("#is_string_rule").val();
		if(stringRule == '' || stringRule == undefined){
			BootstrapDialog.alert("Text Comparison Rule? field is mandatory");
			return false;
		}

		$.ajax({
			url: 'save_rule_gen.php',
			type: "POST",
			data: $("#rulegen").serialize(),
			success: function(data) {
				BootstrapDialog.alert('Rule created successfully.');
				$("checkup_section_id").val("");
				$("checkup_parameter_id").val("");
				$("rule_condition_id").val("");
				$("rule_joiner_id").val("");
				window.location = 'rule_generation.php';
			},
			error: function(data) {
				BootstrapDialog.alert('Error Saving Rule Details');
				return;
			}

		});
	}

	function refr() {
		('#')
	}
</script>