<template>
  <q-page class="opportunities-page">
    <div class="page-header">
      <q-btn flat round dense icon="chevron_left" class="back-btn" @click="router.back()" />
      <div class="page-title">
        {{ $t('provider.dashboard.opportunities.title') }}
      </div>
    </div>

    <q-card flat class="info-banner">
      <q-icon name="mdi-auto-fix" size="22px" />
      <div class="banner-text">
        {{ $t('provider.dashboard.opportunities.banner_text') }}
      </div>
    </q-card>

    <div v-if="loading" class="flex flex-center q-pa-lg">
      <q-spinner-dots color="secondary" size="32px" />
    </div>

    <div v-else-if="!opportunities.length" class="text-center q-pa-md text-grey">
      {{ $t('provider.dashboard.opportunities.empty') }}
    </div>

    <div v-else class="opportunity-list">
      <q-card v-for="item in opportunities" :key="item.id" flat class="opportunity-card">
        <div class="avatar-column">
          <img :src="item.customer_photo || 'https://cdn.quasar.dev/img/avatar.png'" alt="Avatar" class="client-avatar" />
          <div class="service-type">
            {{ item.custom_schedule?.service_type.description }}
          </div>
        </div>

        <div class="center-content">
          <div class="client-name-row">
            <span class="client-name">{{ item.client?.user.name }}</span>

            <!-- campo de avaliação -->
            <span class="rating">
              <q-icon name="star" size="11px" />
              {{ item.client?.average_rating }}
            </span>
          </div>

          <div class="service-date">
            {{ formatDate(item.date) }}
          </div>

          <div class="service-hour">
           {{ `Das ${formatHour(item.start_time)} às ${formatHour(item.end_time)}` }}
          </div>
        </div>

        <div class="right-content">
          <div class="price">
             {{ filterCurrency(chooseprice(item.period_type, user.user.provider.daily_price_8h)) }}
          </div>

          <div class="service-address">
            {{ item.address?.district }}
          </div>


          <div class="distance">
            {{ $t('provider.dashboard.opportunities.distance_km', { distance: item.distance }) }}
          </div>

          

          <q-btn unelevated rounded no-caps color="secondary" :label="$t('provider.dashboard.opportunities.details')" class="details-btn" @click="goToOpportunityDetails(item)" />
        </div>
      </q-card>
    </div>
  </q-page>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { useQuasar } from 'quasar'
import { chooseprice, filterCurrency } from 'src/helpers/utils'
import { getProviderOpportunities } from 'src/api/opportunities'
import { userStore } from 'src/stores/user'
import OpportunityDetailsDialog from './components/OpportunityDetailsDialog.vue'

const $q = useQuasar()

const user = userStore()

const opportunities = ref([])
const loading = ref(false)

const goToOpportunityDetails = (item) => {

  const id = item.custom_schedule?.id || item.id

  $q.dialog({
    component: OpportunityDetailsDialog,
    componentProps: {
      opportunityId: id
    }
  })
}

// formatando a data
const formatDate = (date) => {
  if (!date) return ''

  return new Date(date).toLocaleDateString('pt-BR', {
    weekday: 'long',
    day: '2-digit',
    month: '2-digit'
  })
}

// formatando hora para exibir só HH:mm
const formatHour = (time) => {
  if (!time) return ''
  return time.slice(0, 5)
}


const loadOpportunities = async () => {
  loading.value = true

  try {
    const response = await getProviderOpportunities(
      user.user.provider.id
    )

    opportunities.value = (response || [])

  } catch (error) {
    console.error('Erro ao buscar oportunidades:', error)

    opportunities.value = []

  } finally {
    loading.value = false
  }
}

onMounted(loadOpportunities)
</script>

<style scoped lang="scss">
.opportunities-page {
  padding: 0;
  background: #f7f7fb;
  min-height: 100vh;
}

.page-header {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;

  padding: 14px 16px;
  background: #fff;

  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  border-radius: 0 0 16px 16px;

  margin-bottom: 16px;
  z-index: 10;
}

.back-btn {
  position: absolute;
  left: 8px;
  color: #7c5cff;
}

.page-title {
  font-size: 16px;
  font-weight: 600;
  color: #7c5cff;
}

.info-banner {
  display: flex;
  gap: 12px;
  padding: 14px;
  border-radius: 14px;
  background: #a78bfa;
  color: white;
  margin: 16px;
}

.banner-text {
  font-size: 12px;
  line-height: 1.4;
}

.opportunity-list {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 0 16px 16px;
}

.opportunity-card {
  display: flex;
  gap: 14px;
  padding: 14px;
  border-radius: 18px;
  background: #fff;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.06);
  align-items: flex-start;
}

.avatar-column {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 58px;
}

.client-avatar {
  width: 54px;
  height: 54px;
  border-radius: 50%;
  object-fit: cover;
}

.service-type {
  margin-top: 8px;
  font-size: 11px;
  color: #7c5cff;
  font-weight: 500;
  text-align: center;
}

.center-content {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.client-name-row {
  display: flex;
  align-items: center;
  gap: 4px;
}

.client-name {
  font-size: 15px;
  font-weight: 600;
  color: #2d2d2d;
}

.district {
  margin-top: 4px;
  font-size: 11px;
  color: #666;
}

.rating {
  display: flex;
  align-items: center;
  gap: 2px;
  font-size: 11px;
  color: #ffb800;
}

.service-date,
.service-hour {
  font-size: 11px;
  color: #666;
  line-height: 1.35;
}

.right-content {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  min-width: 110px;
}

.price {
  font-size: 22px;
  font-weight: 700;
  color: #2d2d2d;
}

.service-address {
  margin-top: 6px;
  font-size: 11px;
  color: #666;
  text-align: right;
  line-height: 1.3;
  max-width: 120px;
}

.distance {
  margin-top: 4px;
  font-size: 11px;
  color: #999;
}

.details-btn {
  margin-top: 10px;
  min-height: 30px;
  padding: 0 18px;
  font-size: 11px;
  font-weight: 600;
}
</style>