<template>
  <AdminLayout>
    <Breadcrumb current-page-text="Kullanıcılar Listesi" go="/"/>
    <section class="section-list">
      <list-table-content
        v-if="loaded"
        :tableHeader="tableHeader"
        :rowAction="rwAction"
        icon="customers"
        title="Kullanıcılar"
        listText="Kullanıcı"
        addRoute="yeni-kullanici"
        apiList="Auth/kullaniciList"
        apiText="Kullanıcı Listesi" 
        isUseRoute/>
    </section>
  </AdminLayout>
</template>
<script setup lang="ts">
  import { ref, onBeforeMount, computed } from 'vue'
  import AdminLayout from '@/layouts/AdminLayout.vue'
  import { useGlobalStore } from '@/stores/globalStore'
  const globalStore = useGlobalStore()
  import { useAppUserUnitStore } from '../stores/appUserUnitStore'
  const appUserUnitStore = useAppUserUnitStore()
  import { useAppUserRoleStore } from '../stores/appUserRoleStore'
  const appUserRoleStore = useAppUserRoleStore()
  import { useAppUserUnitService } from '../service/appUserUnitService'
  const appUserUnitService = useAppUserUnitService()
  import { useAppUserRoleService } from '../service/appUserRoleService'
  const appUserRoleService = useAppUserRoleService()
  import router from '@/router'

  const loaded = ref<boolean>(false)

  const userUnitList = computed<Record<string, any>[]>(() => {
    return appUserUnitStore.appUserUnits
  })

  const userRoleList = computed<Record<string, any>[]>(() => {
    return appUserRoleStore.appUserRoles
  })

  const tableHeader = ref<Record<string, any>[]>([
    {
      name: 'ad',
      title: 'Ad',
      sort: true
    },
    {
      name: 'soyad',
      title: 'Soyad',
      sort: true
    },
    {
      name: 'calistigiBirim',
      title: 'Çalıştığı Birim',
      sort: true,
      filter: {
        type: 'select',
        data: userUnitList,
        listVal: 'id',
        listText: 'birimAdi',
        filterId: 'birimId'
      }
    },
    {
      name: 'kullaniciGorevi',
      title: 'Görevi',
      sort: true,
      filter: {
        type: 'select',
        data: userRoleList,
        listVal: 'id',
        listText: 'gorevAdi',
        filterId: 'kullaniciGorevId'
      }
    },
    {
      name: 'email',
      title: 'E-posta',
      sort: true
    },
    {
      name: 'telefonNumarasi',
      title: 'Telefon',
      sort: true
    }
  ])
  const rwAction = (row: any) => {
    globalStore.selUser = row.id
    router.push('detay/' + row.id+'/kullanici-bilgileri')
  }

  onBeforeMount(async () => {
    await appUserUnitService.GetAppUserUnitList()
    await appUserRoleService.GetAppUserRoleList()

    loaded.value = true
  })
</script>
