<?xml version="1.0" encoding="UTF-8"?>
<root>
    <default>
        <includes>
            <!--
                Path to css and js files into plugins folder.
                If your files are not in plugins folder, use absolute paths.
            -->
            <css>
                <file>material-datepicker/css/material-datepicker.min.css</file>
            </css>
            <js>
                <file>material-datepicker/dist/js/material-datepicker.min.js</file>
                <file>material-datepicker/dist/i18n/%language%.js</file>
            </js>
        </includes>
        <js_code><![CDATA[   if (typeof(materialDatePickers) === "undefined") {
        window.materialDatePickers = [];
    }

    document.querySelectorAll("%selector%").forEach(el => {
        let inputId                             = el.getAttribute('id'),
            fieldName                           = el.getAttribute('name'),
            dataAttr                            = el.dataset,
            dataAutoClose                       = dataAttr.autoClose === undefined?false:dataAttr.autoClose === 'true',
            dataFormatSubmit                    = dataAttr.formatSubmit === undefined?undefined:dataAttr.formatSubmit,
            dataFormat                          = dataAttr.format === undefined?'mmm dd, yyyy':dataAttr.format,
            dataDefaultDate                     = dataAttr.defaultDate === undefined?null:new Date(dataAttr.defaultDate),
            dataSetDefaultDate                  = dataAttr.setDefaultDate === undefined?false:dataAttr.setDefaultDate === 'true',
            dataDisableWeekends                 = dataAttr.disableWeekends === undefined?false:dataAttr.disableWeekends === 'true',
            dataFirstDay                        = dataAttr.firstDay === undefined?0:dataAttr.firstDay,
            dataMinDate                         = dataAttr.minDate === undefined?null:new Date(dataAttr.minDate),
            dataMaxDate                         = dataAttr.maxDate === undefined?null:new Date(dataAttr.maxDate),
            dataYearRange                       = dataAttr.yearRange === undefined?2:dataAttr.yearRange,
            dataIsRTL                           = dataAttr.isRtl === undefined?false:dataAttr.isRtl === 'true',
            dataShowMonthAfterYear              = dataAttr.showMonthAfterYear === undefined?false:dataAttr.showMonthAfterYear === 'true',
            dataShowDaysInNextAndPreviousMonths = dataAttr.showDaysInNextAndPreviousMonths === undefined?false:dataAttr.showDaysInNextAndPreviousMonths === 'true',
            dataShowClearBtn                    = dataAttr.showClearBtn === undefined?false:dataAttr.showClearBtn === 'true';

        // create an hidden input to submit a standard date
        if (dataFormatSubmit !== undefined) {
            let $inputSubmit = document.createElement('input');
            $inputSubmit.type = 'hidden';
            $inputSubmit.name = fieldName + '_submit';
            $inputSubmit.value = el.value;
            el.insertAdjacentElement('afterend', $inputSubmit);
        }

        let dpk = M.Datepicker.init(el, {
            autoClose: dataAutoClose,
            format: dataFormat,
            defaultDate: dataDefaultDate,
            setDefaultDate: dataSetDefaultDate,
            disableWeekends: dataDisableWeekends,
            firstDay: dataFirstDay,
            minDate: dataMinDate,
            maxDate: dataMaxDate,
            yearRange: dataYearRange,
            isRTL: dataIsRTL,
            showMonthAfterYear: dataShowMonthAfterYear,
            showDaysInNextAndPreviousMonths: dataShowDaysInNextAndPreviousMonths,
            showClearBtn: dataShowClearBtn,
            i18n: materialDateI18n,
            onClose: function() {
                if (dataFormatSubmit !== undefined) {
                    document.querySelector('[name="' + fieldName + '_submit"]').value = this.toString('yyyy-mm-dd');
                }
                var formId = document.querySelector("%selector%").closest('form').getAttribute('id');
                var form   = forms[formId];
                if (typeof(form.fv) == 'object' && fieldName in form.fv.elements) {
                    form.fv.revalidateField(fieldName);
                }
            }
        });
        materialDatePickers[inputId] = dpk;
    });]]>
        </js_code>
    </default>
    <custom>
        <!--
            If you need differents includes for your custom code,
            copy the default '<includes>' structure and put the needed files into '<file>' nodes.
            You can add several files nodes if necessary.
        -->
        <js_code>
            <!-- your custom code here -->
        </js_code>
    </custom>
</root>
