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/leads/leadsSlice'
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 LeadsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { leads } = useAppSelector((state) => state.leads)
    
    const { currentUser } = useAppSelector((state) => state.auth);
    

    const { id } = router.query;

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


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

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Potansiyel Müşteri Başlığı</p>
                    <p>{leads?.title}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Kaynak</p>
                    <p>{leads?.source ?? 'Veri yok'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Tahmini Değer</p>
                  <p>{leads?.estimated_value || 'Veri yok'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Açıklama</p>
                  {leads.description
                    ? <p dangerouslySetInnerHTML={{__html: leads.description}}/>
                    : <p>Veri yok</p>
                  }
                </div>
              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Ana Yetkili</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{leads?.primary_contact?.first_name ?? 'Veri yok'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Satış Hattı</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{leads?.pipeline?.name ?? 'Veri yok'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Aşama</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{leads?.stage?.name ?? 'Veri yok'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              
                <FormField label='Sonraki Takip Tarihi'>
                    {leads.next_follow_up_at ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={leads.next_follow_up_at ?
                        new Date(
                          dayjs(leads.next_follow_up_at).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>Sonraki takip tarihi yok</p>}
                </FormField>
              

              

              

              

              

              

              

              
              

              

              

              

              

              

              
                <FormField label='Kapanış Tarihi'>
                    {leads.closed_at ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={leads.closed_at ?
                        new Date(
                          dayjs(leads.closed_at).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>Kapanış tarihi yok</p>}
                </FormField>
              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>İlgili 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>
                            {leads.deals_lead && Array.isArray(leads.deals_lead) &&
                              leads.deals_lead.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>
                        {!leads?.deals_lead?.length && <div className={'text-center py-4'}>Veri yok</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>İlgili 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>
                            {leads.activities_lead && Array.isArray(leads.activities_lead) &&
                              leads.activities_lead.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>
                        {!leads?.activities_lead?.length && <div className={'text-center py-4'}>Veri yok</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default LeadsView;