<?php
    $xcrud23 = Xcrud::get_instance();  
    $xcrud23->table('ktsgkbank');
    $xcrud23->columns('datum,maincategorie,subcategorie,cost,special_cost,comment,simple_upload');
    $xcrud23->order_by('datum','desc');
    $xcrud23->duplicate_button();
    $xcrud23->table_name('Költségek 2023', '','icon-leaf');
    $xcrud23->benchmark(true);
    $xcrud23->where('datum >', '2022-12-31')->where('datum <=', '2023-12-31');
    $xcrud23->limit(100);
    $xcrud23->label(array(
        'maincategorie' => 'Főkategória', 
        'subcategorie' => 'Alkategória',
        'datum' => 'Dátum',
        'created' => 'Rögzítve',
        'cost' => 'Költség',
        'comment' => 'Megjegyzés',
        'simple_upload'=> 'kép',
    ));
     
    
    $xcrud23->validation_required('maincategorie');
    $xcrud23->highlight_row('special_cost', '>', 0, '#9ADAFF');
    
    $xcrud23->highlight('priceEach', '>', -300, '#9ADAFF');
    $xcrud23->pass_default('datum', date('Y-m-d'));
    $xcrud23->pass_default('created', date('Y-m-d H:i:s'));
    $xcrud23->before_list('chart_data', 'functions.php');
    $xcrud23->field_callback('maincategorie','cat_input');
    $xcrud23->field_callback('subcategorie','cat_input');
    $xcrud23->change_type('cost', 'text', '', array('maxlength'=>100));
    $xcrud23->before_insert('math_input', 'functions.php');
    $xcrud23->before_update('math_input', 'functions.php'); 
    $xcrud23->change_type('simple_upload', 'file', '', array('not_rename'=>true));
    $xcrud23->sum('cost', 'align-center', 'Total {value}');
    $xcrud23->create_action('my_action','my_function'); 
    
    
    $xcrud22 = Xcrud::get_instance();  
    $xcrud22->table('ktsgkbank');
    $xcrud22->columns('datum,maincategorie,subcategorie,cost,special_cost,comment,simple_upload');
    $xcrud22->order_by('datum','desc');
    $xcrud22->duplicate_button();
    $xcrud22->table_name('Költségek 2022', '','icon-leaf');
    $xcrud22->benchmark(true);
    $xcrud22->where('datum >', '2021-12-31')->where('datum <=', '2022-12-31');
    $xcrud22->limit(100);
    $xcrud22->label(array(
        'maincategorie' => 'Főkategória', 
        'subcategorie' => 'Alkategória',
        'datum' => 'Dátum',
        'created' => 'Rögzítve',
        'cost' => 'Költség',
        'comment' => 'Megjegyzés',
        'simple_upload'=> 'kép',
    ));
     
    
    $xcrud22->validation_required('maincategorie');
    $xcrud22->highlight_row('special_cost', '>', 0, '#9ADAFF');

    $xcrud22->highlight('priceEach', '>', -300, '#9ADAFF');
    $xcrud22->pass_default('datum', date('Y-m-d'));
    $xcrud22->pass_default('created', date('Y-m-d H:i:s'));
    $xcrud22->before_list('chart_data', 'functions.php');
    $xcrud22->field_callback('maincategorie','cat_input');
    $xcrud22->field_callback('subcategorie','cat_input');
    $xcrud22->change_type('cost', 'text', '', array('maxlength'=>100));
    $xcrud22->before_insert('math_input', 'functions.php');
    $xcrud22->before_update('math_input', 'functions.php'); 
    $xcrud22->change_type('simple_upload', 'file', '', array('not_rename'=>true));
    $xcrud22->sum('cost', 'align-center', 'Total {value}');
    $xcrud22->create_action('my_action','my_function'); 
    
    $xcrud0 = Xcrud::get_instance();  
    $xcrud0->table('ktsgkbank');
    $xcrud0->columns('datum,maincategorie,subcategorie,cost,special_cost,comment,simple_upload');
    $xcrud0->order_by('datum','desc');
    $xcrud0->duplicate_button();
    $xcrud0->table_name('Költségek 2021', '','icon-leaf');
    $xcrud0->benchmark(true);
    $xcrud0->where('datum >', '2020-12-31')->where('datum <=', '2021-12-31');
    $xcrud0->limit(100);
    $xcrud0->label(array(
        'maincategorie' => 'Főkategória', 
        'subcategorie' => 'Alkategória',
        'datum' => 'Dátum',
        'created' => 'Rögzítve',
        'cost' => 'Költség',
        'comment' => 'Megjegyzés',
        'simple_upload'=> 'kép',
    ));
     
    
    $xcrud0->validation_required('maincategorie');
    $xcrud0->highlight_row('special_cost', '>', 0, '#9ADAFF');

    $xcrud0->highlight('priceEach', '>', -300, '#9ADAFF');
    $xcrud0->pass_default('datum', date('Y-m-d'));
    $xcrud0->pass_default('created', date('Y-m-d H:i:s'));
    $xcrud0->before_list('chart_data', 'functions.php');
    $xcrud0->field_callback('maincategorie','cat_input');
    $xcrud0->field_callback('subcategorie','cat_input');
    $xcrud0->change_type('cost', 'text', '', array('maxlength'=>100));
    $xcrud0->before_insert('math_input', 'functions.php');
    $xcrud0->before_update('math_input', 'functions.php'); 
    $xcrud0->change_type('simple_upload', 'file', '', array('not_rename'=>true));
    $xcrud0->sum('cost', 'align-center', 'Total {value}');
    $xcrud0->create_action('my_action','my_function'); 
    ////////////////////////////////
    
    $xcrud = Xcrud::get_instance();  
    $xcrud->table('ktsgkbank');
    $xcrud->columns('datum,maincategorie,subcategorie,cost,special_cost,comment,simple_upload');
    $xcrud->order_by('datum','desc');
    $xcrud->duplicate_button();
    $xcrud->table_name('Költségek 2020', '','icon-leaf');
    $xcrud->benchmark(true);
    $xcrud->where('datum >', '2019-12-31')->where('datum <=', '2020-12-31');
    $xcrud->limit(100);
    $xcrud->label(array(
        'maincategorie' => 'Főkategória', 
        'subcategorie' => 'Alkategória',
        'datum' => 'Dátum',
        'created' => 'Rögzítve',
        'cost' => 'Költség',
        'comment' => 'Megjegyzés',
        'simple_upload'=> 'kép',
    ));
     //   $xcrud->fields('orderDate,requiredDate');
    //$xcrud->change_type('orderDate', 'date', '', array('range_end' => 'requiredDate', 'placeholder' => 'Date from...'));
    //$xcrud->change_type('requiredDate', 'date', '', array('range_start' => 'orderDate', 'placeholder' => 'Date to...'));
    
    $xcrud->validation_required('maincategorie');
       //$xcrud->highlight_row('cost', '<=', 500, '#8DED79');
     $xcrud->highlight_row('special_cost', '>', 0, '#9ADAFF');

    $xcrud->highlight('priceEach', '>', -300, '#9ADAFF');
        $xcrud->pass_default('datum', date('Y-m-d'));
        $xcrud->pass_default('created', date('Y-m-d H:i:s'));
       $xcrud->before_list('chart_data', 'functions.php');
        $xcrud->field_callback('maincategorie','cat_input');
        $xcrud->field_callback('subcategorie','cat_input');
       //$grid_data = $xcrud->chart_data( 'functions.php');
        $xcrud->change_type('cost', 'text', '', array('maxlength'=>100));
        $xcrud->before_insert('math_input', 'functions.php');
        $xcrud->before_update('math_input', 'functions.php');
        //$xrcud->before_list_callback($grid_data, $xcrud);
       //print_r($xcrud->result);
        $xcrud->change_type('simple_upload', 'file', '', array('not_rename'=>true));
       /* $xcrud->change_type('simple_upload', 'image', false, array(
        'width' => 450,
        'path' => '../uploads/gallery',
        'thumbs' => array(array(
                'height' => 55,
                'width' => 120,
                'crop' => true,
                'marker' => '_th'))));*/
       // $xcrud->change_type('special_cost','checkbox');
        //$xcrud->change_type('created', 'hidden', false);
        
        $xcrud->sum('cost', 'align-center', 'Total {value}');
        $xcrud->create_action('my_action','my_function'); 
     
        
        $xcrud2 = Xcrud::get_instance(); 
        $xcrud2->table('ktsgkbank');
        $xcrud2->table_name('Költségek 2019', '','icon-leaf');
        $xcrud2->columns('datum,maincategorie,subcategorie,cost,special_cost,comment,simple_upload');
        $xcrud2->order_by('datum','desc');
        $xcrud2->where('datum >', '2018-12-31')->where('datum <=', '2019-12-31');
        $xcrud2->duplicate_button();
        $xcrud2->benchmark(true);
        $xcrud2->limit(50);
        $xcrud2->label(array(
            'maincategorie' => 'Főkategória', 
            'subcategorie' => 'Alkategória',
            'datum' => 'Dátum',
            'created' => 'Rögzítve',
            'cost' => 'Költség',
            'comment' => 'Megjegyzés',
            'simple_upload'=> 'kép',
        ));
        $xcrud2->validation_required('maincategorie');
        $xcrud2->highlight_row('special_cost', '>', 0, '#9ADAFF');

        $xcrud2->highlight('priceEach', '>', -300, '#9ADAFF');
        $xcrud2->pass_default('datum', date('Y-m-d'));
        $xcrud2->pass_default('created', date('Y-m-d H:i:s'));
        $xcrud2->before_list('chart_data', 'functions.php');
        $xcrud2->field_callback('maincategorie','cat_input');
        $xcrud2->field_callback('subcategorie','cat_input');
        $xcrud2->change_type('cost', 'text', '', array('maxlength'=>100));
        $xcrud2->before_insert('math_input', 'functions.php');
        $xcrud2->before_update('math_input', 'functions.php');

        $xcrud2->change_type('simple_upload', 'file', '', array('not_rename'=>true));
        $xcrud2->sum('cost', 'align-center', 'Total {value}');
        
        $xcrud3 = Xcrud::get_instance(); 
        $xcrud3->table('ktsgkbank');
        $xcrud3->table_name('Költségek 2018', '','icon-leaf');
        $xcrud3->columns('datum,maincategorie,subcategorie,cost,special_cost,comment,simple_upload');
        $xcrud3->order_by('datum','desc');
        $xcrud3->where('datum >', '2017-12-31')->where('datum <', '2018-12-31');
        $xcrud3->duplicate_button();
        $xcrud3->benchmark(true);
        $xcrud3->limit(10);
        $xcrud3->label(array(
            'maincategorie' => 'Főkategória', 
            'subcategorie' => 'Alkategória',
            'datum' => 'Dátum',
            'created' => 'Rögzítve',
            'cost' => 'Költség',
            'comment' => 'Megjegyzés',
            'simple_upload'=> 'kép',
        ));
        $xcrud3->validation_required('maincategorie');
        $xcrud3->highlight_row('special_cost', '>', 0, '#9ADAFF');

        $xcrud3->highlight('priceEach', '>', -300, '#9ADAFF');
        $xcrud3->pass_default('datum', date('Y-m-d'));
        $xcrud3->pass_default('created', date('Y-m-d H:i:s'));
        $xcrud3->before_list('chart_data', 'functions.php');
        $xcrud3->field_callback('maincategorie','cat_input');
        $xcrud3->field_callback('subcategorie','cat_input');
        $xcrud3->change_type('cost', 'text', '', array('maxlength'=>100));
        $xcrud3->before_insert('math_input', 'functions.php');
        $xcrud3->before_update('math_input', 'functions.php');

        $xcrud3->change_type('simple_upload', 'file', '', array('not_rename'=>true));
        $xcrud3->sum('cost', 'align-center', 'Total {value}');
        
        echo Xcrud::load_css();
        echo Xcrud::load_js();   
    ?>
    <button class="xcrud-action" data-task="action" data-action="my_action">GO!</button> 
    <button onclick="Xcrud.request('.xcrud-ajax',Xcrud.list_data('.xcrud-ajax',{task:'list',primary:270}))">Edit entry </button>
    
<!-- Resources -->
<script src="https://www.amcharts.com/lib/3/amcharts.js"></script>
<script src="https://www.amcharts.com/lib/3/pie.js"></script>
<script src="https://www.amcharts.com/lib/3/serial.js"></script>
<script src="https://www.amcharts.com/lib/3/plugins/export/export.min.js"></script>
<link rel="stylesheet" href="https://www.amcharts.com/lib/3/plugins/export/export.css" type="text/css" media="all" />
<script src="https://www.amcharts.com/lib/3/themes/light.js"></script>
<script src="https://www.amcharts.com/lib/3/themes/patterns.js"></script>

<!-- Resources amcharts 4-->
<script src="https://www.amcharts.com/lib/4/core.js"></script>
<script src="https://www.amcharts.com/lib/4/charts.js"></script>
<script src="https://www.amcharts.com/lib/4/themes/animated.js"></script>

    <script type="text/javascript">
     console.log(''+ JSON.stringify(Xcrud));
jQuery(document).on("xcrudafterrequest",function(event,container){
//   console.log('dsdsds'+Xcrud.data);
    if(Xcrud.current_task == 'list')
    {
       // Xcrud.show_message(container,'WOW!','success');

    }
 
});
</script>

<script>
var chart2 ="";
 $(document).ready(function () {
   /*   $("select.xcrud-data.xcrud-columns-select.input-small.form-control option[value='ktsgkbank.datum']").attr('selected',true);
     var search = $('.xcrud-nav').clone();
     $("select.xcrud-daterange.xcrud-searchdata.input-small.form-control.xcrud-search").addClass("active").show();
     //console.log('dsdsds'+ $search);
     $('.xcrud-top-actions').append(search);
     $('.xcrud-search.form-inline').show();
      $('.xcrud-datepicker-from').show();
       $('.xcrud-datepicker-to').show();*/
/*    $.ajax({ 
        type: 'POST', 
        url: '../demos/ownpages/ajax_fc.php', 
        data: { 
            start: '2017-01-01',
            end: '2019-12-31',
            table: 'ktsgkbank'
        }, 
        dataType: 'json',
        success: function (daata) { 
            // console.log(daata);
            var chart = AmCharts.makeChart( "chartdiv", {
                  "type": "pie",
                  "theme": "none",
                  "dataProvider": daata.pie,
                  "valueField": "loss",
                  "titleField": "cat",
                  "outlineAlpha": 0.1,
                  "depth3D": 15,
                  "balloonText": "[[title]]<br><span style='font-size:14px'><b>[[value]]</b> ([[percents]]%)</span>",
                  "angle": 30,
                  "export": {
                    "enabled": true
                  }
                } );
            var chart3 = AmCharts.makeChart("chartdiv3", {
                "type": "serial",
            	"theme": "none",
                "legend": {
                    "horizontalGap": 10,
                    "maxColumns": 1,
                    "position": "right",
            		"useGraphSettings": true,
            		"markerSize": 10
                },
                "dataProvider":daata.compare.data,
                "valueAxes": [{
                    "stackType": "regular",
                    "axisAlpha": 0.3,
                    "gridAlpha": 0
                }],
                "graphs":daata.compare.graph,
                "categoryField": "type",
                "categoryAxis": {
                    "gridPosition": "start",
                    "axisAlpha": 0,
                    "gridAlpha": 0,
                    "position": "left"
                },
                "export": {
                	"enabled": true
                 }
            
            });
        }
    }); 
    $.ajax({ 
        type: 'POST', 
        url: '../demos/ownpages/ajax_ch.php', 
        data: { 
            start: '2017-01-01',
            end: '2019-12-31',
            table: 'ktsgkbank',
        }, 
        dataType: 'json',
        success: function (data_ch) { 
        // console.log(data_ch);
        var chart2 = AmCharts.makeChart("chartdiv2", {
            "type": "serial",
            "theme": "none",
            "marginRight": 40,
            "marginLeft": 40,
            "autoMarginOffset": 20,
            "mouseWheelZoomEnabled":true,
            "dataDateFormat": "YYYY-MM-DD",
            "valueAxes": [{
                "id": "v1",
                "axisAlpha": 0,
                "position": "left",
                "ignoreAxisWidth":true
            }],
            "balloon": {
                "borderThickness": 1,
                "shadowAlpha": 0
            },
            "graphs": [{
                "id": "g1",
                "balloon":{
                  "drop":true,
                  "adjustBorderColor":false,
                  "color":"#ffffff"
                },
                "bullet": "round",
                "bulletBorderAlpha": 1,
                "bulletColor": "#FFFFFF",
                "bulletSize": 5,
                "hideBulletsCount": 50,
                "lineThickness": 2,
                "title": "red line",
                "useLineColorForBulletBorder": true,
                "valueField": "value",
                "balloonText": "<span style='font-size:10px;'>[[info]]</span>"
            }],
            "chartScrollbar": {
                "graph": "g1",
                "oppositeAxis":false,
                "offset":30,
                "scrollbarHeight": 80,
                "backgroundAlpha": 0,
                "selectedBackgroundAlpha": 0.1,
                "selectedBackgroundColor": "#888888",
                "graphFillAlpha": 0,
                "graphLineAlpha": 0.5,
                "selectedGraphFillAlpha": 0,
                "selectedGraphLineAlpha": 1,
                "autoGridCount":true,
                "color":"#AAAAAA"
            },
            "chartCursor": {
                "pan": true,
                "valueLineEnabled": true,
                "valueLineBalloonEnabled": true,
                "cursorAlpha":1,
                "cursorColor":"#258cbb",
                "limitToGraph":"g1",
                "valueLineAlpha":0.2,
                "valueZoomable":true
            },
            "valueScrollbar":{
              "oppositeAxis":false,
              "offset":50,
              "scrollbarHeight":10
            },
            "categoryField": "date",
            "categoryAxis": {
                "parseDates": true,
                "dashLength": 1,
                "minorGridEnabled": true
            },
            "export": {
                "enabled": true
            },
            "dataProvider": data_ch,
        });

        }
    });
    var style = [];
    var jsonArr = [];
for (var i = 0; i < 9; i++) {
    jsonArr.push({
        "balloonText": "[[category]] : <b>[[value]]</b>",
        "fillAlphas": 0.9,
        "lineAlpha": "0."+i,
        "title": "Month"+i,
        "type": "column",
        "valueField": "Month"+i
    });
}
      $.ajax({ 
        type: 'POST', 
        url: '../demos/ownpages/ajax_cat_sum.php', 
        data: { 
            start: '2017-01-01',
            end: '2020-12-31',
            table: 'ktsgkbank'
        }, 
        dataType: 'json',
        success: function (cat_sum) { 
            //chart.data = cat_sum;
            console.log(cat_sum);
        
         var chart = AmCharts.makeChart("chartdiv4", {
                "theme": "none",
                "type": "serial",
                "dataProvider": cat_sum,
                "valueAxes": [{
                    "stackType": "3d",
                    "unit": "EUR",
                    "position": "left",
                    "title": "sum",
                }],
                "startDuration": 1,
                "graphs": jsonArr,
                "plotAreaFillAlphas": 0.1,
                "depth3D": 70,
                "angle": 70,
                "categoryField": "maincat",
                "categoryAxis": {
                    "gridPosition": "start"
                },
                "export": {
                    "enabled": true
                 }
            });
       
        }
    }); 
  */
});

</script>

<!-- Chart code -->
<script>
/*am4core.ready(function() {
    
    // Themes begin
    am4core.useTheme(am4themes_animated);
    // Themes end
    
    // Create chart instance
    var chart = am4core.create("chartdiv4", am4charts.XYChart);

   // Add data
    $.ajax({ 
        type: 'POST', 
        url: '../demos/ownpages/ajax_cat_sum.php', 
        data: { 
            start: '2017-01-01',
            end: '2020-12-31',
            table: 'ktsgkbank'
        }, 
        dataType: 'json',
        success: function (cat_sum) { 
            chart.data = cat_sum;
            console.log(cat_sum);
        }
    }); 

    // Create axes
    var yAxis = chart.yAxes.push(new am4charts.CategoryAxis());
    yAxis.dataFields.category = "maincat";
    yAxis.renderer.grid.template.location = 0;
    yAxis.renderer.labels.template.fontSize = 10;
    yAxis.renderer.minGridDistance = 10;
    
    var xAxis = chart.xAxes.push(new am4charts.ValueAxis());
    
    // Create series
    var series = chart.series.push(new am4charts.ColumnSeries());
    series.dataFields.valueX = "sum";
    series.dataFields.categoryY = "maincat";
    series.columns.template.tooltipText = "{categoryY}: [bold]{valueX}[/]";
    series.columns.template.strokeWidth = 0;
    series.columns.template.adapter.add("fill", function(fill, target) {
      if (target.dataItem) {
        switch(target.dataItem.dataContext.Month) {
          case "01":
            return chart.colors.getIndex(0);
            break;
          case "02":
            return chart.colors.getIndex(1);
            break;
          case "03":
            return chart.colors.getIndex(2);
            break;
          case "04":
            return chart.colors.getIndex(3);
            break;
         case "05":
            return chart.colors.getIndex(4);
            break;  
         case "06":
            return chart.colors.getIndex(5);
            break;
         case "07":
            return chart.colors.getIndex(6);
            break;
        case "08":
            return chart.colors.getIndex(7);
            break;
        }
      }
      return fill;
    });
    
    // Add ranges
    function addRange(label, start, end, color) {
      var range = yAxis.axisRanges.create();
      range.category = start;
      range.endCategory = end;
      range.label.text = label;
      range.label.disabled = false;
      range.label.fill = color;
      range.label.location = 0;
      range.label.dx = -130;
      range.label.dy = 12;
      range.label.fontWeight = "bold";
      range.label.fontSize = 12;
      range.label.horizontalCenter = "left"
      range.label.inside = true;
      
      range.grid.stroke = am4core.color("#396478");
      range.grid.strokeOpacity = 1;
      range.tick.length = 200;
      range.tick.disabled = false;
      range.tick.strokeOpacity = 0.6;
      range.tick.stroke = am4core.color("#396478");
      range.tick.location = 0;
      
      range.locations.category = 1;
    }
    
    addRange("01", "Banki költség", "Lakás", chart.colors.getIndex(0));
    addRange("02", "Egyéb", "Lakás", chart.colors.getIndex(1));
    addRange("03", "Banki költség", "Lakás", chart.colors.getIndex(2));
    addRange("04", "Banki költség", "Lakás", chart.colors.getIndex(3));
    addRange("05", "Egyéb", "Lakás", chart.colors.getIndex(4));
    addRange("06", "Banki költség", "Autó", chart.colors.getIndex(5));
    addRange("07", "Banki költség", "Autó", chart.colors.getIndex(6)); 
    addRange("08", "Tömegközlekedés", "Szórakozás", chart.colors.getIndex(7));

    
    chart.cursor = new am4charts.XYCursor();

}); // end am4core.ready()*/
</script>
<script>


function zoomChart() {
    chart2.zoomToIndexes(chart2.dataProvider.length - 40, chart2.dataProvider.length - 1);
}
</script>
<?echo $xcrud23->render();?>
<?echo $xcrud22->render();?>
<?echo $xcrud0->render();?>
<?echo $xcrud->render();?>
<?echo $xcrud2->render();?>
<div class="row">
    <div class="col-md-12">
        <div id="chartdiv2" style="width: 100%;height: 550px;"></div>
    </div>
    <div class="col-md-12">
        <div id="chartdiv" style="width: 100%;height: 350px;"></div>
    </div>
    <div class="col-md-12">
        <div id="chartdiv3" style="width: 100%;height: 350px;"></div>
    </div>
    <div class="col-md-12">
        <div id="chartdiv4" style=" width: 100%;height: 800px;"></div>
    </div>
</div>