import { registerBlockType } from '@wordpress/blocks';
import { InspectorControls } from '@wordpress/block-editor';
import ServerSideRender from '@wordpress/server-side-render';
import { PanelBody, TextControl, TextareaControl, ToggleControl } from '@wordpress/components';

import metadata from '../../../blocks/premium/customer-downloads/block.json';

registerBlockType( metadata.name, {
    edit: ( { attributes, setAttributes } ) => (
        <>
            <InspectorControls>
                <PanelBody title="Content">
                    <TextControl
                        label="Title"
                        value={ attributes.title }
                        onChange={ ( val ) => setAttributes( { title: val } ) }
                    />
                    <ToggleControl
                        label="Show Title"
                        checked={ attributes.show_title === 'yes' }
                        onChange={ ( val ) => setAttributes( { show_title: val ? 'yes' : '' } ) }
                    />
                    <TextareaControl
                        label="Empty Message"
                        value={ attributes.empty_message }
                        onChange={ ( val ) => setAttributes( { empty_message: val } ) }
                    />
                    <TextareaControl
                        label="Login Required Message"
                        value={ attributes.login_message }
                        onChange={ ( val ) => setAttributes( { login_message: val } ) }
                    />
                    <ToggleControl
                        label="Show Product Name"
                        checked={ attributes.show_product_name === 'yes' }
                        onChange={ ( val ) => setAttributes( { show_product_name: val ? 'yes' : '' } ) }
                    />
                    <ToggleControl
                        label="Show Order Number"
                        checked={ attributes.show_order_number === 'yes' }
                        onChange={ ( val ) => setAttributes( { show_order_number: val ? 'yes' : '' } ) }
                    />
                    <ToggleControl
                        label="Show Download Limit"
                        checked={ attributes.show_download_limit === 'yes' }
                        onChange={ ( val ) => setAttributes( { show_download_limit: val ? 'yes' : '' } ) }
                    />
                    <ToggleControl
                        label="Show Expiry Date"
                        checked={ attributes.show_expiry_date === 'yes' }
                        onChange={ ( val ) => setAttributes( { show_expiry_date: val ? 'yes' : '' } ) }
                    />
                    <TextControl
                        label="Download Button Text"
                        value={ attributes.download_button_text }
                        onChange={ ( val ) => setAttributes( { download_button_text: val } ) }
                    />
                </PanelBody>
            </InspectorControls>
            <ServerSideRender block={ metadata.name } attributes={ attributes } />
        </>
    ),
    save: () => null,
} );
