// TELEGRAM BOT SOZLAMALARI
const TELEGRAM_BOT_TOKEN = 'ВАШ_ТОКЕН_БОТА'; 
const TELEGRAM_CHAT_ID = 'ВАШ_CHAT_ID';       

// Servis kartochkasini tanlash
const providerCards = document.querySelectorAll('.provider-card');
providerCards.forEach(card => {
  card.addEventListener('click', () => {
    providerCards.forEach(c => c.classList.remove('selected'));
    card.classList.add('selected');
    card.querySelector('input[type="radio"]').checked = true;
  });
});

// Arizani Telegramga yuborish
const leadForm = document.getElementById('leadForm');
const submitBtn = document.getElementById('submitBtn');
const btnText = document.getElementById('btnText');
const btnLoader = document.getElementById('btnLoader');
const successScreen = document.getElementById('successScreen');

leadForm.addEventListener('submit', async (e) => {
  e.preventDefault();

  const provider = document.querySelector('input[name="provider"]:checked').value;
  const limitSum = document.getElementById('limitSum').value.trim();
  const name = document.getElementById('clientName').value.trim();
  const phone = document.getElementById('clientPhone').value.trim();
  const city = document.getElementById('clientCity').value;

  // Telegram uchun xabar matni
  const message = `🔥 <b>YANGI BUYURTMA (INSTAGRAM)</b>\n\n` +
                  `🏦 <b>Fintex:</b> ${provider}\n` +
                  `💳 <b>Limit summasi:</b> ${limitSum}\n` +
                  `👤 <b>Mijoz ismi:</b> ${name}\n` +
                  `📞 <b>Telefon raqami:</b> ${phone}\n` +
                  `📍 <b>Shahar / Viloyat:</b> ${city}\n\n` +
                  `⚡ <i>Mijoz bilan zudlik bilan bog‘laning!</i>`;

  submitBtn.disabled = true;
  btnText.style.display = 'none';
  btnLoader.style.display = 'block';

  try {
    const response = await fetch(`https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        chat_id: TELEGRAM_CHAT_ID,
        text: message,
        parse_mode: 'HTML'
      })
    });

    const result = await response.json();

    if (result.ok) {
      leadForm.style.display = 'none';
      successScreen.style.display = 'block';
    } else {
      alert('Xatolik yuz berdi. Bot sozlamalarini tekshiring.');
      submitBtn.disabled = false;
      btnText.style.display = 'block';
      btnLoader.style.display = 'none';
    }
  } catch (error) {
    console.error('Xatolik:', error);
    alert('Internet bilan bog‘liq xatolik yuz berdi.');
    submitBtn.disabled = false;
    btnText.style.display = 'block';
    btnLoader.style.display = 'none';
  }
});