import React, { ReactElement, useEffect } from 'react';
import Head from 'next/head'
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
import dayjs from "dayjs";
import {useAppDispatch, useAppSelector} from "../../stores/hooks";
import {useRouter} from "next/router";
import { fetch } from '../../stores/contacts/contactsSlice'
import {saveFile} from "../../helpers/fileSaver";
import dataFormatter from '../../helpers/dataFormatter';
import ImageField from "../../components/ImageField";
import LayoutAuthenticated from "../../layouts/Authenticated";
import {getPageTitle} from "../../config";
import SectionTitleLineWithButton from "../../components/SectionTitleLineWithButton";
import SectionMain from "../../components/SectionMain";
import CardBox from "../../components/CardBox";
import BaseButton from "../../components/BaseButton";
import BaseDivider from "../../components/BaseDivider";
import {mdiChartTimelineVariant} from "@mdi/js";
import {SwitchField} from "../../components/SwitchField";
import FormField from "../../components/FormField";

import {hasPermission} from "../../helpers/userPermissions";


const ContactsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { contacts } = useAppSelector((state) => state.contacts)
    
    const { currentUser } = useAppSelector((state) => state.auth);
    

    const { id } = router.query;

    useEffect(() => {
        dispatch(fetch({ id }));
    }, [dispatch, id]);


    return (
      <>
          <Head>
              <title>{getPageTitle('Kişi detayı')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Kişi detayı'} main>
                <BaseButton
                  color='info'
                  label='Düzenle'
                  href={`/contacts/contacts-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Ad</p>
                    <p>{contacts?.first_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Soyad</p>
                    <p>{contacts?.last_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>E-posta</p>
                    <p>{contacts?.email}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Telefon</p>
                    <p>{contacts?.phone}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Görev Ünvanı</p>
                    <p>{contacts?.job_title}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Durum</p>
                    <p>{contacts?.status ?? 'Veri yok'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Şirket</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{contacts?.company?.name ?? 'Veri yok'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Sorumlu</p>
                    
                    
                        <p>{contacts?.owner?.firstName ?? 'Veri yok'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Notlar</p>
                  {contacts.notes
                    ? <p dangerouslySetInnerHTML={{__html: contacts.notes}}/>
                    : <p>Veri yok</p>
                  }
                </div>
              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Organizasyon</p>
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{contacts?.organizations?.name ?? 'Veri yok'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Bağlı Potansiyel Müşteriler</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>Potansiyel Müşteri Başlığı</th>
                                
                                
                                
                                <th>Kaynak</th>
                                
                                
                                
                                <th>Durum</th>
                                
                                
                                
                                <th>Tahmini Değer</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>NextFollow-upAt</th>
                                
                                
                                
                                <th>Kapanış Tarihi</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {contacts.leads_primary_contact && Array.isArray(contacts.leads_primary_contact) &&
                              contacts.leads_primary_contact.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/leads/leads-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="Başlık">
                                        { item.title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="Kaynak">
                                        { item.source }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="Durum">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="Tahmini değer">
                                        { item.estimated_value }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="Sonraki takip">
                                        { dataFormatter.dateTimeFormatter(item.next_follow_up_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="Kapanış tarihi">
                                        { dataFormatter.dateTimeFormatter(item.closed_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!contacts?.leads_primary_contact?.length && <div className={'text-center py-4'}>Veri yok</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Bağlı Fırsatlar</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>Fırsat Adı</th>
                                
                                
                                
                                <th>Fırsat Türü</th>
                                
                                
                                
                                <th>Durum</th>
                                
                                
                                
                                <th>Tutar</th>
                                
                                
                                
                                <th>Olasılık</th>
                                
                                
                                
                                <th>Beklenen Kapanış Tarihi</th>
                                
                                
                                
                                <th>Kapanış Tarihi</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {contacts.deals_primary_contact && Array.isArray(contacts.deals_primary_contact) &&
                              contacts.deals_primary_contact.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/deals/deals-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="Ad">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="Fırsat türü">
                                        { item.deal_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="Durum">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="Tutar">
                                        { item.amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="Olasılık">
                                        { item.probability }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="Beklenen kapanış">
                                        { dataFormatter.dateTimeFormatter(item.expected_close_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="Kapanış tarihi">
                                        { dataFormatter.dateTimeFormatter(item.closed_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!contacts?.deals_primary_contact?.length && <div className={'text-center py-4'}>Veri yok</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Bağlı Aktiviteler</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>Aktivite Türü</th>
                                
                                
                                
                                <th>Durum</th>
                                
                                
                                
                                <th>Konu</th>
                                
                                
                                
                                
                                
                                <th>Son Tarih</th>
                                
                                
                                
                                <th>Tamamlanma Tarihi</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {contacts.activities_contact && Array.isArray(contacts.activities_contact) &&
                              contacts.activities_contact.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/activities/activities-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="Aktivite türü">
                                        { item.activity_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="Durum">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="Konu">
                                        { item.subject }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="Son tarih">
                                        { dataFormatter.dateTimeFormatter(item.due_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="Tamamlanma tarihi">
                                        { dataFormatter.dateTimeFormatter(item.completed_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!contacts?.activities_contact?.length && <div className={'text-center py-4'}>Veri yok</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

                <BaseButton
                    color='info'
                    label='Geri'
                    onClick={() => router.push('/contacts/contacts-list')}
                />
              </CardBox>
          </SectionMain>
      </>
    );
};

ContactsView.getLayout = function getLayout(page: ReactElement) {
    return (
      <LayoutAuthenticated
        
        permission={'READ_CONTACTS'}
        
      >
          {page}
      </LayoutAuthenticated>
    )
}

export default ContactsView;