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/stones/stonesSlice'
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 StonesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { stones } = useAppSelector((state) => state.stones)
    

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

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>NameArabic</p>
                    <p>{stones?.name_ar}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Slug</p>
                    <p>{stones?.slug}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Color</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{stones?.color?.name_ar ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              
                <>
                    <p className={'block font-bold mb-2'}>Finishes</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                    <th>NameArabic</th>
                                
                                
                                
                                    <th>DescriptionArabic</th>
                                
                                
                                
                                
                                
                                    <th>IsActive</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {stones.finishes && Array.isArray(stones.finishes) &&
                              stones.finishes.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/finishes/finishes-view/?id=${item.id}`)}>
                                  
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                  
                                    
                                    <td data-label="name_ar">
                                        { item.name_ar }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="description_ar">
                                        { item.description_ar }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="is_active">
                                        { dataFormatter.booleanFormatter(item.is_active) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!stones?.finishes?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
              

              

              
              

              

              

              

              

              

              

              

              

              

              

              
                <>
                    <p className={'block font-bold mb-2'}>SuggestedUses</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                    <th>NameArabic</th>
                                
                                
                                
                                    <th>ApplicationScope</th>
                                
                                
                                
                                    <th>DescriptionArabic</th>
                                
                                
                                
                                    <th>IsActive</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {stones.applications && Array.isArray(stones.applications) &&
                              stones.applications.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/applications/applications-view/?id=${item.id}`)}>
                                  
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                  
                                    
                                    <td data-label="name_ar">
                                        { item.name_ar }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="application_scope">
                                        { item.application_scope }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="description_ar">
                                        { item.description_ar }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_active">
                                        { dataFormatter.booleanFormatter(item.is_active) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!stones?.applications?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

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

              
              

              

              

              

              

              

              

              

              

              

              

              

              

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

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Density</p>
                  <p>{stones?.density || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>WaterAbsorption</p>
                  <p>{stones?.water_absorption || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>CompressiveStrength</p>
                  <p>{stones?.compressive_strength || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>FlexuralStrength</p>
                  <p>{stones?.flexural_strength || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>OriginType</p>
                    <p>{stones?.origin_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>OriginCountry</p>
                    <p>{stones?.origin_country}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsFeatured'>
                    <SwitchField
                      field={{name: 'is_featured', value: stones?.is_featured}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Stone_variants Stone</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>VariantType</th>
                                
                                
                                
                                <th>SKU</th>
                                
                                
                                
                                <th>Lengthmm</th>
                                
                                
                                
                                <th>Widthmm</th>
                                
                                
                                
                                <th>Thicknessmm</th>
                                
                                
                                
                                <th>Weightkg</th>
                                
                                
                                
                                <th>AvailabilityStatus</th>
                                
                                
                                
                                <th>BasePrice</th>
                                
                                
                                
                                <th>PriceNoteArabic</th>
                                
                                
                                
                                
                                
                                <th>IsActive</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {stones.stone_variants_stone && Array.isArray(stones.stone_variants_stone) &&
                              stones.stone_variants_stone.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/stone_variants/stone_variants-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="variant_type">
                                        { item.variant_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sku">
                                        { item.sku }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="length_mm">
                                        { item.length_mm }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="width_mm">
                                        { item.width_mm }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="thickness_mm">
                                        { item.thickness_mm }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="weight_kg">
                                        { item.weight_kg }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="availability_status">
                                        { item.availability_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="base_price">
                                        { item.base_price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="price_note_ar">
                                        { item.price_note_ar }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="is_active">
                                        { dataFormatter.booleanFormatter(item.is_active) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!stones?.stone_variants_stone?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Stone_3d_assets Stone</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>AssetType</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>ViewerHintArabic</th>
                                
                                
                                
                                <th>IsActive</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {stones.stone_3d_assets_stone && Array.isArray(stones.stone_3d_assets_stone) &&
                              stones.stone_3d_assets_stone.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/stone_3d_assets/stone_3d_assets-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="asset_type">
                                        { item.asset_type }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="viewer_hint_ar">
                                        { item.viewer_hint_ar }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_active">
                                        { dataFormatter.booleanFormatter(item.is_active) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!stones?.stone_3d_assets_stone?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Lead_requests InterestedStone</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>RequestType</th>
                                
                                
                                
                                <th>FullName</th>
                                
                                
                                
                                <th>CompanyName</th>
                                
                                
                                
                                <th>Email</th>
                                
                                
                                
                                <th>Phone</th>
                                
                                
                                
                                <th>CustomerType</th>
                                
                                
                                
                                <th>MessageArabic</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>LeadStatus</th>
                                
                                
                                
                                
                                
                                <th>PreferredContactTime</th>
                                
                                
                                
                                <th>LastContactedAt</th>
                                
                                
                                
                                <th>InternalNotes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {stones.lead_requests_interested_stone && Array.isArray(stones.lead_requests_interested_stone) &&
                              stones.lead_requests_interested_stone.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/lead_requests/lead_requests-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="request_type">
                                        { item.request_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="full_name">
                                        { item.full_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="company_name">
                                        { item.company_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="email">
                                        { item.email }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="phone">
                                        { item.phone }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="customer_type">
                                        { item.customer_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="message_ar">
                                        { item.message_ar }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="lead_status">
                                        { item.lead_status }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="preferred_contact_time">
                                        { dataFormatter.dateTimeFormatter(item.preferred_contact_time) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="last_contacted_at">
                                        { dataFormatter.dateTimeFormatter(item.last_contacted_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="internal_notes">
                                        { item.internal_notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!stones?.lead_requests_interested_stone?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                

                <BaseDivider />

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

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

export default StonesView;