<template>
  <div class="dashboard-header">
    <div class="dashboard-title">{{ title }}</div>

    <div class="dashboard-filters">
      <q-select
        :model-value="selectedPropertyOption"
        :options="propertyOptions"
        class="dashboard-filter dashboard-filter--property"
        dense
        label="Imóveis"
        option-label="label"
        outlined
        @update:model-value="$emit('update:property', $event)"
      />

      <q-select
        :model-value="selectedMonth"
        :options="monthOptions"
        class="dashboard-filter dashboard-filter--month"
        dense
        emit-value
        label="Mês"
        map-options
        option-label="label"
        option-value="value"
        outlined
        @update:model-value="$emit('update:month', $event)"
      >
        <template #append>
          <q-icon name="mdi-calendar-blank" />
        </template>
      </q-select>

      <q-select
        :model-value="selectedYear"
        :options="yearOptions"
        class="dashboard-filter dashboard-filter--year"
        dense
        emit-value
        label="Ano"
        map-options
        option-label="label"
        option-value="value"
        outlined
        @update:model-value="$emit('update:year', $event)"
      />

      <q-btn
        v-if="actionLabel"
        class="dashboard-action-btn"
        color="primary"
        no-caps
        outline
        :label="actionLabel"
        @click="$emit('action')"
      />

      <q-btn-dropdown
        v-if="showExport"
        class="dashboard-export-btn"
        color="primary"
        label="Exportar"
        no-caps
        unelevated
        :disable="loading || exporting || !canExportReport"
        :loading="exporting"
      >
        <q-list>
          <q-item v-close-popup clickable @click="$emit('export', 'xlsx')">
            <q-item-section>
              <q-item-label>XLSX</q-item-label>
            </q-item-section>
          </q-item>

          <q-item v-close-popup clickable @click="$emit('export', 'pdf')">
            <q-item-section>
              <q-item-label>PDF</q-item-label>
            </q-item-section>
          </q-item>
        </q-list>
      </q-btn-dropdown>
    </div>
  </div>
</template>

<script setup>
defineProps({
  canExportReport: {
    type: Boolean,
    default: false,
  },
  exporting: {
    type: Boolean,
    default: false,
  },
  loading: {
    type: Boolean,
    default: false,
  },
  actionLabel: {
    type: String,
    default: "",
  },
  monthOptions: {
    type: Array,
    default: () => [],
  },
  propertyOptions: {
    type: Array,
    default: () => [],
  },
  selectedMonth: {
    type: Number,
    default: null,
  },
  selectedPropertyOption: {
    type: Object,
    default: null,
  },
  selectedYear: {
    type: Number,
    default: null,
  },
  showExport: {
    type: Boolean,
    default: true,
  },
  title: {
    type: String,
    default: "Dashboard",
  },
  yearOptions: {
    type: Array,
    default: () => [],
  },
});

defineEmits([
  "action",
  "export",
  "update:month",
  "update:property",
  "update:year",
]);
</script>

<style scoped lang="scss">
.dashboard-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  min-width: 0;
  vertical-align: middle;
}

.dashboard-title {
  font-size: 28px;
  font-weight: 700;
  color: #202427;
  line-height: 1.1;
}

.dashboard-filters {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
  flex-wrap: wrap;
  margin-left: auto;
  min-width: 0;
  vertical-align: middle;
}

.dashboard-filter {
  min-width: 120px;
  max-width: 100%;
}

.dashboard-filter--property {
  min-width: 220px;
}

.dashboard-filter--month,
.dashboard-filter--year {
  min-width: 110px;
}

.dashboard-action-btn,
.dashboard-export-btn {
  height: 40px;
  vertical-align: middle;
}

@media (max-width: 640px) {
  .dashboard-header {
    gap: 12px;
  }

  .dashboard-title {
    font-size: 24px;
  }

  .dashboard-filters {
    width: 100%;
    margin-left: 0;
    justify-content: stretch;
  }

  .dashboard-filter,
  .dashboard-filter--property,
  .dashboard-filter--month,
  .dashboard-filter--year {
    width: 100%;
    min-width: 100%;
  }

  .dashboard-action-btn,
  .dashboard-export-btn {
    width: 100%;
    max-width: 100%;
  }
}
</style>
