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/music_playlists/music_playlistsSlice'
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 Music_playlistsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { music_playlists } = useAppSelector((state) => state.music_playlists)
    

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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PlaylistType</p>
                    <p>{music_playlists?.playlist_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='AutoActivate'>
                    <SwitchField
                      field={{name: 'auto_activate', value: music_playlists?.auto_activate}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

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

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Music_tracks Playlist</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>TrackName</th>
                                
                                
                                
                                
                                
                                <th>Duration(Seconds)</th>
                                
                                
                                
                                <th>OrderIndex</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {music_playlists.music_tracks_playlist && Array.isArray(music_playlists.music_tracks_playlist) &&
                              music_playlists.music_tracks_playlist.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/music_tracks/music_tracks-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="duration_seconds">
                                        { item.duration_seconds }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="order_index">
                                        { item.order_index }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!music_playlists?.music_tracks_playlist?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Music_playlistsView;