<!-- src/pages/StatementHeaderReport.vue -->
<template>
  <q-page v-if="canReadFinance" class="q-pa-md page-col">
    <!-- Başlık ve PDF butonu -->
    <div class="row justify-between items-center q-mb-md">
      <div class="text-h6">📄 Cari Hesap Raporu</div>
      <q-btn
        v-if="canExportFinance"
        color="red"
        icon="picture_as_pdf"
        label="PDF Yazdır"
        push
        glossy
        @click="handlestHeadDownload"
      />
    </div>

    <q-separator spaced />

    <!-- Cari ve tarih seçim alanı -->
    <q-card flat bordered class="q-pa-md q-mt-md">
      <div class="row q-col-gutter-md">
        <div class="col-12 col-sm-4">
          <q-input v-model="accountCode" label="Cari Kod" filled dense clearable />
        </div>
        <div class="col-12 col-sm-4">
          <q-input v-model="startDate" label="Başlangıç Tarihi" filled dense />
        </div>
        <div class="col-12 col-sm-4">
          <q-input v-model="endDate" label="Bitiş Tarihi" filled dense />
        </div>
      </div>

      <div class="row q-mt-md">
        <div class="col-12">
          <q-select
            v-model="selectedMonType"
            :options="monetaryTypeOptions"
            label="Parasal İşlem Tipi"
            emit-value
            map-options
            filled
          />
        </div>
      </div>
    </q-card>
  </q-page>

  <q-page v-else class="q-pa-md flex flex-center">
    <div class="text-negative text-subtitle1">
      Bu module erisim yetkiniz yok.
    </div>
  </q-page>
</template>

<script setup>
import { ref } from 'vue'
import { useQuasar } from 'quasar'
import { useDownloadstHeadStore } from 'src/stores/downloadstHeadStore'
import dayjs from 'dayjs'
import { usePermission } from 'src/composables/usePermission'

const { canRead, canExport } = usePermission()
const canReadFinance = canRead('finance')
const canExportFinance = canExport('finance')

const $q = useQuasar()
const downloadstHeadStore = useDownloadstHeadStore()

// form değerleri
const accountCode = ref('')
const startDate = ref(dayjs().startOf('month').format('YYYY-MM-DD'))
const endDate   = ref(dayjs().format('YYYY-MM-DD'))

// parasal işlem tipleri
const monetaryTypeOptions = [
  { label: '1-2 hesap', value: ['1', '2'] },
  { label: '1-3 hesap', value: ['1', '3'] }
]
const selectedMonType = ref(monetaryTypeOptions[0].value)

// indirme butonu
async function handlestHeadDownload() {
  if (!canExportFinance.value) {
    $q.notify({
      type: 'negative',
      message: 'PDF export yetkiniz yok',
      position: 'top-right'
    })
    return
  }

  console.log("▶️ [DEBUG] handlestHeadDownload:", accountCode.value, startDate.value, endDate.value, selectedMonType.value)

  if (!accountCode.value || !startDate.value || !endDate.value) {
    $q.notify({
      type: 'warning',
      message: '⚠️ Cari ve tarih seçmeden PDF alınamaz!',
      position: 'top-right'
    })
    return
  }

  const result = await downloadstHeadStore.handlestHeadDownload(
    accountCode.value,
    startDate.value,
    endDate.value,
    selectedMonType.value
  )

  $q.notify({
    type: result.ok ? 'positive' : 'negative',
    message: result.message,
    position: 'top-right'
  })
}
</script>
