<template>
  <div
    class="form-inner-comment waiting-d"
    v-if="customerIrtibatStore.totalIrtibat === 0">
    <template v-if="usersStore.isPanelUser">
      En az 1 irtibat kişisi eklenmiş olmalıdır.
    </template>
    <template v-else>En az 1 adet irtabita kurulacak kişi eklemelisiniz.</template>
  </div>
  <list-table-content
    :tableHeader="tableHeader"
    :rowAction="EditIrtibat"
    :addAction="AddActionFunction()"
    formTitle="İrtibat Kurulacak Kişiler"
    v-model:totalRecord="customerIrtibatStore.totalIrtibat"
    listText="İrtibat Kurulacak Kişi"
    :apiList="
      'AppUserContactPerson/GetContactPersonAppUserId/' + usersStore.selectedUserId()
    "
    apiText="İrtibat Kurulacak Kişi Listesi"
    page="form"
    v-model:refresh="customerIrtibatStore.refreshList" />
  <panel-wrapper
    v-if="customerIrtibatStore.irtibatPanel"
    v-model="customerIrtibatStore.irtibatPanel"
    :panel-title="
      customerIrtibatStore.isUpdate ? 'İrtibat Kişisini Düzenle' : 'İrtibat Kişisi Ekle'
    ">
    <template #panelContent>
      <panel-user-contact />
    </template>
    <template #footerButton>
      <div class="button-c button-save" @click="customerIrtibatService.SaveIrtibat">
        {{ customerIrtibatStore.isUpdate ? 'Kaydet' : 'Ekle' }}
      </div>
    </template>
  </panel-wrapper>
</template>
<script setup lang="ts">
  import { ref } from 'vue'

  import PanelWrapper from '@/components/PanelWrapper.vue'
  import PanelUserContact from '../panel/PanelUserContact.vue'

  import { useDataStore } from '@/stores/dataStore'
  const dataStore = useDataStore()
  import { useUsersStore } from '@/stores/usersStore'
  const usersStore = useUsersStore()
  import { useValidationStore } from '@/stores/validationStore'
  const validationStore = useValidationStore()

  import { useCustomerIrtibatStore } from '../../stores/customerIrtibatStore'
  const customerIrtibatStore = useCustomerIrtibatStore()
  import { useCustomerIrtibatService } from '../../service/customerIrtibatService'
  const customerIrtibatService = useCustomerIrtibatService()

  const dataPenel = ref<boolean>(false)
  const isUpdate = ref<boolean>(false)
  const refreshList = ref<boolean>(false)
  const totalRecords = ref<number | string>(0)

  const tableHeader = ref<Record<string, any>>([
    {
      name: 'firstName',
      title: 'Adı',
      sort: true,
      style: { width: '25%' }
    },
    {
      name: 'lastName',
      title: 'Soyadı',
      sort: true,
      style: { width: '25%' }
    },
    {
      name: 'email',
      title: 'E-posta',
      sort: true,
      style: { width: '25%' }
    },
    {
      name: 'phone',
      title: 'Telefon Numarası',
      sort: true
    }
  ])

  const AddActionFunction = (): any => {
    if (
      usersStore.isPanelUser ||
      usersStore.selectedUserApproveId === 5 ||
      usersStore.selectedUserApproveId === 1 ||
      usersStore.selectedUserApproveId === 2
    ) {
      return ''
    } else {
      return NewIrtibat as Function
    }
  }
  const NewIrtibat = () => {
    customerIrtibatStore.ResetForm()
    customerIrtibatStore.irtibatPanel = true
  }

  const EditIrtibat = (data: any) => {
    Object.assign(customerIrtibatStore.customerIrtibatForm, data)
    customerIrtibatStore.isUpdate = true
    customerIrtibatStore.irtibatPanel = true
  }
</script>
