<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manage Profile</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <link rel="stylesheet" href="css/main/main.css?ver=<?php echo time(); ?>">
    <script src="js/main/main.js?ver=<?php echo time(); ?>" defer></script>
    <style>
        /* General form styles */
        #content {
            max-width: 1000px;
            margin: 0 auto;
            padding: 20px;
            background-color: ;
            border-radius: 10px;
        }
        
        /* Form title */
        #content h2 {
            font-size: 28px;
            color: #333;
            text-align: center;
            margin-bottom: 20px;
            font-weight: 600;
        }
        
        /* Grid layout for the form */
        .form-grid {
            display: grid;
            grid-template-columns: repeat(2, 1fr); /* Default to 2 columns */
            gap: 20px;
        }
        
        /* Styling for form groups */
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-group label {
            font-size: 16px;
            color: #333;
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
        }
        
        /* Input fields and textareas */
        .form-group input[type="text"],
        .form-group input[type="email"],
        .form-group input[type="tel"],
        .form-group input[type="number"],
        .form-group input[type="url"],
        .form-group textarea {
            width: 100%;
            padding: 10px;
            font-size: 14px;
            color: #333;
            border: 1px solid #ddd;
            border-radius: 5px;
            box-sizing: border-box;
            transition: border 0.3s ease;
        }
        
        /* Input focus effect */
        .form-group input[type="text"]:focus,
        .form-group input[type="email"]:focus,
        .form-group input[type="tel"]:focus,
        .form-group input[type="number"]:focus,
        .form-group input[type="url"]:focus,
        .form-group textarea:focus {
            border-color: #096171; /* Greenish color for focus */
            outline: none;
        }
        
        /* Textarea specific styling */
        .form-group textarea {
            resize: vertical;
            min-height: 120px;
        }
        
        /* File input field (Company Logo) */
        .form-group input[type="file"] {
            width: 100%;
            padding: 8px;
            font-size: 14px;
            color: #333;
            border: 1px solid #ddd;
            border-radius: 5px;
            cursor: pointer;
        }
        
        /* Button styling */
        .submit-btn {
            background-color: #4CAF50; /* Greenish color */
            color: #fff;
            padding: 12px 25px;
            font-size: 16px;
            font-weight: bold;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s ease;
            width: 100%;
            text-align: center;
        }
        
        /* Button hover effect */
        .submit-btn:hover {
            background-color: #064f56; /* Darker green shade */
        }
        
        /* Responsive design for mobile */
        @media screen and (max-width: 1024px) {
            .form-grid {
                grid-template-columns: repeat(2, 1fr); /* 2 columns for tablets */
            }
        }
        
        @media screen and (max-width: 768px) {
            .form-grid {
                grid-template-columns: 1fr; /* 1 column for mobile */
                padding: 0 10px; /* Add padding to mobile view */
            }
            
            .submit-btn {
                font-size: 14px;
                padding: 10px 20px;
            }
        }
        
        @media screen and (max-width: 480px) {
            .form-grid {
                grid-template-columns: 1fr; /* Single column layout for very small screens */
            }
            
            .submit-btn {
                font-size: 12px;  /* Smaller button text */
            }
        }

    
    </style>
</head>
<body>

    <!-- Left Panel (Navigation) -->
    <div class="left-panel left-panel-show" id="left-panel">
    </div>

    <!-- Main Content Area -->
    <div class="main-content main-content-normal" id="main-content">

        <div id="content">
            
            <form id="companyForm" enctype="multipart/form-data">
                <div class="form-group">
                    <label for="companyLogo">Company Logo:</label>
                    <input type="file" id="companyLogo" name="companyLogo" accept="image/*">
                </div>
                
                <div class="form-group">
                    <label for="companyStamp">Sign/Stamp:</label>
                    <input type="file" id="companyStamp" name="companyStamp" accept="image/*">
                </div>
            
                <div class="form-group">
                    <label for="companyName">Company Name:</label>
                    <input type="text" id="companyName" name="companyName" required>
                </div>
            
                <div class="form-group">
                    <label for="email">Email:</label>
                    <input type="email" id="email" name="email" required>
                </div>
            
                <div class="form-group">
                    <label for="phone">Phone (with country code):</label>
                    <input type="tel" id="phone" name="phone" placeholder="+1 234 567 890" required>
                </div>
            
                <div class="form-group">
                    <label for="whatsapp">WhatsApp Number (with Country Code):</label>
                    <input type="tel" id="whatsapp" name="whatsapp" placeholder="+1 234 567 890" required>
                </div>
            
                <div class="form-group">
                    <label for="billingAddress">Billing Address:</label>
                    <textarea id="billingAddress" name="billingAddress" rows="4" required></textarea>
                </div>
            
                <div class="form-group">
                    <label for="shippingAddress">Shipping Address:</label>
                    <textarea id="shippingAddress" name="shippingAddress" rows="4" required></textarea>
                </div>
            
                <div class="form-group">
                    <label for="gstNumber">GST Number:</label>
                    <input type="text" id="gstNumber" name="gstNumber" placeholder="Enter GST Number" required>
                </div>
            
                <div class="form-group">
                    <label for="paymentTerms">Payment Terms (Days):</label>
                    <input type="number" id="paymentTerms" name="paymentTerms" min="1" required>
                </div>
            
                <div class="form-group">
                    <label for="termsAndPolicy">Terms and Policy:</label>
                    <textarea id="termsAndPolicy" name="termsAndPolicy" rows="6" required></textarea>
                </div>
            
                <div class="form-group">
                    <label for="facebook">Facebook:</label>
                    <input type="url" id="facebook" name="facebook">
                </div>
            
                <div class="form-group">
                    <label for="twitter">Twitter:</label>
                    <input type="url" id="twitter" name="twitter">
                </div>
            
                <div class="form-group">
                    <label for="instagram">Instagram:</label>
                    <input type="url" id="instagram" name="instagram">
                </div>
            
                <div class="form-group">
                    <label for="youtube">YouTube:</label>
                    <input type="url" id="youtube" name="youtube">
                </div>
            
                <div class="form-group">
                    <button type="button" class="submit-btn" onclick="submitForm()">Submit</button>
                </div>
            </form>


        </div>
        
    </div>

    <script>
    
function submitForm() {
    var formData = new FormData(document.getElementById("companyForm"));

    // Show SweetAlert with built-in spinner and icon instead of a GIF
    Swal.fire({
        title: '<i class="fas fa-spinner fa-spin" style="margin-right: 10px;"></i> Processing...',
        html: 'Please wait while we save your data.',
        showConfirmButton: false,
        allowOutsideClick: false,
        didOpen: () => {
            Swal.showLoading();  // Optional: keep this to show SweetAlert's native spinner
        }
    });

    var xhr = new XMLHttpRequest();
    xhr.open("POST", "php/settings/save_profile.php", true);
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
            var response = JSON.parse(xhr.responseText);

            Swal.close();  // Close the loading alert

            if (response.status === 'success') {
                Swal.fire({
                    icon: 'success',
                    title: 'Success!',
                    text: response.message,
                    confirmButtonText: 'Ok',
                    confirmButtonColor: '#3085d6',
                });
                loadCompanyProfile();
            } else {
                Swal.fire({
                    icon: 'error',
                    title: 'Error!',
                    text: response.message,
                    confirmButtonText: 'Try Again',
                    confirmButtonColor: '#d33',
                });
            }
        }
    };

    xhr.send(formData);
}



function loadCompanyProfile() {
    fetch('php/settings/fetch_profile_data.php')
        .then(response => response.json())
        .then(data => {
            document.getElementById('companyName').value = data.companyName || '';
            document.getElementById('email').value = data.email || '';
            document.getElementById('phone').value = data.phone || '';
            document.getElementById('whatsapp').value = data.whatsapp || '';
            document.getElementById('billingAddress').value = data.billingAddress || '';
            document.getElementById('shippingAddress').value = data.shippingAddress || '';
            document.getElementById('gstNumber').value = data.gstNumber || '';
            document.getElementById('paymentTerms').value = data.paymentTerms || '';
            document.getElementById('termsAndPolicy').value = data.termsAndPolicy || '';
            document.getElementById('facebook').value = data.facebook || '';
            document.getElementById('twitter').value = data.twitter || '';
            document.getElementById('instagram').value = data.instagram || '';
            document.getElementById('youtube').value = data.youtube || '';

            // Display existing company logo if available
            if (data.companyLogo) {
                const existingLogo = document.getElementById('companyLogoPreview');
                if (!existingLogo) {
                    const logoPreview = document.createElement('img');
                    logoPreview.id = 'companyLogoPreview';
                    logoPreview.src = data.companyLogo.replace(/\\/g, '');
                    logoPreview.alt = 'Company Logo';
                    logoPreview.style.maxWidth = '150px';
                    logoPreview.style.marginTop = '10px';
                    document.getElementById('companyLogo').insertAdjacentElement('afterend', logoPreview);
                } else {
                    existingLogo.src = data.companyLogo.replace(/\\/g, '');
                }
            }

            // ✅ Display company stamp if available
            if (data.companyStamp) {
                const existingStamp = document.getElementById('companyStampPreview');
                if (!existingStamp) {
                    const stampPreview = document.createElement('img');
                    stampPreview.id = 'companyStampPreview';
                    stampPreview.src = data.companyStamp.replace(/\\/g, '');
                    stampPreview.alt = 'Company Stamp';
                    stampPreview.style.maxWidth = '150px';
                    stampPreview.style.marginTop = '10px';
                    document.getElementById('companyStamp').insertAdjacentElement('afterend', stampPreview);
                } else {
                    existingStamp.src = data.companyStamp.replace(/\\/g, '');
                }
            }
        })
        .catch(error => {
            console.error('Error loading profile data:', error);
        });
}

document.addEventListener('DOMContentLoaded', loadCompanyProfile);



    </script>

</body>
</html>