<?php

    /**
     * Smart Report Maker
     * Version 9.0.0
     * Author : Webuccino
     * All copyrights are preserved to Webuccino
     * URL : http://mysqlreports.com/
     */
    defined('DIRECTACESS') or die("Error 301: Access denied!");
    require_once "request.php";
    require_once "lib.php";
    require_once 'checkSession.php';

    if (sessionBe4Step4() === false) {
        header("location: $url?id=1");
        exit();
    } elseif (sessionBe4Step5() === false) {
        header("location: $url?id=2");
        exit();
    }

    $_SESSION[Session_wizard_key]['page_key'] = "Sutotals";
    require_once 'activePages.php';

    $subtotal_checked  = (isset($_SESSION[Session_report_Settings_key]["subtotals_enabled"]) && $_SESSION[Session_report_Settings_key]["subtotals_enabled"] == true) ? "checked" : '';
    $groupby           = (isset($_SESSION[Session_report_Settings_key]["group_by"]) && count($_SESSION[Session_report_Settings_key]["group_by"]) != 0) ? $_SESSION[Session_report_Settings_key]["group_by"][0] : "";
    $selected_function = (isset($_SESSION[Session_report_Settings_key]['sub_totals']['function'])) ? $_SESSION[Session_report_Settings_key]['sub_totals']['function'] : null;
    $selected_Columns  = (isset($_SESSION[Session_report_Settings_key]["sub_totals"]['affected_columns'])) ? $_SESSION[Session_report_Settings_key]["sub_totals"]['affected_columns'] : array();

    // $user_name = (isset ( $_SESSION[Session_report_Settings_key]["user"] )) ? $_SESSION[Session_report_Settings_key]["user"] : '';

    // $password = (isset ( $_SESSION[Session_report_Settings_key]["pass"] )) ? $_SESSION[Session_report_Settings_key]["pass"] : '';

    // $database_name = (isset ( $_SESSION[Session_report_Settings_key]["db"] )) ? $_SESSION[Session_report_Settings_key]["db"] : '';

    // $template_value = (isset ( $_SESSION ['template_value'] ) && ! empty ( $_SESSION ['template_value'] )) ? $_SESSION ['template_value'] : 'no';

    // $all_templates = (isset ( $_SESSION ["all_templates"] ) && ! empty ( $_SESSION ["all_templates"] ) && is_array ( $_SESSION ["all_templates"] )) ? $_SESSION ["all_templates"] : array ();
    // $data_source = (isset ( $_SESSION[Session_report_Settings_key]["datasource"] )) ? $_SESSION[Session_report_Settings_key]["datasource"] : 'table';

?>
<div class="container col-xs-12">
  <form id="myForm" name="myForm" action="<?php echo ($_SERVER['PHP_SELF']); ?>" role="form" method="post"
    onsubmit="return false;">

    <div class="row">
      <div class="col-xs-1"></div>
      <div id="error-container" class="col-xs-10">

      </div>
      <div class="col-xs-1"></div>
    </div>








    <div class="row">
      <div class="col-xs-10"></div>
      <div class="col-xs-2"></div>
    </div>
    <br />
    <div class="row">
      <div class="col-xs-1"></div>
      <div class="form-group col-xs-10" style="margin-top: -10px;">
        <input type="checkbox" id="subtotal" name="subtotal" <?php echo $subtotal_checked ?> /> <label
          for="subtotal">Allow Subtotals</label>


      </div>
      <div class="help-container-i col-xs-1">
        <a href="" id="subtotalhelp" onClick="return false;"> <img src="includes/images/help.png" width="15" height="15"
            border="0">
        </a>
      </div>

    </div>

    <div class="row">
      <div class="col-xs-1"></div>
      <div class="form-group col-xs-10">
        <label for="host_name">Group by</label>
        <div class="left-inner-addon">
          <i class="glyphicon glyphicon-list-alt"></i> <input name="Group_by" class="form-control font-size-lg"
            type="text" id="Group_by" placeholder="" value="<?php echo $groupby; ?>" readonly />
        </div>
      </div>

    </div>
    <br />
    <div class="row">
      <div class="col-xs-1"></div>


      <div class="form-group col-xs-10" style="margin-top: -10px;">

        <label for="style_name">Function</label>

        <div class="left-inner-addon">
          <i class="glyphicon glyphicon-plus"></i> <select id="Function" name="Function" class="form-control" <?php

                                                                                                                  if ($subtotal_checked != "checked") {
                                                                                                                      echo "disabled";
                                                                                                                  }

                                                                                                              ?>>

            <option value='sum' <?php

                                    if ($selected_function == "sum") {
                                        echo ' selected';
                                    }

                                ?>>SUM</option>
            <option value='count' <?php

                                      if ($selected_function == "count") {
                                          echo ' selected';
                                      }

                                  ?>>Count</option>
            <option value='average' <?php

                                        if ($selected_function == "average") {
                                            echo ' selected';
                                        }

                                    ?>>Average</option>
            <option value='min' <?php

                                    if ($selected_function == "min") {
                                        echo ' selected';
                                    }

                                ?>>Min</option>
            <option value='max' <?php

                                    if ($selected_function == "sum") {
                                        echo ' selected';
                                    }

                                ?>>Max</option>
          </select>

        </div>
      </div>




    </div>


    <br />

    <div class="row">
      <div class="col-xs-1"></div>




      <div class="form-group col-xs-10" style="margin-top: -10px;">
        <label for="allFields">Affected Columns</label>
        <div class="left-inner-addon">
          <i class="glyphicon glyphicon-pushpin"></i>
          <select size="5" name="AffectedColumns" id="AffectedColumns" class="form-control" style="height: 150px;"
            multiple <?php

                         if ($subtotal_checked != "checked") {
                             echo "disabled";
                         }

                     ?>>

            <?php

                foreach ($_SESSION[Session_report_Settings_key]["cells"] as $val) {

                    if ($val['cellType'] == "image") {

                        continue;

                    }

                    if (in_array($val["column"], $selected_Columns)) {
                        echo "<option value='" . $val["column"] . "' selected >" . $val["column"] . " </option>";
                    } else {
                        echo "<option value='" . $val["column"] . "' >" . $val["column"] . "</option>";
                    }

                }

            ?>
          </select>
        </div>
      </div>


    </div>
    <?php

    if ($_SESSION[Session_wizard_key]["edit_mode"]) {?>
    <div class="alert alert-info">
      <div class="row">
        <div class="col-xs-1" style="margin-right: -17px;margin-top: 5px;">
          <i class="fa fa-info-circle fa-fw"></i>
        </div>
        <div class="col-xs-11">

          <span style="font-size: smaller;">If you make changes to saved options at any point, remember to click
            'Next' on that step to ensure your changes are saved</span>
        </div>
      </div>
    </div>
    <?php }

    ?>

    <div class="row" style="margin-top:10px">
      <div class="col-xs-1"></div>
      <div class="col-xs-4">
        <button name="btn_back" id="btn_back" class="btn btn-sunny btn-block">
          <span class="icon glyphicon glyphicon-backward"></span><span class="separator"></span> Back
        </button>
      </div>
      <div class="col-xs-3"></div>
      <div class="col-xs-4">
        <button name="continue" id="btn_cont" class="btn btn-sunny btn-block">
          <span class="icon glyphicon glyphicon-forward"></span><span class="separator"></span> Next
        </button>
      </div>
    </div>

  </form>
</div>

<script>
$("#btn_back").mousedown(function() {
  backToPage("4");
});


$('input[name=option]').change(function() {

  $('#security').prop('checked', false);
  $('#security').prop('disabled', false);
  $('#members').prop('checked', false);
  $('#members').prop('disabled', false);

  $('#sec_Username').prop('disabled', false);
  $('#sec_pass').prop('disabled', false);
  $('#error-container').empty();

});

$("#btn_cont").mousedown(function() {


  var AffectedColumns = $("#AffectedColumns").val();
  var selected_function = $('#Function').val();
  var Group_by = $('#Group_by').val();
  var subtotal = $("#subtotal").is(':checked') ? "enabled" : "disabled";

  $.ajax({
    url: "services/Sutotals.php",
    type: "post",
    data: "affectedcolumns=" + AffectedColumns + "&selected_function=" + selected_function + "&group_by=" +
      Group_by + "&subtotal=" + subtotal,
    success: function(data) {
      data = data.trim();
      if (data === "success") {
        nextToPage("6");
        SwitchStatusDone();
      } else {
        $("#error-container").append("<div class='alert alert-danger'>*  " + data + "</div>");
        // $("#btn_cont").prop("disabled", true);
      }
    },
  });
});


<?php

if (online_demo_mode === 1): ?>


$("#subtotal").click(function(e) {
  e.preventDefault();
  alertify.error("Available only in full version");
  return;

});
$("form :input").change(function(e) {
  e.preventDefault();
  alertify.error("Available only in full version");
  return;

});

<?php else: ?>
$("#subtotal").click(function() {
  if ($("#subtotal").is(':checked')) {
    $("#Function").prop("disabled", false);
    $("#AffectedColumns").prop("disabled", false);
  } else {
    $("#Function").prop("disabled", true);
    $("#AffectedColumns").prop("disabled", true);
  }
});

<?php endif?>
</script>