<?php
if(!isset($_SESSION)) 
{ 
    session_start(); 
}
if(isset($_SESSION['user_name']) && $_SESSION['role'] == 'admin') {

?>
<!-- Header.php. Contains header content -->
<?php include 'template/header.php';?>
<body class="hold-transition skin-blue sidebar-mini">
<?php include 'template/main-header.php';?>
 <!-- Left side column. contains the logo and sidebar -->
<?php include 'template/main-sidebar.php';?>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
        Event Dashboard 
        <button type="button" class="btn btn-warning" data-toggle="modal" data-target="#modal-default">
                Search Filter
              </button>
              
      <button type="button" class="btn btn-default" id="daterange-btn">
        <span>
        <?php 
          if(isset($_POST["startDate"]) && $_POST["startDate"] != '')
          {
            echo $_POST["startDate"] . ' - ' . $_POST["endDate"];
          }
          else{
            echo '<i class=\"fa fa-calendar\"></i> Filter by Date';
          }

        ?>

        </span>
        <i class="fa fa-caret-down"></i>
      </button>
      </h1>
    </section>


<style>
.container_chart_1 {
    position: relative;
    padding-bottom: 40%;
    height: 0;
    overflow: hidden;
}

.container_chart_1 iframe {
    position: absolute;
    top:0;
    left: 0;
    width: 100%;
    height: 100%;
}
</style>

<div class ='container_chart_1'>
<iframe src='render.php' frameborder='0' scrolling = 'no' id = 'iframe_chart_1'>
</iframe>
</div>




    <!-- Main content -->
<section class="content">
  <div class="modal fade" id="modal-default">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">×</span></button>
          <h4 class="modal-title">Search Filter</h4>

        </div>
        <div class="modal-body" id="modal-body">
            <div class="margin">
              <p>Match 
                <select name="filter" id="filter">
                <?php if (!isset($vals[0]['filter'])) $vals[0]['filter'] = '';?>
                <option value="all" <?php if ($vals[0]['filter'] == 'all') { echo ' selected="selected"'; } ?> >All
                </option>
                <option value="any" <?php if ($vals[0]['filter'] == 'any') { echo ' selected="selected"'; } ?> >Any
                </option>
                </select>
              of the following
              </p>
            </div>
            <?php if(empty($vals[0]['searchQuery']))
            {
            ?>
            <div class="margin" id="advanceSearch">
                <select name="criteria" id="criteria">
                  <option value="decoyName">Decoy Name</option>
                  <option value="decoyGroup">Network Location</option>
                  <option value="decoyIP">Decoy IP</option>
                  <option value="attackerIP">Attacker IP</option>
                  <option value="eventType">Event Type</option>
                </select>

                <select name="condition" id="condition">
                  <option value="eq">Is equal to</option>
                  <option value="not_eq">Not equal to</option>
                </select>
                <input type="text"  style="width:200px; height: 30px" name="search_data" placeholder="Enter Here" id="search_data" required="" />
                <i class="fa fa-fw fa-plus" id="add-filter"></i>
            </div>
            <?php 
            }
            ?>
            <?php if(!empty($vals[0]['searchQuery']))
            {
              //print_r($vals[0]['searchQuery']);
              $i = 0;
              foreach ($vals[0]['searchQuery'] as $vals):
            ?>
            <div class="margin" id="advanceSearch">
                <select name="criteria" id="criteria">
                  <option value="decoyName" <?php if ($vals['criteria'] == 'decoyName') { echo ' selected="selected"'; } ?>>Decoy Name
                  </option>
                  <option value="decoyGroup" <?php if ($vals['criteria'] == 'decoyGroup') { echo ' selected="selected"'; } ?> >Network Location
                  </option>
                  <option value="decoyIP" <?php if ($vals['criteria'] == 'decoyIP') { echo ' selected="selected"'; } ?> >Decoy IP
                  </option>
                  <option value="attackerIP" <?php if ($vals['criteria'] == 'attackerIP') { echo ' selected="selected"'; } ?> >Attacker IP
                  </option>
                  <option value="eventType" <?php if ($vals['criteria'] == 'eventType') { echo ' selected="selected"'; } ?> >Event Type
                  </option>
                </select>

                <select name="condition" id="condition">
                  <option value="eq" <?php if ($vals['condition'] == 'eq') { echo ' selected="selected"'; } ?> >Is equal to</option>
                  <option value="not_eq" <?php if ($vals['condition'] == 'not_eq') { echo ' selected="selected"'; } ?> >Not equal to</option>
                </select>
                <input type="text"  style="width:200px; height: 30px" name="search_data" value="<?= dataFilter($vals['search_data'])?>" id="search_data" required="" />
                <?php if($i == 0){ ?>
                  <i class="fa fa-fw fa-plus" id="add-filter"></i>
                <?php }
                else { ?>
                  <i class="fa fa-fw fa-close" id="del-filter" onClick='Remove(this)'></i>
                <?php }?>
            </div>
            <?php
              $i++;
              endforeach;
            }
            ?>
            <form id="formSearch" name="formSearch" action="loggraph.php" method="POST">
                <input type="hidden" name="jsonSearchString" id="jsonSearchString" value="" />
                <input type="hidden" name="deleteFilter" id="deleteFilter" value="" />
                 <input type="hidden" name="startDate" id="startDate" value="" />
                  <input type="hidden" name="endDate" id="endDate" value="" />
            </form>
          
          <script>
            var new_filter =`<div class="margin" id="advanceSearch">
                <select name="criteria" id="criteria">
                  <option value="decoyName">Decoy Name</option>
                  <option value="decoyGroup">Network Location</option>
                  <option value="decoyIP">Decoy IP</option>
                  <option value="attackerIP">Attacker IP</option>
                  <option value="eventType">Event Type</option>
                </select>

                <select name="condition" id="condition">
                  <option value="eq">Is equal to</option>
                  <option value="not_eq">Not equal to</option>
                </select>
                <input type="text" id="search_data" style="width:200px; height: 30px" name="search_data" placeholder="Enter Here" required="" />
                <i class="fa fa-fw fa-close" id="del-filter" onClick='Remove(this)'></i>
            </div>`;//Get inner html
            $(document).ready(function(){
                $('#add-filter').click(function() {
                    $('#modal-body').append(new_filter);
                    return false;
                    });

                jsonObj = [];

                masterSearch = [];

                });

            function Remove(obj){
                $(obj).closest('div').remove();
            }

            function searchSubmit() {

                $('div#advanceSearch').each(function() {
                    
                  
                    var criteria = $('#criteria', this).val();
                    var condition = $('#condition', this).val();
                    var search_data = $('#search_data', this).val();

                    item = {};

                    item ["criteria"] = criteria;
                    item ["condition"] = condition;
                    item ["search_data"] = search_data;
                    
                    jsonObj.push(item);
                });

                 //Get the filter value

                var filter = $('#filter').val();
                

                masterSearch.push({
                    filter: filter,
                    searchQuery: jsonObj
                });
                
                var jsonSearchString = JSON.stringify(masterSearch);

                $('#jsonSearchString').val(jsonSearchString);

                $("#formSearch").submit();

            }

            function deleteFilter()
            {
               var deleteFilter = 'Yes';

                $('#deleteFilter').val(deleteFilter);

                $("#formSearch").submit();

            }
            </script>    
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default pull-left" onclick="deleteFilter()">Remove Filter</button>

          <button type="button" class="btn btn-primary" id="advSearchSubmit" onclick="searchSubmit()">Save changes</button>
        </div>
      </div>
      <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
  </div>
</section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->
<!-- DataTables -->
<script src="UIElements/bower_components/datatables.net/js/jquery.dataTables.min.js"></script>
<script src="UIElements/bower_components/datatables.net-bs/js/dataTables.bootstrap.min.js"></script>
 <!-- DataTables -->
<link rel="stylesheet" href="UIElements/bower_components/datatables.net-bs/css/dataTables.bootstrap.min.css">
<script>
  $(function () {
    $('#example1').DataTable()
    $('#example2').DataTable({
      'paging'      : true,
      'lengthChange': false,
      'searching'   : false,
      'ordering'    : true,
      'info'        : true,
      'autoWidth'   : false
    })
  })
</script>
<script>
  $(function () {
    //Initialize Select2 Elements
    $('.select2').select2()

    //Datemask dd/mm/yyyy
    $('#datemask').inputmask('dd/mm/yyyy', { 'placeholder': 'dd/mm/yyyy' })
    //Datemask2 mm/dd/yyyy
    $('#datemask2').inputmask('mm/dd/yyyy', { 'placeholder': 'mm/dd/yyyy' })
    //Money Euro
    $('[data-mask]').inputmask()

    //Date range picker
    $('#reservation').daterangepicker()
    //Date range picker with time picker
    $('#reservationtime').daterangepicker({ timePicker: true, timePickerIncrement: 30, format: 'MM/DD/YYYY h:mm A' })
    //Date range as a button
    $('#daterange-btn').daterangepicker(
      {
        ranges   : {
          'Today'       : [moment(), moment()],
          'Yesterday'   : [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
          'Last 7 Days' : [moment().subtract(6, 'days'), moment()],
          'Last 30 Days': [moment().subtract(29, 'days'), moment()],
          'This Month'  : [moment().startOf('month'), moment().endOf('month')],
          'Last Month'  : [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
        },
        startDate: moment().subtract(29, 'days'),
        endDate  : moment()
      },
      function (start, end) {
        $('#daterange-btn span').html(start.format('YYYY-MM-DD') + ' to ' + end.format('YYYY-MM-DD'));
        $('#startDate').val(start.format('YYYY-MM-DD'));
        $('#endDate').val(end.format('YYYY-MM-DD'));
        searchSubmit();
      }
    )

    //Date picker
    $('#datepicker').datepicker({
      autoclose: true
    })

    //iCheck for checkbox and radio inputs
    $('input[type="checkbox"].minimal, input[type="radio"].minimal').iCheck({
      checkboxClass: 'icheckbox_minimal-blue',
      radioClass   : 'iradio_minimal-blue'
    })
    //Red color scheme for iCheck
    $('input[type="checkbox"].minimal-red, input[type="radio"].minimal-red').iCheck({
      checkboxClass: 'icheckbox_minimal-red',
      radioClass   : 'iradio_minimal-red'
    })
    //Flat red color scheme for iCheck
    $('input[type="checkbox"].flat-red, input[type="radio"].flat-red').iCheck({
      checkboxClass: 'icheckbox_flat-green',
      radioClass   : 'iradio_flat-green'
    })

    //Colorpicker
    $('.my-colorpicker1').colorpicker()
    //color picker with addon
    $('.my-colorpicker2').colorpicker()

    //Timepicker
    $('.timepicker').timepicker({
      showInputs: false
    })
  })
</script>
<?php include 'template/main-footer.php';?>

</body>
<?php
}
else 
{
  header('location:loginView.php');
}
?>
