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/campaigns/campaignsSlice'
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";


const CampaignsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { campaigns } = useAppSelector((state) => state.campaigns)
    

    const { id } = router.query;
    
    function removeLastCharacter(str) {
      console.log(str,`str`)
      return str.slice(0, -1);
    }

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


    return (
      <>
          <Head>
              <title>{getPageTitle('View campaigns')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View campaigns')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/campaigns/campaigns-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Brand</p>
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{campaigns?.brand?.brand_name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>CampaignName</p>
                    <p>{campaigns?.campaign_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>CampaignType</p>
                    <p>{campaigns?.campaign_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Status</p>
                    <p>{campaigns?.status ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              
                <FormField label='Multi Text' hasTextareaHeight>
                  <textarea className={'w-full'} disabled value={campaigns?.objective} />
                </FormField>
              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PrimaryAudience</p>
                    <p>{campaigns?.primary_audience}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>BudgetAmount</p>
                  <p>{campaigns?.budget_amount || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>BudgetCurrency</p>
                    <p>{campaigns?.budget_currency}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              
                <FormField label='StartAt'>
                    {campaigns.start_at ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={campaigns.start_at ?
                        new Date(
                          dayjs(campaigns.start_at).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No StartAt</p>}
                </FormField>
              

              

              

              

              

              

              

              
              

              

              

              

              

              

              
                <FormField label='EndAt'>
                    {campaigns.end_at ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={campaigns.end_at ?
                        new Date(
                          dayjs(campaigns.end_at).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No EndAt</p>}
                </FormField>
              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Six_hat_sessions Campaign</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>SessionName</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>PromptContext</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {campaigns.six_hat_sessions_campaign && Array.isArray(campaigns.six_hat_sessions_campaign) &&
                              campaigns.six_hat_sessions_campaign.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/six_hat_sessions/six_hat_sessions-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="session_name">
                                        { item.session_name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="prompt_context">
                                        { item.prompt_context }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!campaigns?.six_hat_sessions_campaign?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Campaign_brain_configs Campaign</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>LayerName</th>
                                
                                
                                
                                <th>PriorityOrder</th>
                                
                                
                                
                                <th>IsEnabled</th>
                                
                                
                                
                                <th>InfluenceWeight</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {campaigns.campaign_brain_configs_campaign && Array.isArray(campaigns.campaign_brain_configs_campaign) &&
                              campaigns.campaign_brain_configs_campaign.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/campaign_brain_configs/campaign_brain_configs-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="layer_name">
                                        { item.layer_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="priority_order">
                                        { item.priority_order }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_enabled">
                                        { dataFormatter.booleanFormatter(item.is_enabled) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="influence_weight">
                                        { item.influence_weight }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!campaigns?.campaign_brain_configs_campaign?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Ad_projects Campaign</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>ProjectName</th>
                                
                                
                                
                                <th>Channel</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {campaigns.ad_projects_campaign && Array.isArray(campaigns.ad_projects_campaign) &&
                              campaigns.ad_projects_campaign.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/ad_projects/ad_projects-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="project_name">
                                        { item.project_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="channel">
                                        { item.channel }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!campaigns?.ad_projects_campaign?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Learning_signals Campaign</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>SignalType</th>
                                
                                
                                
                                <th>SignalAt</th>
                                
                                
                                
                                <th>SignalTitle</th>
                                
                                
                                
                                
                                
                                <th>ConfidenceScore</th>
                                
                                
                                
                                <th>AppliedToBrain</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {campaigns.learning_signals_campaign && Array.isArray(campaigns.learning_signals_campaign) &&
                              campaigns.learning_signals_campaign.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/learning_signals/learning_signals-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="signal_type">
                                        { item.signal_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="signal_at">
                                        { dataFormatter.dateTimeFormatter(item.signal_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="signal_title">
                                        { item.signal_title }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="confidence_score">
                                        { item.confidence_score }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="applied_to_brain">
                                        { dataFormatter.booleanFormatter(item.applied_to_brain) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!campaigns?.learning_signals_campaign?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                

                <BaseDivider />

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

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

export default CampaignsView;