File size: 4,416 Bytes
168704c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

// 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();
}