<?php
/**
 * Debug script to check stock taking related tables
 */

error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once __DIR__ . '/../config/config.php';
require_once __DIR__ . '/../config/database.php';

header('Content-Type: text/html; charset=utf-8');

?>
<!DOCTYPE html>
<html>
<head>
    <title>Check Stock Taking Tables - TellerPro</title>
    <style>
        body { font-family: Arial, sans-serif; max-width: 800px; margin: 50px auto; padding: 20px; background: #f5f5f5; }
        .container { background: white; padding: 30px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
        h1 { color: #333; margin-top: 0; }
        .success { color: #16a34a; font-weight: bold; }
        .error { color: #dc2626; font-weight: bold; }
        .info { color: #2563eb; }
        pre { background: #f3f4f6; padding: 15px; border-radius: 4px; overflow-x: auto; margin: 10px 0; }
        .section { margin: 20px 0; padding: 15px; background: #f9fafb; border-radius: 4px; }
    </style>
</head>
<body>
    <div class="container">
        <h1>Check Stock Taking Tables</h1>
        
        <?php
        $tables = ['stock_takings', 'stock_taking_items', 'locations', 'categories'];
        
        foreach ($tables as $table) {
            echo '<div class="section">';
            echo "<h3>Checking: $table</h3>";
            try {
                $stmt = db()->prepare("SHOW TABLES LIKE '$table'");
                $stmt->execute();
                $result = $stmt->fetch();
                
                if ($result) {
                    echo '<p class="success">✓ Table exists</p>';
                    
                    $stmt = db()->prepare("DESCRIBE $table");
                    $stmt->execute();
                    $columns = $stmt->fetchAll();
                    echo '<p><strong>Columns:</strong></p>';
                    echo '<pre>';
                    foreach ($columns as $col) {
                        echo htmlspecialchars($col['Field'] . ' - ' . $col['Type']) . "\n";
                    }
                    echo '</pre>';
                } else {
                    echo '<p class="error">✗ Table does not exist</p>';
                }
            } catch (Exception $e) {
                echo '<p class="error">✗ Error: ' . htmlspecialchars($e->getMessage()) . '</p>';
            }
            echo '</div>';
        }
        ?>
    </div>
</body>
</html>
