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/coin_codes/coin_codesSlice'
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 Coin_codesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { coin_codes } = useAppSelector((state) => state.coin_codes)
    

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

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Code</p>
                    <p>{coin_codes?.code}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PIN</p>
                    <p>{coin_codes?.pin}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>AmountDoodlecoins</p>
                  <p>{coin_codes?.amount_doodlecoins || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>MaxRedemptions</p>
                  <p>{coin_codes?.max_redemptions || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='Active'>
                    <SwitchField
                      field={{name: 'active', value: coin_codes?.active}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Coin_code_redemptions CoinCode</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>RedeemedAt</th>
                                
                                
                                
                                <th>AmountDoodlecoins</th>
                                
                                
                                
                                <th>StatusMessage</th>
                                
                                
                                
                                <th>Successful</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {coin_codes.coin_code_redemptions_coin_code && Array.isArray(coin_codes.coin_code_redemptions_coin_code) &&
                              coin_codes.coin_code_redemptions_coin_code.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/coin_code_redemptions/coin_code_redemptions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="redeemed_at">
                                        { dataFormatter.dateTimeFormatter(item.redeemed_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="amount_doodlecoins">
                                        { item.amount_doodlecoins }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status_message">
                                        { item.status_message }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="successful">
                                        { dataFormatter.booleanFormatter(item.successful) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!coin_codes?.coin_code_redemptions_coin_code?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Coin_codesView;