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/widget_catalog_items/widget_catalog_itemsSlice'
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 Widget_catalog_itemsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { widget_catalog_items } = useAppSelector((state) => state.widget_catalog_items)
    
    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 widget_catalog_items')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View widget_catalog_items')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/widget_catalog_items/widget_catalog_items-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>IconName</p>
                    <p>{widget_catalog_items?.icon_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>AuthorName</p>
                    <p>{widget_catalog_items?.author_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>WidgetType</p>
                    <p>{widget_catalog_items?.widget_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Description</p>
                  {widget_catalog_items.description
                    ? <p dangerouslySetInnerHTML={{__html: widget_catalog_items.description}}/>
                    : <p>No data</p>
                  }
                </div>
              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsPublic'>
                    <SwitchField
                      field={{name: 'is_public', value: widget_catalog_items?.is_public}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsAdultOnly'>
                    <SwitchField
                      field={{name: 'is_adult_only', value: widget_catalog_items?.is_adult_only}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>BundleFiles</p>
                    {widget_catalog_items?.bundle_files?.length
                      ? dataFormatter.filesFormatter(widget_catalog_items.bundle_files).map(link => (
                        <button
                          key={link.publicUrl}
                          onClick={(e) => saveFile(e, link.publicUrl, link.name)}
                        >
                            {link.name}
                        </button>
                      )) : <p>No BundleFiles</p>
                    }
                </div>
              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Overlay_widgets CatalogItem</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>InstanceName</th>
                                
                                
                                
                                <th>ConfigJSON</th>
                                
                                
                                
                                <th>X</th>
                                
                                
                                
                                <th>Y</th>
                                
                                
                                
                                <th>Width</th>
                                
                                
                                
                                <th>Height</th>
                                
                                
                                
                                <th>ZIndex</th>
                                
                                
                                
                                <th>IsVisible</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {widget_catalog_items.overlay_widgets_catalog_item && Array.isArray(widget_catalog_items.overlay_widgets_catalog_item) &&
                              widget_catalog_items.overlay_widgets_catalog_item.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/overlay_widgets/overlay_widgets-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="instance_name">
                                        { item.instance_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="config_json">
                                        { item.config_json }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="x">
                                        { item.x }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="y">
                                        { item.y }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="width">
                                        { item.width }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="height">
                                        { item.height }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="z_index">
                                        { item.z_index }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_visible">
                                        { dataFormatter.booleanFormatter(item.is_visible) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!widget_catalog_items?.overlay_widgets_catalog_item?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Widget_catalog_itemsView;