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

    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 distribution_platforms')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View distribution_platforms')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/distribution_platforms/distribution_platforms-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Platformname</p>
                    <p>{distribution_platforms?.name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Category</p>
                    <p>{distribution_platforms?.category ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>WebsiteURL</p>
                    <p>{distribution_platforms?.website_url}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Logo</p>
                    {distribution_platforms?.logo?.length
                      ? (
                        <ImageField
                          name={'logo'}
                          image={distribution_platforms?.logo}
                          className='w-20 h-20'
                        />
                      ) : <p>No Logo</p>
                    }
                </div>
              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='Isactive'>
                    <SwitchField
                      field={{name: 'is_active', value: distribution_platforms?.is_active}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>organizations</p>
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{distribution_platforms?.organizations?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Release_deliveries Platform</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Deliverystatus</th>
                                
                                
                                
                                <th>Requestedat</th>
                                
                                
                                
                                <th>Sentat</th>
                                
                                
                                
                                <th>Deliveredat</th>
                                
                                
                                
                                <th>PlatformreleaseURL</th>
                                
                                
                                
                                <th>Externalreference</th>
                                
                                
                                
                                <th>Errormessage</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {distribution_platforms.release_deliveries_platform && Array.isArray(distribution_platforms.release_deliveries_platform) &&
                              distribution_platforms.release_deliveries_platform.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/release_deliveries/release_deliveries-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="delivery_status">
                                        { item.delivery_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="requested_at">
                                        { dataFormatter.dateTimeFormatter(item.requested_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sent_at">
                                        { dataFormatter.dateTimeFormatter(item.sent_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="delivered_at">
                                        { dataFormatter.dateTimeFormatter(item.delivered_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="platform_release_url">
                                        { item.platform_release_url }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="external_reference">
                                        { item.external_reference }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="error_message">
                                        { item.error_message }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!distribution_platforms?.release_deliveries_platform?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Smart_link_destinations Platform</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>DestinationURL</th>
                                
                                
                                
                                <th>Sortorder</th>
                                
                                
                                
                                <th>Isprimary</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {distribution_platforms.smart_link_destinations_platform && Array.isArray(distribution_platforms.smart_link_destinations_platform) &&
                              distribution_platforms.smart_link_destinations_platform.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/smart_link_destinations/smart_link_destinations-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="destination_url">
                                        { item.destination_url }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sort_order">
                                        { item.sort_order }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_primary">
                                        { dataFormatter.booleanFormatter(item.is_primary) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!distribution_platforms?.smart_link_destinations_platform?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Smart_link_clicks Platform</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Clickedat</th>
                                
                                
                                
                                <th>Country</th>
                                
                                
                                
                                <th>City</th>
                                
                                
                                
                                <th>Referrer</th>
                                
                                
                                
                                <th>UTMsource</th>
                                
                                
                                
                                <th>UTMcampaign</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {distribution_platforms.smart_link_clicks_platform && Array.isArray(distribution_platforms.smart_link_clicks_platform) &&
                              distribution_platforms.smart_link_clicks_platform.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/smart_link_clicks/smart_link_clicks-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="clicked_at">
                                        { dataFormatter.dateTimeFormatter(item.clicked_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="country">
                                        { item.country }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="city">
                                        { item.city }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="referrer">
                                        { item.referrer }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="utm_source">
                                        { item.utm_source }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="utm_campaign">
                                        { item.utm_campaign }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!distribution_platforms?.smart_link_clicks_platform?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Playlists Platform</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>Playlistname</th>
                                
                                
                                
                                
                                
                                <th>Playlisttype</th>
                                
                                
                                
                                <th>Curatorname</th>
                                
                                
                                
                                <th>PlaylistURL</th>
                                
                                
                                
                                <th>Genrefocus</th>
                                
                                
                                
                                <th>Moodtags</th>
                                
                                
                                
                                <th>Isactive</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {distribution_platforms.playlists_platform && Array.isArray(distribution_platforms.playlists_platform) &&
                              distribution_platforms.playlists_platform.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/playlists/playlists-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="playlist_type">
                                        { item.playlist_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="curator_name">
                                        { item.curator_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="playlist_url">
                                        { item.playlist_url }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="genre_focus">
                                        { item.genre_focus }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="mood_tags">
                                        { item.mood_tags }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_active">
                                        { dataFormatter.booleanFormatter(item.is_active) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!distribution_platforms?.playlists_platform?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Royalty_reports Platform</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Periodstartat</th>
                                
                                
                                
                                <th>Periodendat</th>
                                
                                
                                
                                <th>Sourcetype</th>
                                
                                
                                
                                
                                
                                <th>Sourcereference</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>Errordetails</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {distribution_platforms.royalty_reports_platform && Array.isArray(distribution_platforms.royalty_reports_platform) &&
                              distribution_platforms.royalty_reports_platform.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/royalty_reports/royalty_reports-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="period_start_at">
                                        { dataFormatter.dateTimeFormatter(item.period_start_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="period_end_at">
                                        { dataFormatter.dateTimeFormatter(item.period_end_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="source_type">
                                        { item.source_type }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="source_reference">
                                        { item.source_reference }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="error_details">
                                        { item.error_details }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!distribution_platforms?.royalty_reports_platform?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Analytics_daily Platform</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Metricdate</th>
                                
                                
                                
                                <th>Streams</th>
                                
                                
                                
                                <th>Uniquelisteners</th>
                                
                                
                                
                                <th>Saves</th>
                                
                                
                                
                                <th>Likes</th>
                                
                                
                                
                                <th>Shares</th>
                                
                                
                                
                                <th>Country</th>
                                
                                
                                
                                <th>City</th>
                                
                                
                                
                                <th>Trafficsource</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {distribution_platforms.analytics_daily_platform && Array.isArray(distribution_platforms.analytics_daily_platform) &&
                              distribution_platforms.analytics_daily_platform.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/analytics_daily/analytics_daily-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="metric_date">
                                        { dataFormatter.dateTimeFormatter(item.metric_date) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="streams">
                                        { item.streams }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="unique_listeners">
                                        { item.unique_listeners }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="saves">
                                        { item.saves }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="likes">
                                        { item.likes }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="shares">
                                        { item.shares }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="country">
                                        { item.country }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="city">
                                        { item.city }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="traffic_source">
                                        { item.traffic_source }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!distribution_platforms?.analytics_daily_platform?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Distribution_platformsView;