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 {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 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'}>Avatar</p>
                    {users?.avatar?.length
                      ? (
                        <ImageField
                          name={'avatar'}
                          image={users?.avatar}
                          className='w-20 h-20'
                        />
                      ) : <p>No Avatar</p>
                    }
                </div>
              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <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'}>Custom Permissions</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                    <th>Name</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.custom_permissions && Array.isArray(users.custom_permissions) &&
                              users.custom_permissions.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/permissions/permissions-view/?id=${item.id}`)}>
                                  
                                    
                                    
                                  
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.custom_permissions?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
              

              

              
              

                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Accounts Assignedsalesrep</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>Accountname</th>
                                
                                
                                
                                <th>Accounttype</th>
                                
                                
                                
                                <th>Accountnumber</th>
                                
                                
                                
                                <th>Taxexemptnumber</th>
                                
                                
                                
                                <th>Taxexempt</th>
                                
                                
                                
                                <th>Creditstatus</th>
                                
                                
                                
                                <th>Creditlimit</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>Notes</th>
                                
                                
                                
                                <th>Active</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.accounts_assigned_sales_rep && Array.isArray(users.accounts_assigned_sales_rep) &&
                              users.accounts_assigned_sales_rep.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/accounts/accounts-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="account_name">
                                        { item.account_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="account_type">
                                        { item.account_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="account_number">
                                        { item.account_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="tax_exempt_number">
                                        { item.tax_exempt_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_tax_exempt">
                                        { dataFormatter.booleanFormatter(item.is_tax_exempt) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="credit_status">
                                        { item.credit_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="credit_limit">
                                        { item.credit_limit }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_active">
                                        { dataFormatter.booleanFormatter(item.is_active) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.accounts_assigned_sales_rep?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Carts Buyer</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Cartstatus</th>
                                
                                
                                
                                <th>POnumber</th>
                                
                                
                                
                                <th>Requesteddeliverydate</th>
                                
                                
                                
                                <th>Buyernotes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.carts_buyer && Array.isArray(users.carts_buyer) &&
                              users.carts_buyer.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/carts/carts-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="cart_status">
                                        { item.cart_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="po_number">
                                        { item.po_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="requested_delivery_date">
                                        { dataFormatter.dateTimeFormatter(item.requested_delivery_date) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="buyer_notes">
                                        { item.buyer_notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.carts_buyer?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Orders Buyer</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>Ordernumber</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>POnumber</th>
                                
                                
                                
                                <th>Orderedat</th>
                                
                                
                                
                                <th>Requesteddeliverydate</th>
                                
                                
                                
                                <th>Promiseddeliverydate</th>
                                
                                
                                
                                <th>Orderstatus</th>
                                
                                
                                
                                <th>Paymentterms</th>
                                
                                
                                
                                <th>Subtotal</th>
                                
                                
                                
                                <th>Taxtotal</th>
                                
                                
                                
                                <th>Shippingtotal</th>
                                
                                
                                
                                <th>Ordertotal</th>
                                
                                
                                
                                <th>Buyernotes</th>
                                
                                
                                
                                <th>Internalnotes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.orders_buyer && Array.isArray(users.orders_buyer) &&
                              users.orders_buyer.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/orders/orders-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="order_number">
                                        { item.order_number }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="po_number">
                                        { item.po_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ordered_at">
                                        { dataFormatter.dateTimeFormatter(item.ordered_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="requested_delivery_date">
                                        { dataFormatter.dateTimeFormatter(item.requested_delivery_date) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="promised_delivery_date">
                                        { dataFormatter.dateTimeFormatter(item.promised_delivery_date) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="order_status">
                                        { item.order_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="payment_terms">
                                        { item.payment_terms }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="subtotal">
                                        { item.subtotal }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="tax_total">
                                        { item.tax_total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="shipping_total">
                                        { item.shipping_total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="order_total">
                                        { item.order_total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="buyer_notes">
                                        { item.buyer_notes }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="internal_notes">
                                        { item.internal_notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.orders_buyer?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Quotes Requestedby</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>Quotenumber</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Requestedat</th>
                                
                                
                                
                                <th>Expiresat</th>
                                
                                
                                
                                <th>Quotestatus</th>
                                
                                
                                
                                <th>Quotetotal</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.quotes_requested_by && Array.isArray(users.quotes_requested_by) &&
                              users.quotes_requested_by.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/quotes/quotes-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="quote_number">
                                        { item.quote_number }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="requested_at">
                                        { dataFormatter.dateTimeFormatter(item.requested_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="expires_at">
                                        { dataFormatter.dateTimeFormatter(item.expires_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="quote_status">
                                        { item.quote_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="quote_total">
                                        { item.quote_total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.quotes_requested_by?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                <>
                    <p className={'block font-bold mb-2'}>Quotes Owner</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>Quotenumber</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Requestedat</th>
                                
                                
                                
                                <th>Expiresat</th>
                                
                                
                                
                                <th>Quotestatus</th>
                                
                                
                                
                                <th>Quotetotal</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.quotes_owner && Array.isArray(users.quotes_owner) &&
                              users.quotes_owner.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/quotes/quotes-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="quote_number">
                                        { item.quote_number }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="requested_at">
                                        { dataFormatter.dateTimeFormatter(item.requested_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="expires_at">
                                        { dataFormatter.dateTimeFormatter(item.expires_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="quote_status">
                                        { item.quote_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="quote_total">
                                        { item.quote_total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.quotes_owner?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Sample_requests Requestedby</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>Samplerequestnumber</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Samplequantity</th>
                                
                                
                                
                                <th>Requestedat</th>
                                
                                
                                
                                <th>Neededby</th>
                                
                                
                                
                                <th>Samplestatus</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.sample_requests_requested_by && Array.isArray(users.sample_requests_requested_by) &&
                              users.sample_requests_requested_by.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/sample_requests/sample_requests-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="sample_request_number">
                                        { item.sample_request_number }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="sample_quantity">
                                        { item.sample_quantity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="requested_at">
                                        { dataFormatter.dateTimeFormatter(item.requested_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="needed_by">
                                        { dataFormatter.dateTimeFormatter(item.needed_by) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sample_status">
                                        { item.sample_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.sample_requests_requested_by?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Saved_lists Owner</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Listname</th>
                                
                                
                                
                                <th>Listtype</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.saved_lists_owner && Array.isArray(users.saved_lists_owner) &&
                              users.saved_lists_owner.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/saved_lists/saved_lists-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="list_name">
                                        { item.list_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="list_type">
                                        { item.list_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.saved_lists_owner?.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
        
        permission={'READ_USERS'}
        
      >
          {page}
      </LayoutAuthenticated>
    )
}

export default UsersView;