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

    const { id } = router.query;

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


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

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Fırsat Adı</p>
                    <p>{deals?.name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Fırsat Türü</p>
                    <p>{deals?.deal_type ?? 'Veri yok'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Tutar</p>
                  <p>{deals?.amount || 'Veri yok'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Olasılık</p>
                  <p>{deals?.probability || 'Veri yok'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              
                <FormField label='Kapanış Tarihi'>
                    {deals.closed_at ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={deals.closed_at ?
                        new Date(
                          dayjs(deals.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'}>Şirket</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{deals?.company?.name ?? 'Veri yok'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Potansiyel Müşteri</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{deals?.lead?.title ?? 'Veri yok'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Fırsata 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>
                            {deals.activities_deal && Array.isArray(deals.activities_deal) &&
                              deals.activities_deal.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>
                        {!deals?.activities_deal?.length && <div className={'text-center py-4'}>Veri yok</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Fırsata Bağlı Üretim Emirleri</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>İş Emri No</th>
                                
                                
                                
                                <th>Durum</th>
                                
                                
                                
                                <th>Planlanan Başlangıç</th>
                                
                                
                                
                                <th>Planlanan Bitiş</th>
                                
                                
                                
                                <th>Gerçek Başlangıç</th>
                                
                                
                                
                                <th>Gerçek Bitiş</th>
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {deals.production_orders_deal && Array.isArray(deals.production_orders_deal) &&
                              deals.production_orders_deal.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/production_orders/production_orders-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="İş emri no">
                                        { item.order_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="Durum">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="Planlanan başlangıç">
                                        { dataFormatter.dateTimeFormatter(item.planned_start_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="Planlanan bitiş">
                                        { dataFormatter.dateTimeFormatter(item.planned_end_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="Gerçek başlangıç">
                                        { dataFormatter.dateTimeFormatter(item.actual_start_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="Gerçek bitiş">
                                        { dataFormatter.dateTimeFormatter(item.actual_end_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!deals?.production_orders_deal?.length && <div className={'text-center py-4'}>Veri yok</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default DealsView;