agriconnect-bazaar / script.js
Aramaaaa's picture
این فقط ظاهر برنامه هست یک تندش رو بسازید
168704c verified
Raw History Blame Contribute Delete
4.42 kB
// Initialize animations
document.addEventListener('DOMContentLoaded', function() {
// Animate elements with data-animate attribute
const animatedElements = document.querySelectorAll('[data-animate]');
animatedElements.forEach((element, index) => {
const animationType = element.getAttribute('data-animate');
element.classList.add(animationType);
element.style.animationDelay = `${index * 0.1}s`;
});
// Mobile menu toggle functionality will be handled by navbar component
});
// API functions
async function fetchFeaturedProducts() {
try {
// Using mock API for demo
const mockProducts = [
{
id: 1,
name: "گوجه فرنگی",
price: 15000,
farmer: "محمد رضایی",
location: "اصفهان",
image: "http://static.photos/agriculture/320x240/1"
},
{
id: 2,
name: "خیار",
price: 12000,
farmer: "علی محمدی",
location: "تهران",
image: "http://static.photos/agriculture/320x240/2"
},
{
id: 3,
name: "سیب",
price: 25000,
farmer: "حسن کریمی",
location: "شیراز",
image: "http://static.photos/agriculture/320x240/3"
}
];
return mockProducts;
} catch (error) {
console.error('Error fetching featured products:', error);
return [];
}
}
// Form validation
function validatePhoneNumber(phone) {
const regex = /^09[0-9]{9}$/;
return regex.test(phone);
}
// Utility functions
function formatPrice(price) {
return new Intl.NumberFormat('fa-IR').format(price) + ' تومان';
}
// Register form submissions
document.addEventListener('DOMContentLoaded', function() {
// Farmer registration form
const farmerForm = document.getElementById('farmerForm');
if (farmerForm) {
farmerForm.addEventListener('submit', function(e) {
e.preventDefault();
if (validatePhoneNumber(this.phone.value)) {
// In a real app, you would send data to server here
alert('ثبت‌نام کشاورز با موفقیت انجام شد!');
this.reset();
} else {
alert('لطفا شماره تماس معتبر وارد کنید (09xxxxxxxxx)');
}
});
}
// Buyer registration form
const buyerForm = document.getElementById('buyerForm');
if (buyerForm) {
buyerForm.addEventListener('submit', function(e) {
e.preventDefault();
if (validatePhoneNumber(this.phone.value)) {
// In a real app, you would send data to server here
alert('ثبت‌نام خریدار با موفقیت انجام شد!');
this.reset();
} else {
alert('لطفا شماره تماس معتبر وارد کنید (09xxxxxxxxx)');
}
});
}
});
// Load featured products on homepage
async function loadFeaturedProducts() {
const products = await fetchFeaturedProducts();
const container = document.getElementById('featuredProducts');
if (container && products.length > 0) {
container.innerHTML = products.map(product => `
<div class="bg-white rounded-xl shadow-md overflow-hidden">
<img src="${product.image}" alt="${product.name}" class="w-full h-48 object-cover">
<div class="p-4">
<h3 class="font-bold text-lg mb-2">${product.name}</h3>
<p class="text-gray-600 mb-2">کشاورز: ${product.farmer}</p>
<p class="text-gray-600 mb-2">موقعیت: ${product.location}</p>
<p class="text-primary font-bold">${formatPrice(product.price)}</p>
<button class="mt-4 w-full bg-primary hover:bg-green-600 text-white py-2 px-4 rounded-lg transition">
مشاهده جزئیات
</button>
</div>
</div>
`).join('');
}
}
// Initialize on homepage
if (document.getElementById('featuredProducts')) {
loadFeaturedProducts();
}