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/districts/districtsSlice'
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 DistrictsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { districts } = useAppSelector((state) => state.districts)

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

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

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

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Province</p>

                        <p>{districts?.province?.name_en ?? 'No data'}</p>

                </div>

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

                                <th>Code</th>

                                <th>NameEN</th>

                                <th>NameKH</th>

                                <th>DateofBirth</th>

                                <th>PhoneNumber</th>

                                <th>IsNew</th>

                                <th>DocumentNumber</th>

                                <th>Sex</th>

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

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

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

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

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

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

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

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

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

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

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

                                <th>NameKH</th>

                                <th>NameEN</th>

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

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

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

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

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

                                <th>FullName</th>

                                <th>Sex</th>

                                <th>DateofBirth</th>

                                <th>DocumentType</th>

                                <th>DocumentNumber</th>

                                <th>PhoneNumber</th>

                                <th>FullAddressInput</th>

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

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

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

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

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

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

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

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

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

                <BaseDivider />

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

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

export default DistrictsView;
