function previewImage() {
  const fileInput = document.getElementById('image');
  const previewContainer = document.getElementById('imagePreview');
  previewContainer.innerHTML = '';

  const file = fileInput.files[0];
  if (file) {
    const reader = new FileReader();
    reader.onload = function (e) {
      const img = document.createElement('img');
      img.src = e.target.result;
      img.classList.add('img-fluid', 'img-thumbnail', 'mt-2');
      img.style.maxHeight = '200px';
      previewContainer.appendChild(img);
    };
    reader.readAsDataURL(file);
  }
}

function resetPreview() {
  document.getElementById('imagePreview').innerHTML = '';
}

  $(document).ready(function() {

    $('#description').summernote({
      height: 150
    });

    const data = jsonData;

    // Initialize statisticsChart
    var ctx = document.getElementById('statisticsChart').getContext('2d');
    var statisticsChart = new Chart(ctx, {
      type: 'line',
      data: {
        labels: data.months,
        datasets: [
          {
            label: "Users",
            borderColor: 'rgba(255, 193, 7, 1)',
            pointBackgroundColor: 'rgba(255, 193, 7, 0.6)',
            pointRadius: 0,
            backgroundColor: 'rgba(255, 193, 7, 0.4)',
            legendColor: '#ffc107',
            fill: true,
            borderWidth: 2,
            data: data.users
          },
          {
            label: "Deposit",
            borderColor: 'rgba(23, 125, 255, 1)',
            pointBackgroundColor: 'rgba(23, 125, 255, 0.6)',
            pointRadius: 0,
            backgroundColor: 'rgba(23, 125, 255, 0.4)',
            legendColor: '#177dff',
            fill: true,
            borderWidth: 2,
            data: data.deposits
          },
          {
            label: "Withdrawal",
            borderColor: '#f3545d',
            pointBackgroundColor: 'rgba(243, 84, 93, 0.6)',
            pointRadius: 0,
            backgroundColor: 'rgba(243, 84, 93, 0.4)',
            legendColor: '#f3545d',
            fill: true,
            borderWidth: 2,
            data: data.withdrawals
          }
        ]
      },
      options: {
        responsive: true,
        maintainAspectRatio: false,
        legend: {
          display: false
        },
        tooltips: {
          bodySpacing: 4,
          mode: "nearest",
          intersect: 0,
          position: "nearest",
          xPadding: 10,
          yPadding: 10,
          caretPadding: 10
        },
        layout: {
          padding: {
            left: 5,
            right: 5,
            top: 15,
            bottom: 15
          }
        },
        scales: {
          yAxes: [
            {
              ticks: {
                fontStyle: "500",
                beginAtZero: false,
                maxTicksLimit: 5,
                padding: 10
              },
              gridLines: {
                drawTicks: false,
                display: false
              }
            }
          ],
          xAxes: [
            {
              gridLines: {
                zeroLineColor: "transparent"
              },
              ticks: {
                padding: 10,
                fontStyle: "500"
              }
            }
          ]
        },
        legendCallback: function(chart) {
          var text = [];
          text.push('<ul class="' + chart.id + '-legend html-legend">');
          for (var i = 0; i < chart.data.datasets.length; i++) {
            text.push('<li><span style="background-color:' + chart.data.datasets[i].legendColor + '"></span>');
            if (chart.data.datasets[i].label) {
              text.push(chart.data.datasets[i].label);
            }
            text.push('</li>');
          }
          text.push('</ul>');
          return text.join('');
        }
      }
    });

    // Generate legend
    var myLegendContainer = document.getElementById("myChartLegend");
    myLegendContainer.innerHTML = statisticsChart.generateLegend();

    // Add click event to legend items
    var legendItems = myLegendContainer.getElementsByTagName('li');
    for (var i = 0; i < legendItems.length; i++) {
      legendItems[i].addEventListener("click", legendClickCallback, false);
    }
  });

var dailySalesChart = document.getElementById('dailySalesChart').getContext('2d');

var myDailySalesChart = new Chart(dailySalesChart, {
  type: 'line',
  data: {
    labels: [
      "00:00", "01:00", "02:00", "03:00", "04:00", "05:00",
      "06:00", "07:00", "08:00", "09:00", "10:00", "11:00",
      "12:00", "13:00", "14:00", "15:00", "16:00", "17:00",
      "18:00", "19:00", "20:00", "21:00", "22:00", "23:00"
    ], // Waktu dalam sehari
    datasets: [
      {
        label: "Penghasilan Harian",
        fill: true,
        backgroundColor: "rgba(255, 255, 255, 0.2)",
        borderColor: "#fff",
        pointBorderColor: "#fff",
        pointBackgroundColor: "#fff",
        pointBorderWidth: 1,
        pointHoverRadius: 5,
        pointHoverBackgroundColor: "#fff",
        pointHoverBorderColor: "#fff",
        pointHoverBorderWidth: 1,
        pointRadius: 3,
        pointHitRadius: 10,
        data: dailyData // Data total transaksi per jam
      }
    ]
  },
  options: {
    maintainAspectRatio: false,
    legend: {
      display: true
    },
    animation: {
      easing: "easeInOutBack"
    },
    scales: {
      yAxes: [
        {
          display: true,
          ticks: {
            fontColor: "rgba(0,0,0,0.7)",
            fontStyle: "bold",
            beginAtZero: true,
            callback: function (value) {
              return "Rp " + value.toLocaleString(); // Format Rupiah
            }
          },
          gridLines: {
            drawTicks: true,
            display: true
          }
        }
      ],
      xAxes: [
        {
          display: true,
          gridLines: {
            zeroLineColor: "transparent"
          },
          ticks: {
            fontColor: "rgba(0,0,0,0.7)",
            fontStyle: "bold"
          }
        }
      ]
    }
  }
});