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/price_lists/price_listsSlice'
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 Price_listsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { price_lists } = useAppSelector((state) => state.price_lists)
    

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

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Pricelistname</p>
                    <p>{price_lists?.price_list_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Pricelisttype</p>
                    <p>{price_lists?.price_list_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Accounts Defaultpricelist</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>
                            {price_lists.accounts_default_price_list && Array.isArray(price_lists.accounts_default_price_list) &&
                              price_lists.accounts_default_price_list.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>
                        {!price_lists?.accounts_default_price_list?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Price_list_items Pricelist</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Caseprice</th>
                                
                                
                                
                                <th>Unitprice</th>
                                
                                
                                
                                <th>Minimumcaseqty</th>
                                
                                
                                
                                <th>Effectivestart</th>
                                
                                
                                
                                <th>Effectiveend</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {price_lists.price_list_items_price_list && Array.isArray(price_lists.price_list_items_price_list) &&
                              price_lists.price_list_items_price_list.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/price_list_items/price_list_items-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="case_price">
                                        { item.case_price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="unit_price">
                                        { item.unit_price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="min_case_qty">
                                        { item.min_case_qty }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="effective_start">
                                        { dataFormatter.dateTimeFormatter(item.effective_start) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="effective_end">
                                        { dataFormatter.dateTimeFormatter(item.effective_end) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!price_lists?.price_list_items_price_list?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Account_price_lists Pricelist</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Effectivestart</th>
                                
                                
                                
                                <th>Effectiveend</th>
                                
                                
                                
                                <th>Primary</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {price_lists.account_price_lists_price_list && Array.isArray(price_lists.account_price_lists_price_list) &&
                              price_lists.account_price_lists_price_list.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/account_price_lists/account_price_lists-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="effective_start">
                                        { dataFormatter.dateTimeFormatter(item.effective_start) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="effective_end">
                                        { dataFormatter.dateTimeFormatter(item.effective_end) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_primary">
                                        { dataFormatter.booleanFormatter(item.is_primary) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!price_lists?.account_price_lists_price_list?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Price_listsView;