/**
 * Date Field Block
 */

(function (blocks, element, blockEditor, components, i18n) {
	const { registerBlockType } = blocks;
	const { createElement: el, Fragment, useEffect } = element;
	const { InspectorControls, useBlockProps } = blockEditor;
	const { PanelBody, TextControl, ToggleControl } = components;
	const { __ } = i18n;

	registerBlockType('giraforms/date-field', {
		icon: {
			src: 'calendar',
			foreground: '#3858e9'
		},
		edit: function (props) {
			const { attributes, setAttributes, clientId } = props;
			const { fieldId, label, required, requiredMessage, helpText, minDate, maxDate } = attributes;

			// Generate field ID if not set
			useEffect(() => {
				if (!fieldId) {
					setAttributes({
						fieldId: 'date_' + Math.random().toString(36).substring(2, 11),
					});
				}
			}, []);

			const blockProps = useBlockProps({
				className: 'giraforms-field giraforms-field-date',
			});

			return el(
				Fragment,
				null,
				el(
					InspectorControls,
					null,
					el(
						PanelBody,
						{ title: __('Field Settings', 'giraforms'), initialOpen: true },
						el(TextControl, {
							label: __('Label', 'giraforms'),
							value: label,
							onChange: (value) => setAttributes({ label: value }),
						}),
						el(ToggleControl, {
							label: __('Required Field', 'giraforms'),
							checked: required,
							onChange: (value) => setAttributes({ required: value }),
						}),
						required && el(TextControl, {
							label: __('Required message', 'giraforms'),
							value: requiredMessage,
							onChange: (val) => setAttributes({ requiredMessage: val }),
							placeholder: __('This field is required.', 'giraforms'),
							help: __('Overrides the default required error message for this field.', 'giraforms'),
						}),
						el(TextControl, {
							label: __('Help text', 'giraforms'),
							value: helpText,
							onChange: (value) => setAttributes({ helpText: value }),
						})
					),
					el(
						PanelBody,
						{ title: __('Date Restrictions', 'giraforms'), initialOpen: false },
						el(TextControl, {
							label: __('Min Date', 'giraforms'),
							value: minDate,
							onChange: (value) => setAttributes({ minDate: value }),
							type: 'date',
							help: __('YYYY-MM-DD', 'giraforms'),
						}),
						el(TextControl, {
							label: __('Max Date', 'giraforms'),
							value: maxDate,
							onChange: (value) => setAttributes({ maxDate: value }),
							type: 'date',
							help: __('YYYY-MM-DD', 'giraforms'),
						})
					),
					
				),
				el(
					'div',
					blockProps,
					el(
						'label',
						{ htmlFor: fieldId, className: required ? 'required' : '' },
						label,

					),
					el('input', {
						type: 'date',
						id: fieldId,
						disabled: true,
					}),
					helpText &&
						el('div', { className: 'giraforms-field-description' }, helpText)
				)
			);
		},

		save: function (props) {
			const { attributes } = props;
			const { fieldId, label, required, requiredMessage, helpText, minDate, maxDate } = attributes;

			const blockProps = useBlockProps.save({
				className: 'giraforms-field giraforms-field-date',
			});

			return el(
				'div',
				blockProps,
				el(
					'label',
					{ htmlFor: fieldId, className: required ? 'required' : '' },
					label
				),
				el('input', {
					type: 'date',
					id: fieldId,
					name: fieldId,
					required: required,
					'data-required-message': requiredMessage || undefined,
					min: minDate || null,
					max: maxDate || null,
				}),
				helpText &&
					el('div', { className: 'giraforms-field-description' }, helpText)
			);
		},
	});
})(
	window.wp.blocks,
	window.wp.element,
	window.wp.blockEditor,
	window.wp.components,
	window.wp.i18n
);
