<template>
   <div>
     <vue-table-dynamic :params="params" :key="redraw"></vue-table-dynamic>
   </div>
</template>

<script>
export default {
  props: ['fromdate','todate','type','range','host'],
  name: 'Stats',
  data() {
    return {
      params: {
        data: [
          ['', 'Min', 'Max', 'Avg', 'Posledná'],
        ],
        header: 'row',
        border: true,
        stripe: true,
        height: 0,
      },
      redraw: 0,
    }
  },
  mounted: function () {
      console.log("Mounted");
      console.log(this.data);
      console.log("type=",this.type);
      var that = this;
      moment.locale('sk_SK');

        axios.get('/stats/get', {
          params: {
            type: this.type,
            startdate: this.fromdate,
            enddate: this.todate,
            range: this.range,
            host: this.host
          }
        })
        .then(function (response) {
            console.log("STATS DATA");
            console.log(that.type);
            that.params.data[0][0] = 'Čas';
            if (that.range) {
              for (var i = 0; i < response.data.length; i++) {
                var row = response.data[i];
                if (response.config.params.range == '1d') 
                  var fmt = 'LL';
                else
                  var fmt = 'LLL';
                 let time = moment(row.time).format(fmt);
                 let mean = null;
                 if (row.mean != null) { 
                   mean = row.mean.toFixed(2);
                    that.params.data.push([time,row.min,row.max,mean,row.last]);
                 }
                 that.params.height = 400;
              } 
            } else {
              that.params.data.push(['Hodnota',response.data[0].min,response.data[0].max,response.data[0].mean.toFixed(2),response.data[0].last]);
            };
            console.log(that.params.data);
            that.redraw += 1; 

        })
        .catch(function (error) {
            console.log(error);
        });

      
  }
}
</script>