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/commission_plans/commission_plansSlice'
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 Commission_plansView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { commission_plans } = useAppSelector((state) => state.commission_plans)
    
    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 commission_plans')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View commission_plans')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/commission_plans/commission_plans-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Tenant</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{commission_plans?.tenant?.tenant_name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PlanName</p>
                    <p>{commission_plans?.plan_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PlanStatus</p>
                    <p>{commission_plans?.plan_status ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>CalculationBase</p>
                    <p>{commission_plans?.calculation_base ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>CommissionType</p>
                    <p>{commission_plans?.commission_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>DefaultRate</p>
                  <p>{commission_plans?.default_rate || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>DefaultAmount</p>
                  <p>{commission_plans?.default_amount || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>organizations</p>
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{commission_plans?.organizations?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Commission_rules CommissionPlan</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>AppliesToRole</th>
                                
                                
                                
                                
                                
                                <th>CommissionType</th>
                                
                                
                                
                                <th>Rate</th>
                                
                                
                                
                                <th>Amount</th>
                                
                                
                                
                                <th>MinOrderAmount</th>
                                
                                
                                
                                <th>IsActive</th>
                                
                                
                                
                                <th>RuleName</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {commission_plans.commission_rules_commission_plan && Array.isArray(commission_plans.commission_rules_commission_plan) &&
                              commission_plans.commission_rules_commission_plan.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/commission_rules/commission_rules-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="applies_to_role">
                                        { item.applies_to_role }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="commission_type">
                                        { item.commission_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="rate">
                                        { item.rate }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="amount">
                                        { item.amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="min_order_amount">
                                        { item.min_order_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_active">
                                        { dataFormatter.booleanFormatter(item.is_active) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="rule_name">
                                        { item.rule_name }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!commission_plans?.commission_rules_commission_plan?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Commissions CommissionPlan</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>CommissionCode</th>
                                
                                
                                
                                <th>CommissionStatus</th>
                                
                                
                                
                                <th>CommissionType</th>
                                
                                
                                
                                <th>BaseAmount</th>
                                
                                
                                
                                <th>CommissionAmount</th>
                                
                                
                                
                                <th>EarnedAt</th>
                                
                                
                                
                                <th>ApprovedAt</th>
                                
                                
                                
                                <th>PaidAt</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {commission_plans.commissions_commission_plan && Array.isArray(commission_plans.commissions_commission_plan) &&
                              commission_plans.commissions_commission_plan.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/commissions/commissions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="commission_code">
                                        { item.commission_code }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="commission_status">
                                        { item.commission_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="commission_type">
                                        { item.commission_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="base_amount">
                                        { item.base_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="commission_amount">
                                        { item.commission_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="earned_at">
                                        { dataFormatter.dateTimeFormatter(item.earned_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="approved_at">
                                        { dataFormatter.dateTimeFormatter(item.approved_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="paid_at">
                                        { dataFormatter.dateTimeFormatter(item.paid_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!commission_plans?.commissions_commission_plan?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Commission_plansView;