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

              

              

              

              

              

              

              

              

              

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

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Classification</p>
                    <p>{ore_bodies?.classification ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>OreType</p>
                    <p>{ore_bodies?.ore_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>HeadGradeAu(g/t)</p>
                  <p>{ore_bodies?.head_grade_au_gpt || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Tonnage(Mt)</p>
                  <p>{ore_bodies?.tonnage_mt || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Samples OreBody</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>SampleCode</th>
                                
                                
                                
                                <th>SampleType</th>
                                
                                
                                
                                <th>DrillholeID</th>
                                
                                
                                
                                <th>DepthFrom(m)</th>
                                
                                
                                
                                <th>DepthTo(m)</th>
                                
                                
                                
                                <th>CollectionDate</th>
                                
                                
                                
                                <th>Lithology</th>
                                
                                
                                
                                <th>Alteration</th>
                                
                                
                                
                                <th>GeometDomain</th>
                                
                                
                                
                                
                                
                                <th>PrepProtocol</th>
                                
                                
                                
                                <th>QAQCType</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {ore_bodies.samples_ore_body && Array.isArray(ore_bodies.samples_ore_body) &&
                              ore_bodies.samples_ore_body.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/samples/samples-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="sample_code">
                                        { item.sample_code }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sample_type">
                                        { item.sample_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="drillhole_id">
                                        { item.drillhole_id }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="depth_from_m">
                                        { item.depth_from_m }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="depth_to_m">
                                        { item.depth_to_m }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="collection_date">
                                        { dataFormatter.dateTimeFormatter(item.collection_date) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="lithology">
                                        { item.lithology }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="alteration">
                                        { item.alteration }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="geomet_domain">
                                        { item.geomet_domain }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="prep_protocol">
                                        { item.prep_protocol }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="qaqc_type">
                                        { item.qaqc_type }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!ore_bodies?.samples_ore_body?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Ore_bodiesView;