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

              

              

              

              

              

              

              

              

              

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

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>AppCategory</p>
                    <p>{marketplace_apps?.app_category ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Runtime</p>
                    <p>{marketplace_apps?.runtime ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Version</p>
                    <p>{marketplace_apps?.version}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>WASMArtifactURI</p>
                    <p>{marketplace_apps?.wasm_artifact_uri}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>ReliabilityScore</p>
                  <p>{marketplace_apps?.reliability_score || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>VettingStatus</p>
                    <p>{marketplace_apps?.vetting_status ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>App_installations MarketplaceApp</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>InstallationStatus</th>
                                
                                
                                
                                <th>InstalledAt</th>
                                
                                
                                
                                <th>DisabledAt</th>
                                
                                
                                
                                <th>DisableReason</th>
                                
                                
                                
                                <th>AutoSuggestEnabled</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {marketplace_apps.app_installations_marketplace_app && Array.isArray(marketplace_apps.app_installations_marketplace_app) &&
                              marketplace_apps.app_installations_marketplace_app.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/app_installations/app_installations-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="installation_status">
                                        { item.installation_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="installed_at">
                                        { dataFormatter.dateTimeFormatter(item.installed_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="disabled_at">
                                        { dataFormatter.dateTimeFormatter(item.disabled_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="disable_reason">
                                        { item.disable_reason }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="auto_suggest_enabled">
                                        { dataFormatter.booleanFormatter(item.auto_suggest_enabled) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!marketplace_apps?.app_installations_marketplace_app?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>App_crash_reports MarketplaceApp</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Severity</th>
                                
                                
                                
                                <th>ClientPlatform</th>
                                
                                
                                
                                <th>ClientVersion</th>
                                
                                
                                
                                <th>ReportedAt</th>
                                
                                
                                
                                <th>Stacktrace</th>
                                
                                
                                
                                <th>ContextPayload</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {marketplace_apps.app_crash_reports_marketplace_app && Array.isArray(marketplace_apps.app_crash_reports_marketplace_app) &&
                              marketplace_apps.app_crash_reports_marketplace_app.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/app_crash_reports/app_crash_reports-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="severity">
                                        { item.severity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="client_platform">
                                        { item.client_platform }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="client_version">
                                        { item.client_version }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reported_at">
                                        { dataFormatter.dateTimeFormatter(item.reported_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="stacktrace">
                                        { item.stacktrace }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="context_payload">
                                        { item.context_payload }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!marketplace_apps?.app_crash_reports_marketplace_app?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Marketplace_appsView;