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

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>EndpointName</p>
                    <p>{webhook_endpoints?.endpoint_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>URL</p>
                    <p>{webhook_endpoints?.url}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Secret</p>
                    <p>{webhook_endpoints?.secret}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsEnabled'>
                    <SwitchField
                      field={{name: 'is_enabled', value: webhook_endpoints?.is_enabled}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Webhook_deliveries Endpoint</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>EventType</th>
                                
                                
                                
                                <th>EventReference</th>
                                
                                
                                
                                <th>DeliveredAt</th>
                                
                                
                                
                                <th>DeliveryStatus</th>
                                
                                
                                
                                <th>HTTPStatus</th>
                                
                                
                                
                                <th>RequestPayload</th>
                                
                                
                                
                                <th>ResponseBody</th>
                                
                                
                                
                                <th>AttemptCount</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {webhook_endpoints.webhook_deliveries_endpoint && Array.isArray(webhook_endpoints.webhook_deliveries_endpoint) &&
                              webhook_endpoints.webhook_deliveries_endpoint.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/webhook_deliveries/webhook_deliveries-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="event_type">
                                        { item.event_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="event_reference">
                                        { item.event_reference }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="delivered_at">
                                        { dataFormatter.dateTimeFormatter(item.delivered_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="delivery_status">
                                        { item.delivery_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="http_status">
                                        { item.http_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="request_payload">
                                        { item.request_payload }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="response_body">
                                        { item.response_body }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="attempt_count">
                                        { item.attempt_count }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!webhook_endpoints?.webhook_deliveries_endpoint?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                

                <BaseDivider />

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

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

export default Webhook_endpointsView;