<?php
session_start();

// Your Shopify App Credentials
$client_id = 'f8dd1e89905a9a919fc369b4085ad49d';
$scopes = 'write_products,read_products';
$redirect_uri = 'https://development.hatinco.com/vehicle-car-details/v2/upload_product.php';
$shopify_access_token = '';
?>
<!DOCTYPE html>
<html>
<head>
    <title>Connect to Shopify</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 500px;
            margin: 100px auto;
            padding: 20px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .container {
            background: white;
            border-radius: 15px;
            padding: 40px;
            box-shadow: 0 20px 60px rgba(0,0,0,0.3);
            width: 100%;
        }
        h1 {
            color: #96bf48;
            text-align: center;
            margin-bottom: 30px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #333;
        }
        input[type="text"] {
            width: 100%;
            padding: 12px;
            border: 2px solid #ddd;
            border-radius: 8px;
            font-size: 16px;
            transition: border-color 0.3s;
        }
        input[type="text"]:focus {
            outline: none;
            border-color: #96bf48;
        }
        .btn {
            background: #96bf48;
            color: white;
            border: none;
            padding: 14px;
            width: 100%;
            border-radius: 8px;
            font-size: 16px;
            font-weight: bold;
            cursor: pointer;
            transition: background 0.3s;
            margin-top: 10px;
        }
        .btn:hover {
            background: #7fa53d;
        }
        .connected {
            background: #d4edda;
            color: #155724;
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
            border-left: 4px solid #155724;
        }
        .info {
            background: #e8f5e9;
            padding: 15px;
            border-radius: 8px;
            margin-top: 20px;
            font-size: 14px;
        }
        .disconnect-btn {
            background: #dc3545;
            color: white;
            border: none;
            padding: 8px 15px;
            border-radius: 5px;
            text-decoration: none;
            display: inline-block;
            margin-top: 10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Connect Shopify Store</h1>
        
        <?php if (isset($_SESSION['shopify_access_token'])): $shopify_access_token = $_SESSION['shopify_access_token']?>
            <div class="connected">
                ✅ Already connected to: <strong><?php echo htmlspecialchars($_SESSION['shopify_shop']); ?></strong>
                <p>You can now upload products to your Shopify store.</p>
                <a href="upload.php" class="btn" style="margin-top: 15px;">Go to Product Upload</a>
                <a href="?disconnect=1" class="disconnect-btn">Disconnect</a>
            </div>
        <?php endif; ?>
        <input type="hidden" value="<?php echo $shopify_access_token;?>" id="shopify_access_token">
        
        <form method="POST" action="">
            <div class="form-group">
                <label for="shop">Shopify Store Domain</label>
                <input type="text" 
                       id="shop" 
                       name="shop" 
                       placeholder="car-touchup-paint.myshopify.com"
                       value="car-touchup-paint.myshopify.com"
                       required>
                <small>Enter your Shopify store domain</small>
            </div>
            
            <button type="submit" class="btn">Connect to Shopify</button>
        </form>
        
        <div class="info">
            <p><strong>What happens next?</strong></p>
            <ol>
                <li>You'll be redirected to Shopify</li>
                <li>Authorize the app to access your store</li>
                <li>You'll be redirected back to receive an access token</li>
                <li>You can then upload products to your store</li>
            </ol>
        </div>
        
        <?php
        // Handle disconnect
        if (isset($_GET['disconnect'])) {
            session_destroy();
            header("Location: index.php");
            exit;
        }
        
        // Handle form submission
        if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['shop'])) {
            $shop = trim($_POST['shop']);
            
            // Validate shop domain
            if (!preg_match('/^[a-zA-Z0-9\-]+\.myshopify\.com$/', $shop)) {
                echo "<script>alert('Please enter a valid Shopify domain (e.g., mystore.myshopify.com)');</script>";
                exit;
            }
            
            // Build OAuth URL
            $auth_url = "https://$shop/admin/oauth/authorize?" . http_build_query([
                'client_id' => $client_id,
                'scope' => $scopes,
                'redirect_uri' => $redirect_uri,
                'state' => md5(time() . rand())
            ]);
            
            // Redirect to Shopify for authorization
            header("Location: $auth_url");
            exit;
        }
        ?>
    </div>
</body>
</html>