<div class='col-xs-12' style="padding-top: 1em; margin-bottom: 1em;">
   <div class="input-daterange input-group" id="datepicker">
       <input data-chartid="dynamicChart" id="fromDate" data-daysofhistory="30"  type="text" class="form-control" name="start">
        <span class="input-group-addon">to</span>
       <input class="form-control" data-chartid="dynamicChart" id="toDate" type="text"  name="end">
        <span class='input-group-btn'>
           <a href="#!" data-chartid="dynamicChart" data-otherchartids="statusTimeline" class="btn chart-update btn-theme">Run</a>
        </span>
    </div>
</div>
<div class='clearfix col-xs-12' style='height: 300px' id="dynamicChart" data-chart="dynamicchart" data-daysofhistory="30"  data-chartlabel="Data" data-ylabel="" data-xlabel="Date" data-units=""  data-channelnames="poc.go_kwh,poc.go_electricity_cost,poc.go_peak_load,poc.go_min_load,poc.go_average_spm,poc.go_production_calculated,poc.go_full_card_production,poc.go_polished_rod_hp,poc.go_lifting_cost,poc.go_fluid_above_pump,poc.go_pump_intake_pressure,poc.go_kwh_regen,poc.go_inflow_rate"></div>

<div class='col-xs-12' style='margin-top: 2em;'>
    <table class="table">
        <thead>
            <th>Date</th>
            <th>% Run</th>
            <th>kWh_Regen</th>
            <th>Electricity Cost</th>
            <th>Peak Load</th>
            <th>Min. Load</th>
            <th>Average SPM</th>
            <th>Production</th>
            <th>Full Card Production</th>
            <th>Polished Rod HP</th>
            <th>Lifting Cost</th>
            <th>Fluid Level</th>
            <th>Pump Intake Pressure</th>
            <th>kWh Regen</th>
            <th>Inflow Rate</th>
        </thead>

        <tbody id="output">
        </tbody>
    </table>
	<!--<%= JSON.stringify(channels['poc.events'].value) %>-->
</div>


<style>
  .dynamic-chart-form {
      background-color: whiteSmoke;
      padding: 1em 0.5em;
      margin-top: 1em;
  }
  </style>

<script>
  /*go_channels = {
      "percent_run":{"meshifyName":"go_percent_run","last_value":"","last_send_time":0,"data_type":"float","change_amount":0},
      "kWh":{"meshifyName":"go_kwh","last_value":"","last_send_time":0,"data_type":"float","change_amount":0},
      "electricity_cost":{"meshifyName":"go_electricity_cost","last_value":"","last_send_time":0,"data_type":"float","change_amount":0},
      "peak_load":{"meshifyName":"go_peak_load","last_value":"","last_send_time":0,"data_type":"float","change_amount":0},
      "min_load":{"meshifyName":"go_min_load","last_value":"","last_send_time":0,"data_type":"float","change_amount":0},
      "average_SPM":{"meshifyName":"go_average_spm","last_value":"","last_send_time":0,"data_type":"float","change_amount":0},
      "production_calculated":{"meshifyName":"go_production_calculated","last_value":"","last_send_time":0,"data_type":"float","change_amount":0},
      "full_card_production":{"meshifyName":"go_full_card_production","last_value":"","last_send_time":0,"data_type":"float","change_amount":0},
      "polished_rod_HP":{"meshifyName":"go_polished_rod_hp","last_value":"","last_send_time":0,"data_type":"float","change_amount":0},
      "lifting_cost":{"meshifyName":"go_lifting_cost","last_value":"","last_send_time":0,"data_type":"float","change_amount":0},
      "fluid_above_pump":{"meshifyName":"go_fluid_above_pump","last_value":"","last_send_time":0,"data_type":"float","change_amount":0},
      "pump_intake_pressure":{"meshifyName":"go_pump_intake_pressure","last_value":"","last_send_time":0,"data_type":"float","change_amount":0},
      "kWh_regen":{"meshifyName":"go_kwh_regen","last_value":"","last_send_time":0,"data_type":"float","change_amount":0},
      "inflow_rate":{"meshifyName":"go_inflow_rate","last_value":"","last_send_time":0,"data_type":"float","change_amount":0},
      }*/



  var nodeID = <%= node.nodeId %>;
  var nodeType = '<%= node.nodetypeName %>';
  var channelIDs = {
      go_percent_run:               <%= channels['poc.go_percent_run'].channelId %>,
      go_kwh:                       <%= channels['poc.go_kwh'].channelId %>,
      go_electricity_cost:          <%= channels['poc.go_electricity_cost'].channelId %>,
      go_peak_load:                 <%= channels['poc.go_peak_load'].channelId %>,
      go_min_load:                  <%= channels['poc.go_min_load'].channelId %>,
      go_average_spm:               <%= channels['poc.go_average_spm'].channelId %>,
      go_production_calculated:     <%= channels['poc.go_production_calculated'].channelId %>,
      go_full_card_production:      <%= channels['poc.go_full_card_production'].channelId %>,
      go_polished_rod_hp:           <%= channels['poc.go_polished_rod_hp'].channelId %>,
      go_lifting_cost:              <%= channels['poc.go_lifting_cost'].channelId %>,
      go_fluid_above_pump:          <%= channels['poc.go_fluid_above_pump'].channelId %>,
      go_pump_intake_pressure:      <%= channels['poc.go_pump_intake_pressure'].channelId %>,
      go_kwh_regen:                 <%= channels['poc.go_kwh_regen'].channelId %>,
      go_inflow_rate:               <%= channels['poc.go_inflow_rate'].channelId %>
  };


  var go_percent_run = <%= channels['poc.go_percent_run'].channelId %>;
  var go_kwh = <%= channels['poc.go_kwh'].channelId %>;
  var go_electricity_cost = <%= channels['poc.go_electricity_cost'].channelId %>;
  var go_peak_load = <%= channels['poc.go_peak_load'].channelId %>;
  var go_min_load = <%= channels['poc.go_min_load'].channelId %>;
  var go_average_spm = <%= channels['poc.go_average_spm'].channelId %>;
  var go_production_calculated = <%= channels['poc.go_production_calculated'].channelId %>;
  var go_full_card_production = <%= channels['poc.go_full_card_production'].channelId %>;
  var go_polished_rod_hp = <%= channels['poc.go_polished_rod_hp'].channelId %>;
  var go_lifting_cost = <%= channels['poc.go_lifting_cost'].channelId %>;
  var go_fluid_above_pump = <%= channels['poc.go_fluid_above_pump'].channelId %>;
  var go_pump_intake_pressure = <%= channels['poc.go_pump_intake_pressure'].channelId %>;
  var go_kwh_regen = <%= channels['poc.go_kwh_regen'].channelId %>;
  var go_inflow_rate = <%= channels['poc.go_inflow_rate'].channelId %>;

  var formatDate = function(str){
      var c1, c2;
      c1 = new Date(str);
      c2 = (c1.getTime() / 1000);
      c2 = Math.floor(c2);
      return c2.toString();
  };

  var updateTable = function(chID){
      var apiData, start, end;
      var $output = $('#output');
      start = $('#datePicker').find('#fromDate');
      dateString = start.val().replace(/-/g, "/");
      start = formatDate(dateString);
      end = $('#datePicker').find('#toDate');
      dateString = end.val().replace(/-/g, "/");
      end = formatDate(dateString);
      apiData = "&nodelist[0][nodeId]=" + nodeID.toString() + "&nodelist[0][channelId]=" + chID.toString();
      apiData += "&start=" + start + "&end=" + end;
      $.ajax({
          url: "http://henrypump.meshify.com/api2/Nodechannels",
          data: apiData,
          dataType: "json",
          type: "GET",
          success: function(data) {
              console.log(channeldata:data);
            /*var events = data.listofstreams[0];
            for(var i = events.stream.length-1; i > 0; i--) {
              var event = events.stream[i];
              var timestamp = event.x;
              var jsonBlock = event.y;
              var ev = JSON.parse(jsonBlock);
              var row  = "<tr>";
              row += "<td>" + ev.id + "</td>";
              row += "<td>" + ev.datetime + "</td>";
              row += "<td>" + ev.type + "</td>";
              row += "<td>" + ev.cond + "</td>";
              row += "<td>" + ev.tag + "</td>";
              row += "<td>" + ev.value + "</td>";
              row += "<td>" + ev.device_name + "</td>";
              row += "<td>" + ev.stroke_number + "</td>";
              row += "</tr>"
              $output.append(row)
              //$output.append(JSON.stringify(jsonBlock));*/
            //}
          }
      });
  }
$(document).ready(function(){
    for (var channel in channelIDs){
        if(channelIDs.hasOwnProperty(channel)){
            updateTable(channelIDs[channel]);
        }
    }
});

</script>
