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

import metadata from '../../../blocks/premium/kit-contents/block.json';

registerBlockType( metadata.name, {
    edit: ( { attributes, setAttributes } ) => (
        <>
            <InspectorControls>
                <PanelBody title="Content">
                    <ProductSearchControl
                        value={ attributes.productId }
                        onChange={ ( val ) => setAttributes( { productId: val } ) }
                    />
                    <TextControl
                        label="Section 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' : '' } ) }
                    />
                    <ToggleControl
                        label="Show Component Images"
                        checked={ attributes.show_images === 'yes' }
                        onChange={ ( val ) => setAttributes( { show_images: val ? 'yes' : '' } ) }
                    />
                    <ToggleControl
                        label="Show Quantities"
                        checked={ attributes.show_quantities === 'yes' }
                        onChange={ ( val ) => setAttributes( { show_quantities: val ? 'yes' : '' } ) }
                    />
                    <ToggleControl
                        label="Show Component Prices"
                        checked={ attributes.show_prices === 'yes' }
                        onChange={ ( val ) => setAttributes( { show_prices: val ? 'yes' : '' } ) }
                    />
                    <ToggleControl
                        label="Link to Component Products"
                        checked={ attributes.link_to_products === 'yes' }
                        onChange={ ( val ) => setAttributes( { link_to_products: val ? 'yes' : '' } ) }
                    />
                    <SelectControl
                        label="Layout"
                        value={ attributes.layout }
                        options={ [
                            { label: 'List', value: 'list' },
                            { label: 'Grid', value: 'grid' },
                            { label: 'Table', value: 'table' },
                        ] }
                        onChange={ ( val ) => setAttributes( { layout: val } ) }
                    />
                    <ToggleControl
                        label="Hide for Non-Kit Products"
                        checked={ attributes.hide_if_not_kit === 'yes' }
                        onChange={ ( val ) => setAttributes( { hide_if_not_kit: val ? 'yes' : '' } ) }
                    />
                </PanelBody>
            </InspectorControls>
            <ServerSideRender block={ metadata.name } attributes={ attributes } />
        </>
    ),
    save: () => null,
} );
