<!DOCTYPE html>
<html>
<head>
<!-- FORM CSS CODE -->
<?php include"comman/code_css_form.php"; ?>
<style>
.cleanup-warning {
    background: linear-gradient(135deg, #eb3349 0%, #f45c43 100%);
    color: white;
    padding: 20px;
    border-radius: 8px;
    margin-bottom: 20px;
    box-shadow: 0 4px 12px rgba(235, 51, 73, 0.3);
}
.cleanup-stats {
    background: white;
    padding: 20px;
    border-radius: 8px;
    margin-bottom: 20px;
    border-left: 4px solid #11998e;
}
.stat-item {
    padding: 10px;
    margin: 5px 0;
    background: rgba(17, 153, 142, 0.05);
    border-radius: 4px;
}
.confirm-section {
    background: #fff3cd;
    border: 2px solid #ffc107;
    padding: 20px;
    border-radius: 8px;
    margin-top: 20px;
}
.btn-danger-custom {
    background: linear-gradient(135deg, #eb3349 0%, #f45c43 100%);
    border: none;
    color: white;
    padding: 12px 30px;
    font-weight: 600;
    border-radius: 8px;
    transition: all 0.3s ease;
}
.btn-danger-custom:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(235, 51, 73, 0.4);
    color: white;
}
</style>
</head>

<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">

  <?php include"sidebar.php"; ?>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
        <i class="fa fa-warning text-danger"></i> Database Cleanup
        <small>Remove All Dummy Data</small>
      </h1>
      <ol class="breadcrumb">
        <li><a href="<?php echo $base_url; ?>dashboard"><i class="fa fa-dashboard"></i> Home</a></li>
        <li class="active">Cleanup</li>
      </ol>
    </section>

    <!-- Main content -->
    <section class="content">
      <div class="row">
        <!-- ALERT MESSAGE -->
        <?php include"comman/code_flashdata.php"; ?>

        <div class="col-xs-12">

          <!-- WARNING BOX -->
          <div class="cleanup-warning">
            <h3><i class="fa fa-exclamation-triangle"></i> DANGER: Data Deletion Warning</h3>
            <p style="font-size: 16px; margin-top: 10px;">
              This action will <strong>PERMANENTLY DELETE</strong> all transactional and dummy data from your database.
              This includes all sales, purchases, customers, items, and related records.
            </p>
            <p style="font-size: 14px; margin-top: 10px;">
              <i class="fa fa-info-circle"></i> A backup will be created automatically before deletion.
            </p>
          </div>

          <!-- STATISTICS BOX -->
          <div class="box">
            <div class="box-header with-border">
              <h3 class="box-title"><i class="fa fa-database"></i> Data to be Removed</h3>
            </div>
            <div class="box-body">
              <div class="row">
                <div class="col-md-6">
                  <h4><strong>Transactional Data:</strong></h4>
                  <div class="cleanup-stats">
                    <?php if(isset($stats['tables'])): ?>
                      <?php
                      $total_trans = 0;
                      foreach($stats['tables'] as $table => $count):
                        $total_trans += $count;
                      ?>
                        <div class="stat-item">
                          <i class="fa fa-table"></i> <?= str_replace('db_', '', $table); ?>:
                          <strong><?= number_format($count); ?> records</strong>
                        </div>
                      <?php endforeach; ?>
                      <hr>
                      <div class="stat-item" style="background: rgba(17, 153, 142, 0.15);">
                        <strong>Total Transactional Records: <?= number_format($total_trans); ?></strong>
                      </div>
                    <?php endif; ?>
                  </div>
                </div>

                <div class="col-md-6">
                  <h4><strong>Master Data:</strong></h4>
                  <div class="cleanup-stats">
                    <div class="stat-item">
                      <i class="fa fa-users"></i> Customers:
                      <strong><?= number_format($stats['customers']); ?> records</strong>
                    </div>
                    <div class="stat-item">
                      <i class="fa fa-cubes"></i> Items:
                      <strong><?= number_format($stats['items']); ?> records</strong>
                    </div>
                    <div class="stat-item">
                      <i class="fa fa-tags"></i> Brands:
                      <strong><?= number_format($stats['brands']); ?> records</strong>
                    </div>
                    <div class="stat-item">
                      <i class="fa fa-folder"></i> Categories:
                      <strong><?= number_format($stats['categories']); ?> records</strong>
                    </div>
                    <hr>
                    <div class="stat-item" style="background: rgba(17, 153, 142, 0.15);">
                      <strong>Total Master Records: <?= number_format($stats['total_master_records']); ?></strong>
                    </div>
                  </div>
                </div>
              </div>

              <!-- GRAND TOTAL -->
              <div class="alert alert-info" style="margin-top: 20px; font-size: 16px;">
                <i class="fa fa-info-circle"></i>
                <strong>Total Records to Delete:
                  <?= number_format($stats['total_transactional_records'] + $stats['total_master_records']); ?></strong>
              </div>
            </div>
          </div>

          <!-- BACKUP INFO -->
          <div class="box box-success">
            <div class="box-header with-border">
              <h3 class="box-title"><i class="fa fa-shield"></i> Backup Protection</h3>
            </div>
            <div class="box-body">
              <?php if($backup_writable): ?>
                <p><i class="fa fa-check-circle text-success"></i>
                  <strong>Automatic backup will be created in:</strong>
                  <code><?= FCPATH . 'backup/'; ?></code>
                </p>
                <p><i class="fa fa-info-circle"></i>
                  Backup filename format: <code>pos_backup_YYYY-MM-DD_HH-mm-ss.zip</code>
                </p>
              <?php else: ?>
                <div class="alert alert-danger">
                  <i class="fa fa-exclamation-triangle"></i>
                  <strong>Warning:</strong> Backup directory is not writable. Please ensure proper permissions.
                </div>
              <?php endif; ?>
            </div>
          </div>

          <!-- CONFIRMATION FORM -->
          <div class="box box-danger">
            <div class="box-header with-border">
              <h3 class="box-title"><i class="fa fa-key"></i> Confirm Cleanup</h3>
            </div>
            <div class="box-body">
              <div class="confirm-section">
                <form method="POST" action="<?= base_url('cleanup/execute'); ?>" onsubmit="return confirmCleanup();">
                  <input type="hidden" name="<?= $this->security->get_csrf_token_name(); ?>"
                         value="<?= $this->security->get_csrf_hash(); ?>">

                  <div class="form-group">
                    <label style="font-size: 16px;">
                      <i class="fa fa-warning text-danger"></i>
                      To confirm, type <strong>YES_DELETE_ALL_DATA</strong> below:
                    </label>
                    <input type="text"
                           name="confirm_cleanup"
                           id="confirm_cleanup"
                           class="form-control input-lg"
                           placeholder="Type confirmation text here..."
                           required
                           style="border: 2px solid #dc3545; font-weight: 600;">
                  </div>

                  <div class="checkbox">
                    <label style="font-size: 14px;">
                      <input type="checkbox" id="understand_checkbox" required>
                      <strong>I understand this action cannot be undone</strong>
                    </label>
                  </div>

                  <hr>

                  <div class="text-center">
                    <button type="submit"
                            class="btn btn-danger-custom btn-lg"
                            id="cleanup_btn"
                            disabled>
                      <i class="fa fa-trash"></i> DELETE ALL DUMMY DATA
                    </button>
                    <a href="<?= base_url('dashboard'); ?>" class="btn btn-default btn-lg">
                      <i class="fa fa-times"></i> Cancel
                    </a>
                  </div>
                </form>
              </div>
            </div>
          </div>

        </div>
      </div>
    </section>
  </div>

  <?php include"footer.php"; ?>
</div>

<!-- JAVASCRIPT CODE -->
<?php include"comman/code_js_form.php"; ?>

<script>
// Enable submit button only when confirmation is typed correctly
$('#confirm_cleanup, #understand_checkbox').on('keyup change', function() {
    var confirmText = $('#confirm_cleanup').val();
    var checkboxChecked = $('#understand_checkbox').is(':checked');

    if(confirmText === 'YES_DELETE_ALL_DATA' && checkboxChecked) {
        $('#cleanup_btn').prop('disabled', false);
    } else {
        $('#cleanup_btn').prop('disabled', true);
    }
});

// Final confirmation before submit
function confirmCleanup() {
    return confirm(
        'FINAL WARNING!\n\n' +
        'This will permanently delete all dummy data.\n' +
        'A backup will be created first.\n\n' +
        'Are you absolutely sure you want to proceed?'
    );
}
</script>

</body>
</html>
