================================================================================
⚡ QUICK COMMANDS - GENERATE VOUCHER AGENT
================================================================================

COMMAND CEPAT UNTUK TESTING DAN TROUBLESHOOTING

================================================================================

1. CEK FILE ADA ATAU TIDAK:
===========================

Windows (CMD):
--------------
dir mikhmon\lib\routeros_api.class.php
dir mikhmon\agent\generate.php
dir mikhmon\include\config.php
dir mikhmon\include\db_config.php

Windows (PowerShell):
--------------------
Test-Path mikhmon\lib\routeros_api.class.php
Test-Path mikhmon\agent\generate.php
Test-Path mikhmon\include\config.php
Test-Path mikhmon\include\db_config.php

Linux/Mac:
----------
ls -la mikhmon/lib/routeros_api.class.php
ls -la mikhmon/agent/generate.php
ls -la mikhmon/include/config.php
ls -la mikhmon/include/db_config.php

================================================================================

2. CEK ISI FILE (GREP):
=======================

Windows (PowerShell):
--------------------
# Cek fungsi randNULC ada atau tidak
Select-String -Path mikhmon\lib\routeros_api.class.php -Pattern "function randNULC"

# Cek include routeros_api.class.php
Select-String -Path mikhmon\agent\generate.php -Pattern "routeros_api.class.php"

# Cek session MikroTik
Select-String -Path mikhmon\include\config.php -Pattern "ALIJAYA-NET"

Linux/Mac:
----------
# Cek fungsi randNULC ada atau tidak
grep "function randNULC" mikhmon/lib/routeros_api.class.php

# Cek include routeros_api.class.php
grep "routeros_api.class.php" mikhmon/agent/generate.php

# Cek session MikroTik
grep "ALIJAYA-NET" mikhmon/include/config.php

================================================================================

3. CEK DATABASE:
================

MySQL Command Line:
-------------------
mysql -u root -p

USE mikhmon;

-- Cek tabel ada atau tidak
SHOW TABLES LIKE 'agent_%';

-- Cek struktur tabel
DESCRIBE agent_system;
DESCRIBE agent_vouchers;
DESCRIBE agent_transactions;
DESCRIBE agent_prices;

-- Cek data agent
SELECT * FROM agent_system;

-- Cek saldo agent
SELECT agent_code, name, balance FROM agent_system WHERE status = 'active';

-- Cek harga profile
SELECT a.agent_code, ap.profile_name, ap.buy_price, ap.sell_price 
FROM agent_prices ap 
JOIN agent_system a ON ap.agent_id = a.agent_id;

-- Cek voucher terakhir
SELECT * FROM agent_vouchers ORDER BY created_at DESC LIMIT 10;

-- Cek transaksi terakhir
SELECT * FROM agent_transactions ORDER BY created_at DESC LIMIT 10;

phpMyAdmin:
-----------
1. Buka: http://localhost/phpmyadmin
2. Pilih database: mikhmon
3. Cek tabel: agent_system, agent_vouchers, agent_transactions, agent_prices

================================================================================

4. CEK PHP ERROR LOG:
=====================

Windows (XAMPP):
----------------
type C:\xampp\apache\logs\error.log | findstr /i "agent"
type C:\xampp\php\logs\php_error_log | findstr /i "agent"

Windows (Laragon):
------------------
type C:\laragon\etc\apache2\logs\error.log | findstr /i "agent"

Linux/Mac:
----------
tail -f /var/log/apache2/error.log | grep -i agent
tail -f /var/log/php/error.log | grep -i agent

================================================================================

5. CEK MIKROTIK CONNECTION:
===========================

PHP Test Script (test_mikrotik.php):
------------------------------------
<?php
include('include/config.php');
include('lib/routeros_api.class.php');

$sessions = array_keys($data);
foreach ($sessions as $s) {
    if ($s != 'mikhmon') {
        $session = $s;
        break;
    }
}

if ($session && isset($data[$session])) {
    $iphost = explode('!', $data[$session][1])[1];
    $userhost = explode('@|@', $data[$session][2])[1];
    $passwdhost = explode('#|#', $data[$session][3])[1];
    
    echo "IP: $iphost\n";
    echo "User: $userhost\n";
    
    $API = new RouterosAPI();
    if ($API->connect($iphost, $userhost, decrypt($passwdhost))) {
        echo "✅ Koneksi MikroTik BERHASIL!\n";
        $API->disconnect();
    } else {
        echo "❌ Koneksi MikroTik GAGAL!\n";
    }
} else {
    echo "❌ Session MikroTik tidak ditemukan!\n";
}
?>

Jalankan:
php test_mikrotik.php

================================================================================

6. TEST FUNGSI RANDOM:
======================

PHP Test Script (test_random.php):
----------------------------------
<?php
include('lib/routeros_api.class.php');

echo "Testing fungsi random generator:\n\n";

echo "randN(6): " . randN(6) . "\n";
echo "randUC(6): " . randUC(6) . "\n";
echo "randLC(6): " . randLC(6) . "\n";
echo "randULC(6): " . randULC(6) . "\n";
echo "randNLC(6): " . randNLC(6) . "\n";
echo "randNUC(6): " . randNUC(6) . "\n";
echo "randNULC(6): " . randNULC(6) . "\n";

echo "\nContoh Username:\n";
for ($i = 1; $i <= 5; $i++) {
    echo "AG" . randNULC(8) . "\n";
}

echo "\nContoh Password:\n";
for ($i = 1; $i <= 5; $i++) {
    echo randNULC(6) . "\n";
}
?>

Jalankan:
php test_random.php

================================================================================

7. CEK AGENT SESSION:
=====================

PHP Test Script (test_agent_session.php):
-----------------------------------------
<?php
session_start();

if (isset($_SESSION['agent_id'])) {
    echo "✅ Agent sudah login!\n";
    echo "Agent ID: " . $_SESSION['agent_id'] . "\n";
    
    include('include/db_config.php');
    include('lib/Agent.class.php');
    
    $agent = new Agent();
    $agentData = $agent->getAgentById($_SESSION['agent_id']);
    
    echo "Agent Code: " . $agentData['agent_code'] . "\n";
    echo "Name: " . $agentData['name'] . "\n";
    echo "Balance: Rp " . number_format($agentData['balance'], 0, ',', '.') . "\n";
} else {
    echo "❌ Agent belum login!\n";
}
?>

Jalankan:
php test_agent_session.php

================================================================================

8. CLEAR CACHE & SESSION:
=========================

Browser:
--------
Chrome/Edge: Ctrl + Shift + Delete
Firefox: Ctrl + Shift + Del
Safari: Cmd + Option + E

PHP Session (Manual):
--------------------
Windows:
del /q C:\xampp\tmp\sess_*

Linux/Mac:
rm -f /tmp/sess_*

PHP Session (Script):
--------------------
<?php
session_start();
session_destroy();
echo "✅ Session cleared!\n";
?>

================================================================================

9. RESTART SERVICES:
====================

Windows (XAMPP):
----------------
# Stop
net stop Apache2.4
net stop MySQL

# Start
net start Apache2.4
net start MySQL

# Atau via XAMPP Control Panel
# Klik "Stop" lalu "Start"

Windows (Laragon):
------------------
# Via Laragon GUI
# Klik "Stop All" lalu "Start All"

Linux:
------
sudo systemctl restart apache2
sudo systemctl restart mysql

Mac:
----
brew services restart httpd
brew services restart mysql

================================================================================

10. BACKUP DATABASE:
====================

MySQL Command:
--------------
mysqldump -u root -p mikhmon > backup_mikhmon_$(date +%Y%m%d_%H%M%S).sql

Windows (CMD):
--------------
mysqldump -u root -p mikhmon > backup_mikhmon_%date:~-4,4%%date:~-10,2%%date:~-7,2%.sql

phpMyAdmin:
-----------
1. Buka: http://localhost/phpmyadmin
2. Pilih database: mikhmon
3. Klik tab "Export"
4. Pilih "Quick" atau "Custom"
5. Klik "Go"

================================================================================

11. RESTORE DATABASE:
=====================

MySQL Command:
--------------
mysql -u root -p mikhmon < backup_mikhmon_20251028.sql

phpMyAdmin:
-----------
1. Buka: http://localhost/phpmyadmin
2. Pilih database: mikhmon
3. Klik tab "Import"
4. Pilih file backup
5. Klik "Go"

================================================================================

12. CEK PERMISSIONS:
====================

Windows:
--------
icacls mikhmon\agent\generate.php
icacls mikhmon\lib\routeros_api.class.php

Linux/Mac:
----------
ls -la mikhmon/agent/generate.php
ls -la mikhmon/lib/routeros_api.class.php

# Set permissions jika perlu
chmod 644 mikhmon/agent/generate.php
chmod 644 mikhmon/lib/routeros_api.class.php

================================================================================

13. MONITORING LOG REAL-TIME:
==============================

Windows (PowerShell):
--------------------
Get-Content C:\xampp\apache\logs\error.log -Wait -Tail 50

Linux/Mac:
----------
tail -f /var/log/apache2/error.log
tail -f /var/log/php/error.log

================================================================================

14. QUICK SQL QUERIES:
======================

-- Cek agent aktif
SELECT agent_code, name, balance, status FROM agent_system WHERE status = 'active';

-- Cek harga profile untuk agent tertentu
SELECT profile_name, buy_price, sell_price 
FROM agent_prices 
WHERE agent_id = 1;

-- Cek voucher hari ini
SELECT username, password, profile_name, created_at 
FROM agent_vouchers 
WHERE DATE(created_at) = CURDATE();

-- Cek transaksi hari ini
SELECT type, amount, description, created_at 
FROM agent_transactions 
WHERE DATE(created_at) = CURDATE();

-- Cek total voucher per agent
SELECT a.agent_code, COUNT(v.voucher_id) as total_voucher 
FROM agent_system a 
LEFT JOIN agent_vouchers v ON a.agent_id = v.agent_id 
GROUP BY a.agent_id;

-- Cek total profit per agent
SELECT a.agent_code, 
       SUM(v.sell_price - v.buy_price) as total_profit 
FROM agent_system a 
LEFT JOIN agent_vouchers v ON a.agent_id = v.agent_id 
GROUP BY a.agent_id;

-- Reset saldo agent (HATI-HATI!)
UPDATE agent_system SET balance = 10000 WHERE agent_id = 1;

-- Hapus voucher testing (HATI-HATI!)
DELETE FROM agent_vouchers WHERE DATE(created_at) = CURDATE();

================================================================================

15. BROWSER CONSOLE COMMANDS:
==============================

Buka Browser Console (F12), lalu:

// Cek jQuery loaded
console.log(typeof jQuery);

// Cek form data
console.log($('form').serialize());

// Cek AJAX response
$.ajax({
    url: 'agent/generate.php',
    method: 'POST',
    data: $('form').serialize(),
    success: function(response) {
        console.log(response);
    },
    error: function(xhr, status, error) {
        console.error(error);
    }
});

// Clear localStorage
localStorage.clear();

// Clear sessionStorage
sessionStorage.clear();

================================================================================

16. QUICK FIXES:
================

Fix 1: Fungsi randNULC tidak ditemukan
---------------------------------------
# Cek include
grep "routeros_api.class.php" mikhmon/agent/generate.php

# Jika tidak ada, tambahkan:
include('../lib/routeros_api.class.php');

Fix 2: Session MikroTik tidak ditemukan
---------------------------------------
# Login ulang sebagai admin
# Buka: http://localhost/mikhmon/admin.php

Fix 3: Saldo tidak cukup
------------------------
# Topup saldo via SQL
UPDATE agent_system SET balance = balance + 10000 WHERE agent_id = 1;

Fix 4: Harga profile belum diset
---------------------------------
# Set harga via SQL
INSERT INTO agent_prices (agent_id, profile_name, buy_price, sell_price) 
VALUES (1, '1Jam', 2000, 3000);

Fix 5: Clear semua session
---------------------------
# Via SQL
TRUNCATE TABLE sessions;

# Via PHP
<?php
session_start();
session_destroy();
?>

================================================================================

17. TESTING COMMANDS:
=====================

# Test koneksi MikroTik
php test_mikrotik.php

# Test fungsi random
php test_random.php

# Test agent session
php test_agent_session.php

# Test generate voucher (via browser)
http://localhost/mikhmon/agent/generate.php

# Test admin panel
http://localhost/mikhmon/admin.php

# Test agent panel
http://localhost/mikhmon/agent/

================================================================================

18. USEFUL ALIASES (Optional):
===============================

Windows (PowerShell Profile):
-----------------------------
# Edit: notepad $PROFILE

function mikhmon-log { Get-Content C:\xampp\apache\logs\error.log -Wait -Tail 50 }
function mikhmon-admin { Start-Process "http://localhost/mikhmon/admin.php" }
function mikhmon-agent { Start-Process "http://localhost/mikhmon/agent/" }
function mikhmon-backup { mysqldump -u root -p mikhmon > "backup_$(Get-Date -Format 'yyyyMMdd_HHmmss').sql" }

Linux/Mac (.bashrc or .zshrc):
------------------------------
alias mikhmon-log='tail -f /var/log/apache2/error.log'
alias mikhmon-admin='open http://localhost/mikhmon/admin.php'
alias mikhmon-agent='open http://localhost/mikhmon/agent/'
alias mikhmon-backup='mysqldump -u root -p mikhmon > backup_$(date +%Y%m%d_%H%M%S).sql'

================================================================================

QUICK TROUBLESHOOTING FLOW:
===========================

1. Cek file ada → dir/ls
2. Cek fungsi ada → grep/Select-String
3. Cek database → MySQL/phpMyAdmin
4. Cek error log → tail/Get-Content
5. Cek koneksi MikroTik → test_mikrotik.php
6. Cek fungsi random → test_random.php
7. Clear cache & session
8. Restart services
9. Test lagi

================================================================================

SELAMAT TROUBLESHOOTING! 🔧

================================================================================
