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/shareholders/shareholdersSlice'
import dataFormatter from '../../helpers/dataFormatter';
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 ShareholdersView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { shareholders } = useAppSelector((state) => state.shareholders)

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

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>NameEN</p>
                    <p>{shareholders?.name_en}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>NameKH</p>
                    <p>{shareholders?.name_kh}</p>
                </div>

                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>EarnRate</p>
                  <p>{shareholders?.earn_rate || 'No data'}</p>
                </div>

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

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PhoneNumber</p>
                    <p>{shareholders?.phone_number}</p>
                </div>

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

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>BornPlace</p>
                    <p>{shareholders?.born_place}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>DocumentType</p>
                    <p>{shareholders?.document_type}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>DocumentNumber</p>
                    <p>{shareholders?.document_number}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>EmergencyNumber</p>
                    <p>{shareholders?.emergency_number}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>CurrentPlace</p>
                    <p>{shareholders?.current_place}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Sex</p>
                    <p>{shareholders?.sex ?? 'No data'}</p>
                </div>

                <>
                    <p className={'block font-bold mb-2'}>Deposits Shareholder</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>

                                <th>DepositDatetime</th>

                                <th>DepositAmount</th>

                            </tr>
                            </thead>
                            <tbody>
                            {shareholders.deposits_shareholder && Array.isArray(shareholders.deposits_shareholder) &&
                              shareholders.deposits_shareholder.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/deposits/deposits-view/?id=${item.id}`)}>

                                    <td data-label="deposit_datetime">
                                        { dataFormatter.dateTimeFormatter(item.deposit_datetime) }
                                    </td>

                                    <td data-label="deposit_amount">
                                        { item.deposit_amount }
                                    </td>

                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!shareholders?.deposits_shareholder?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>

                <BaseDivider />

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

ShareholdersView.getLayout = function getLayout(page: ReactElement) {
    return (
      <LayoutAuthenticated>
          {page}
      </LayoutAuthenticated>
    )
}

export default ShareholdersView;
