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/tax_rates/tax_ratesSlice'
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";

import {hasPermission} from "../../helpers/userPermissions";


const Tax_ratesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { tax_rates } = useAppSelector((state) => state.tax_rates)
    
    const { currentUser } = useAppSelector((state) => state.auth);
    

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

              

              

              

              

              

              

              

              

              

              
                {hasPermission(currentUser, 'READ_ORGANIZATIONS') && 
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Organization</p>
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{tax_rates?.organization?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                  } 
              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>TaxName</p>
                    <p>{tax_rates?.name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>RatePercent</p>
                  <p>{tax_rates?.rate_percent || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Products TaxRate</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>ProductName</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>SalePriceHT</th>
                                
                                
                                
                                <th>PurchasePriceHT</th>
                                
                                
                                
                                
                                
                                <th>StockOnHand</th>
                                
                                
                                
                                <th>StockReserved</th>
                                
                                
                                
                                <th>ReorderLevel</th>
                                
                                
                                
                                <th>WeightedAvgCost</th>
                                
                                
                                
                                <th>TrackStock</th>
                                
                                
                                
                                <th>Active</th>
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {tax_rates.products_tax_rate && Array.isArray(tax_rates.products_tax_rate) &&
                              tax_rates.products_tax_rate.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/products/products-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="sku">
                                        { item.sku }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="sale_price_ht">
                                        { item.sale_price_ht }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="purchase_price_ht">
                                        { item.purchase_price_ht }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="stock_on_hand">
                                        { item.stock_on_hand }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="stock_reserved">
                                        { item.stock_reserved }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reorder_level">
                                        { item.reorder_level }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="weighted_avg_cost">
                                        { item.weighted_avg_cost }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="track_stock">
                                        { dataFormatter.booleanFormatter(item.track_stock) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_active">
                                        { dataFormatter.booleanFormatter(item.is_active) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!tax_rates?.products_tax_rate?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Tax_ratesView;