import React, { useEffect } from 'react'
import { Col, Row, Input, Form, Button, Select } from 'antd'
import { connect, useDispatch } from 'react-redux'
import commonStyles from '../../../Utils/Common.less'
import { SaveOutlined } from '@ant-design/icons'
import type {
    GroupAndPartnerFormDataInterface,
    GroupAndPartnerFormInterface,
    GroupAndPartnerFormStateInterface,
} from '../../../Types/GroupAndPartners'
import { GetCountryList } from '../../../Services/Common'
import { GroupAndPartnerItemDefaultData } from './groupAndPartnerList'
import type { ListFrontEndInterface } from '../../../Types/CommonType'
import S3Upload from '../../../Components/S3Upload'
import ActivePremission from '../../../Utils/premissins'
import { PermissionType } from '../../../Types/Enum/PermissionType'
import { GetParentCategoryList } from '../../../Services/GroupAndPartners'

const GroupAndPartnerForm: React.FC<GroupAndPartnerFormInterface> = (props) => {
    const {
        onSave,
        editData,
        countryList,
        parentCategoryList,
        isLoadingModal,
    } = props
    const [form] = Form.useForm()
    const dispatch = useDispatch()

    useEffect(() => {
        GetCountryList(dispatch)
        GetParentCategoryList(dispatch)
    }, [dispatch])

    useEffect(() => {
        const countryData = countryList.find(
            (data: ListFrontEndInterface) => data.label === editData.country
        )
        const newData = {
            ...editData,
            country: countryData?.value ?? '0',
        }
        form.setFieldsValue(newData)
        if (editData.parentCategoryId === 0) {
            form.setFieldValue('parentCategoryId', '0')
        }
    }, [editData, countryList])

    const setFormData = (name: string, location: string): void => {
        form.setFieldValue(name, location)
    }

    const resetForm = (): void => {
        form.setFieldsValue(GroupAndPartnerItemDefaultData)
    }

    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"
                labelCol={{ span: 24 }}
                wrapperCol={{ span: 24 }}
                initialValues={{ remember: true }}
                layout={'vertical'}
                autoComplete="off"
                onFinish={(data) => {
                    onSave(data, resetForm)
                }}
                form={form}
                requiredMark={customizeRequiredMark}
            >
                <Form.Item<GroupAndPartnerFormDataInterface>
                    name="clientCategoryId"
                    hidden={true}
                >
                    <Input />
                </Form.Item>
                <Row gutter={16}>
                    <Col span={12}>
                        <Form.Item<GroupAndPartnerFormDataInterface>
                            label="Company Name"
                            name="clientCategoryName"
                            rules={[
                                {
                                    required: true,
                                    message: 'Please input your company name!',
                                },
                            ]}
                        >
                            <Input />
                        </Form.Item>
                    </Col>
                    <Col span={12}>
                        <Form.Item<GroupAndPartnerFormDataInterface>
                            label="Address One"
                            name="addressOne"
                            rules={[
                                {
                                    required: true,
                                    message: 'Please input your address one!',
                                },
                            ]}
                        >
                            <Input />
                        </Form.Item>
                    </Col>
                </Row>
                <Row gutter={16}>
                    <Col span={12}>
                        <Form.Item<GroupAndPartnerFormDataInterface>
                            label="Address Two"
                            name="addressTwo"
                        >
                            <Input />
                        </Form.Item>
                    </Col>
                    <Col span={12}>
                        <Form.Item<GroupAndPartnerFormDataInterface>
                            label="Town"
                            name="town"
                            rules={[
                                {
                                    required: true,
                                    message: 'Please input your town!',
                                },
                            ]}
                        >
                            <Input />
                        </Form.Item>
                    </Col>
                </Row>
                <Row gutter={16}>
                    <Col span={12}>
                        <Form.Item<GroupAndPartnerFormDataInterface>
                            label="Country"
                            name="country"
                        >
                            <Select
                                allowClear
                                showSearch
                                placeholder="Please select"
                                onChange={() => {}}
                                optionFilterProp="children"
                                filterOption={(input, option) =>
                                    (option?.label ?? '')
                                        .toLowerCase()
                                        .includes(input.toLowerCase())
                                }
                                options={[
                                    { value: '0', label: '' },
                                    ...countryList,
                                ]}
                            />
                        </Form.Item>
                    </Col>
                    <Col span={12}>
                        <Form.Item<GroupAndPartnerFormDataInterface>
                            label="PostCode"
                            name="postCode"
                            rules={[
                                {
                                    required: true,
                                    message: 'Please input your PostCode!',
                                },
                            ]}
                        >
                            <Input />
                        </Form.Item>
                    </Col>
                </Row>
                <Row gutter={16}>
                    <Col span={12}>
                        <Form.Item<GroupAndPartnerFormDataInterface>
                            label="Mobile No"
                            name="mobileNumber"
                            rules={[
                                {
                                    pattern:
                                        /^\(?(\d{2})\)?[- ]?(\d{4})[- ]?(\d{6})$/,
                                    message: 'please enter valid mobile number',
                                },
                                {
                                    required: true,
                                    message: 'Please input your mobile number!',
                                },
                            ]}
                        >
                            <Input />
                        </Form.Item>
                    </Col>
                    <Col span={12}>
                        <Form.Item<GroupAndPartnerFormDataInterface>
                            label="Work No"
                            name="workNumber"
                            rules={[
                                {
                                    pattern:
                                        /^\(?(\d{2})\)?[- ]?(\d{4})[- ]?(\d{6})$/,
                                    message: 'please enter valid mobile number',
                                },
                            ]}
                        >
                            <Input />
                        </Form.Item>
                    </Col>
                </Row>
                <Row gutter={16}>
                    <Col span={12}>
                        <Form.Item<GroupAndPartnerFormDataInterface>
                            label="Home No"
                            name="homeNumber"
                            rules={[
                                {
                                    pattern:
                                        /^\(?(\d{2})\)?[- ]?(\d{4})[- ]?(\d{6})$/,
                                    message: 'please enter valid mobile number',
                                },
                            ]}
                        >
                            <Input />
                        </Form.Item>
                    </Col>
                    <Col span={12}>
                        <Form.Item<GroupAndPartnerFormDataInterface>
                            label="Email"
                            name="emailAddress"
                            rules={[
                                {
                                    type: 'email',
                                    required: true,
                                    message: 'Please input your email!',
                                },
                            ]}
                        >
                            <Input />
                        </Form.Item>
                    </Col>
                </Row>
                <Row gutter={16}>
                    <Col span={12}>
                        <Form.Item<GroupAndPartnerFormDataInterface>
                            label="Alternative Email"
                            name="alternativeEmailAddress"
                            rules={[
                                {
                                    type: 'email',
                                    message: 'Please Input Alternative Email!',
                                },
                            ]}
                        >
                            <Input />
                        </Form.Item>
                    </Col>
                    <Col span={12}>
                        <Form.Item<GroupAndPartnerFormDataInterface>
                            label="Website"
                            name="website"
                            rules={[
                                {
                                    pattern:
                                        /^(https?:\/\/)(www\.)?[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}([/?].*)?$/,
                                    message: 'Please enter a valid website URL',
                                },
                            ]}
                        >
                            <Input />
                        </Form.Item>
                    </Col>
                </Row>
                <Row gutter={16}>
                    <Col span={12}>
                        <Form.Item<GroupAndPartnerFormDataInterface>
                            label="Document Footer Text"
                            name="documentFooterText"
                        >
                            <Input.TextArea rows={4} />
                        </Form.Item>
                    </Col>
                    <Col span={12}>
                        <Form.Item<GroupAndPartnerFormDataInterface>
                            label="Parent Company"
                            name="parentCategoryId"
                        >
                            <Select
                                allowClear
                                placeholder="Please select"
                                onChange={() => {}}
                                options={[
                                    { value: '0', label: '' },
                                    ...parentCategoryList,
                                ]}
                            />
                        </Form.Item>
                    </Col>
                </Row>
                <Row gutter={16}>
                    <Col span={12}>
                        <Form.Item<GroupAndPartnerFormDataInterface>
                            label="Business Logo"
                            name="businessLogoPath"
                        >
                            <S3Upload
                                name={'businessLogoPath'}
                                value={form.getFieldValue('businessLogoPath')}
                                setFormData={setFormData}
                                corporate={''}
                                corporateid={0}
                                documenttype={''}
                                documentbaseId={0}
                                memberpersonId={0}
                            />
                        </Form.Item>
                    </Col>
                    <Col span={12}>
                        <Form.Item<GroupAndPartnerFormDataInterface>
                            label="E-Signature"
                            name="signaturePath"
                        >
                            <S3Upload
                                name={'signaturePath'}
                                value={form.getFieldValue('signaturePath')}
                                setFormData={setFormData}
                                corporate={''}
                                corporateid={0}
                                documenttype={''}
                                documentbaseId={0}
                                memberpersonId={0}
                            />
                        </Form.Item>
                    </Col>
                </Row>
                <Row gutter={16}>
                    <Col span={12}>
                        <Form.Item<GroupAndPartnerFormDataInterface>
                            label="Rubber Stamp"
                            name="rubberStampLogo"
                        >
                            <S3Upload
                                name={'rubberStampLogo'}
                                value={form.getFieldValue('rubberStampLogo')}
                                setFormData={setFormData}
                                corporate={''}
                                corporateid={0}
                                documenttype={''}
                                documentbaseId={0}
                                memberpersonId={0}
                            />
                        </Form.Item>
                    </Col>
                </Row>
                {ActivePremission('1000', PermissionType.SAVE) && (
                    <Row gutter={16}>
                        <Col offset={22} span={2}>
                            <Button
                                type="primary"
                                htmlType="submit"
                                loading={Boolean(isLoadingModal)}
                            >
                                <SaveOutlined />
                            </Button>
                        </Col>
                    </Row>
                )}
            </Form>
        </div>
    )
}

const mapStateToProps = (state: any): GroupAndPartnerFormStateInterface => {
    return {
        countryList: state.common.countryList,
        parentCategoryList: state.initial.parentCategoryList,
    }
}

export default connect(mapStateToProps)(GroupAndPartnerForm)
