<?php
require __DIR__ . '/../_boot.php';
require __DIR__ . '/../partials/nav.php';

if (!$user || !\App\Core\Auth::hasPermission('loyalty.manage', $user)) {
    header('Location: dashboard.php');
    exit;
}
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Inactivity Rules | Pyrk Admin</title>
  <link rel="stylesheet" href="../assets/css/app.css">
  <link rel="stylesheet" href="../assets/css/admin_inactivity_rules.css?v=1">
</head>
<body>
<?php pyrk_render_nav('admin'); ?>

<main class="container enterprise-page inactivity-page">
  <section class="page-hero admin-hero enterprise-hero inactivity-hero">
    <div>
      <p class="eyebrow">Loyalty automation</p>
      <h1>Inactive member rules</h1>
      <p class="muted">Create professional rules that deduct loyalty points when members stay inactive for a period you define.</p>
    </div>
    <div class="hero-actions">
      <button class="btn btn-light" type="button" id="resetRuleBtn">New rule</button>
      <button class="btn" type="button" id="runAllRulesBtn">Apply active rules now</button>
    </div>
  </section>

  <section class="grid four compact-stats inactivity-stats">
    <div class="stat stat-pro"><span>Rules</span><strong id="statRules">0</strong></div>
    <div class="stat stat-pro"><span>Active</span><strong id="statActiveRules">0</strong></div>
    <div class="stat stat-pro"><span>Members penalized 30d</span><strong id="statMembers30">0</strong></div>
    <div class="stat stat-pro"><span>Points deducted 30d</span><strong id="statPoints30">0</strong></div>
  </section>

  <section class="grid inactivity-layout">
    <section class="panel inactivity-form-panel">
      <div class="section-head">
        <div>
          <p class="eyebrow" id="ruleFormMode">Create</p>
          <h2 id="ruleFormTitle">Inactivity rule</h2>
          <p class="muted small">Example: deduct 50 points if no activity for 60 days. Repeat only once every 60 days.</p>
        </div>
      </div>

      <form id="ruleForm" class="stack-form">
        <input type="hidden" name="id" id="ruleId">
        <label>Rule name<input name="rule_name" id="ruleName" placeholder="Example: 60-day inactivity penalty" required></label>
        <label>Description<textarea name="description" id="ruleDescription" rows="3" placeholder="Optional admin note"></textarea></label>

        <div class="form-grid three">
          <label>Inactive for
            <input name="inactive_value" id="inactiveValue" type="number" min="1" value="60">
          </label>
          <label>Period
            <select id="inactiveUnit">
              <option value="days">Days</option>
              <option value="weeks">Weeks</option>
              <option value="months" selected>Months</option>
            </select>
          </label>
          <label>Penalty points
            <input name="penalty_points" id="penaltyPoints" type="number" min="1" value="50">
          </label>
        </div>

        <div class="form-grid three">
          <label>Repeat protection
            <input name="frequency_days" id="frequencyDays" type="number" min="1" value="60">
            <small>Same member cannot be penalized again by this rule before this many days.</small>
          </label>
          <label>Minimum current points
            <input name="min_points_before_penalty" id="minPoints" type="number" min="0" value="1">
          </label>
          <label>Max members per run
            <input name="max_members_per_run" id="maxMembers" type="number" min="0" value="0">
            <small>0 = no limit.</small>
          </label>
        </div>

        <div class="form-grid three">
          <label>Activity basis
            <select name="activity_basis" id="activityBasis">
              <option value="any_activity">Any walk activity / registration</option>
              <option value="completed_walk">Completed walks only</option>
            </select>
          </label>
          <label>Target account status
            <select name="target_user_status" id="targetStatus">
              <option value="active">Active accounts only</option>
              <option value="inactive">Inactive accounts only</option>
              <option value="all">All member accounts</option>
            </select>
          </label>
          <label>Status
            <select name="status" id="ruleStatus">
              <option value="active">Active</option>
              <option value="inactive">Inactive</option>
            </select>
          </label>
        </div>

        <label class="pro-switch">
          <input type="checkbox" name="allow_negative" id="allowNegative" value="1">
          <span><b></b></span>
          <strong>Allow member points to go below zero</strong>
        </label>

        <input type="hidden" name="inactivity_days" id="inactivityDays" value="60">

        <div class="rule-preview-box" id="rulePreviewBox">
          Deduct <strong>50</strong> points after <strong>60 days</strong> of inactivity.
        </div>

        <div class="form-actions">
          <button class="btn btn-light" type="button" id="previewRuleBtn">Preview members</button>
          <button class="btn" type="submit">Save rule</button>
        </div>
        <p id="ruleMsg" class="notice"></p>
      </form>
    </section>

    <section class="panel">
      <div class="section-head">
        <div>
          <p class="eyebrow">Rules</p>
          <h2>Configured rules</h2>
        </div>
      </div>
      <div id="rulesList" class="inactivity-rules-list"></div>
    </section>
  </section>

  <section class="grid two">
    <section class="panel">
      <div class="section-head">
        <div>
          <p class="eyebrow">Preview</p>
          <h2>Eligible members</h2>
          <p class="muted small">Preview does not deduct points.</p>
        </div>
      </div>
      <div class="table-wrap">
        <table class="pro-table inactivity-preview-table">
          <thead><tr><th>Member</th><th>Current points</th><th>Inactive days</th><th>Deduct</th><th>Last activity</th></tr></thead>
          <tbody id="previewRows"><tr><td colspan="5" class="muted">Choose a rule and click Preview members.</td></tr></tbody>
        </table>
      </div>
    </section>

    <section class="panel">
      <div class="section-head">
        <div>
          <p class="eyebrow">Audit</p>
          <h2>Recent runs</h2>
        </div>
      </div>
      <div id="runHistory" class="rule-run-history"></div>
    </section>
  </section>
</main>

<script>
window.PYRK_API_BASE='../../api';
window.PYRK_CSRF='<?= htmlspecialchars($csrf) ?>';
</script>
<script src="../assets/js/api.js"></script>
<script src="../assets/js/admin_inactivity_rules.js?v=1"></script>
</body>
</html>
