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/users/usersSlice'
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 UsersView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { users } = useAppSelector((state) => state.users)

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

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>First Name</p>
                    <p>{users?.firstName}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Last Name</p>
                    <p>{users?.lastName}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Phone Number</p>
                    <p>{users?.phoneNumber}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>E-Mail</p>
                    <p>{users?.email}</p>
                </div>

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

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

                        <p>{users?.app_role?.name ?? 'No data'}</p>

                </div>

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

                                <th>Name</th>

                                <th>Status</th>

                                <th>Campaign Number</th>

                                <th>Campaign Name</th>

                                <th>Scheduled Completion Date</th>

                                <th>Billing Status</th>

                                <th>Total Cost</th>

                                <th>Total Invoiced</th>

                                <th>Uninvoiced</th>

                                <th>Remaining Invoice %</th>

                                <th>Start Date</th>

                                <th>Country</th>

                                <th>Created Date</th>

                                <th>Last Updated Date</th>

                                <th>Job Number Count</th>

                                <th>Count of Left for Invoice</th>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                                <th>Name</th>

                                <th>Status</th>

                                <th>Campaign Number</th>

                                <th>Campaign Name</th>

                                <th>Scheduled Completion Date</th>

                                <th>Billing Status</th>

                                <th>Total Cost</th>

                                <th>Total Invoiced</th>

                                <th>Uninvoiced</th>

                                <th>Remaining Invoice %</th>

                                <th>Start Date</th>

                                <th>Country</th>

                                <th>Created Date</th>

                                <th>Last Updated Date</th>

                                <th>Job Number Count</th>

                                <th>Count of Left for Invoice</th>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                                <th>Name</th>

                                <th>Status</th>

                                <th>Campaign Number</th>

                                <th>Campaign Name</th>

                                <th>Scheduled Completion Date</th>

                                <th>Billing Status</th>

                                <th>Total Cost</th>

                                <th>Total Invoiced</th>

                                <th>Uninvoiced</th>

                                <th>Remaining Invoice %</th>

                                <th>Start Date</th>

                                <th>Country</th>

                                <th>Created Date</th>

                                <th>Last Updated Date</th>

                                <th>Job Number Count</th>

                                <th>Count of Left for Invoice</th>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                                <th>Title</th>

                                <th>Visibility</th>

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

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

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

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

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

                                <th>Content</th>

                                <th>CommentedAt</th>

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

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

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

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

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

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

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

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

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

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

                <BaseDivider />

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

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

export default UsersView;
