import { Input, Form, Select, Button, Row, Col, Tooltip } from 'antd'
import { connect, useDispatch } from 'react-redux'
import commonStyles from '../../../../../../Utils/Common.less'
import React, { useEffect } from 'react'
import { SaveOutlined } from '@ant-design/icons'
import {
    GetPaymentOptionDropDownList,
    SetDirectDebitCustomer,
} from '../../../../../../Services/Corporate'
import { type SoleTraderConfigurationInviteClientInterface } from '../../../../../../Types/Client/RegisteredClients/SoleTrader'
import { useLocation } from 'react-router-dom'

const { TextArea } = Input

const GoCardlessInviteClient = (
    props: SoleTraderConfigurationInviteClientInterface
): React.ReactElement => {
    const { paymentOptionDropDownList, cancelGoCardlessModal } = props
    const dispatch = useDispatch()

    const location = useLocation()
    const SoleTraderData = location.state?.SoleTraderData
    const [form] = Form.useForm()

    useEffect(() => {
        GetPaymentOptionDropDownList(dispatch)
    }, [])

    const handleFinish = (): void => {
        void handleFinishData()
    }
    const handleFinishData = async (): Promise<void> => {
        try {
            await form.validateFields()
            const formData = form.getFieldsValue()
            const data = {
                CustomerId: 0,
                ApplicationId: 0,
                ReferenceId: SoleTraderData?.soleTraderId,
                ClientCategoryId: SoleTraderData?.clientcategoryid ?? 0,
                BusinessTypeId: 2,
                CompanyName: '',
                GivenName: '',
                FamilyName: '',
                Email: formData.email,
                AddressLine1: '',
                AddressLine2: '',
                AddressLine3: '',
                City: '',
                PostalCode: '',
                CountryCode: '',
                Region: '',
                Language: '',
                PhoneNumber: '',
                DanishIdentityNumber: '',
                SwedishIdentityNumber: '',
                Description: formData?.description ?? '',
                RegisterToken: '',
                PaymentOptionId: formData?.goCardlessAccount,
            }

            void SetDirectDebitCustomer(dispatch, data, cancelGoCardlessModal)
        } catch (error) {}
    }

    const customizeRequiredMark = (
        label: React.ReactNode,
        { required }: { required: boolean }
    ): React.ReactElement => (
        <>
            {label}
            &nbsp;
            {required && <span className={commonStyles.requireIcon}>*</span>}
        </>
    )

    return (
        <div className={commonStyles.formWrapper}>
            <Form
                name="complex-form"
                layout={'vertical'}
                autoComplete="off"
                form={form}
                onFinish={handleFinish}
                requiredMark={customizeRequiredMark}
                className={commonStyles.flexDirectionColumn}
            >
                <Form.Item
                    label="Email"
                    name="email"
                    rules={[
                        {
                            required: true,
                            message: 'Please Enter Email',
                        },
                        {
                            type: 'email',
                            // pattern:
                            //     /^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})(?:\.[a-zA-Z]{2,})?$/,
                            message: 'Please enter a valid email address!',
                        },
                    ]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    label="Gocardless Account"
                    name="goCardlessAccount"
                    rules={[
                        {
                            required: true,
                            message: 'Please Enter Gocardless Account',
                        },
                    ]}
                >
                    <Select
                        allowClear
                        placeholder="Select your payment option"
                        onChange={() => {}}
                        options={paymentOptionDropDownList}
                        showSearch
                        filterOption={(input, option) =>
                            option?.label
                                ?.toLowerCase()
                                .includes(input.toLowerCase()) ?? false
                        }
                    />
                </Form.Item>
                <Form.Item label="Description" name="description">
                    <TextArea rows={4} />
                </Form.Item>
                <Row gutter={16}>
                    <Col offset={21} span={3}>
                        <Tooltip title="Send Invite CLient">
                            <Button type="primary" htmlType="submit">
                                <SaveOutlined />
                            </Button>
                        </Tooltip>
                    </Col>
                </Row>
            </Form>
        </div>
    )
}

const mapStateToProps = (state: any): any => {
    return {
        paymentOptionDropDownList: state.client.paymentOptionDropDownList,
    }
}

export default connect(mapStateToProps)(GoCardlessInviteClient)
