import 'admin-lte/plugins/daterangepicker/daterangepicker';
import 'admin-lte/plugins/daterangepicker/daterangepicker.css';

export default () => {
    const locale = {
        format: LOCALE.FORMAT__DATE,
        cancelLabel: LOCALE.DATE_RANGE_PICKER__CANCEL_TEST,
        applyLabel: LOCALE.DATE_RANGE_PICKER__APPLY_TEST,
        daysOfWeek: [
            LOCALE.DATE_RANGE_PICKER__SUNDAY,
            LOCALE.DATE_RANGE_PICKER__MONDAY,
            LOCALE.DATE_RANGE_PICKER__TUESDAY,
            LOCALE.DATE_RANGE_PICKER__WEDNESDAY,
            LOCALE.DATE_RANGE_PICKER__THURSDAY,
            LOCALE.DATE_RANGE_PICKER__FRIDAY,
            LOCALE.DATE_RANGE_PICKER__SATURDAY,
        ],
        monthNames: [
            LOCALE.DATE_RANGE_PICKER__JANUARY,
            LOCALE.DATE_RANGE_PICKER__FEBRUARY,
            LOCALE.DATE_RANGE_PICKER__MARCH,
            LOCALE.DATE_RANGE_PICKER__APRIL,
            LOCALE.DATE_RANGE_PICKER__MAY,
            LOCALE.DATE_RANGE_PICKER__JUNE,
            LOCALE.DATE_RANGE_PICKER__JULY,
            LOCALE.DATE_RANGE_PICKER__AUGUST,
            LOCALE.DATE_RANGE_PICKER__SEPTEMBER,
            LOCALE.DATE_RANGE_PICKER__OCTOBER,
            LOCALE.DATE_RANGE_PICKER__NOVEMBER,
            LOCALE.DATE_RANGE_PICKER__DECEMBER,
        ],
    };

    $('.date-range-picker').each(function () {
        let target = $(this);
        if (target.is('input')) {
            target.daterangepicker({
                locale,
            });
        } else {
            let wrapper = $(this);
            let txtStart = wrapper.find('[name=startDate]');
            let startDate = txtStart.val() || null;
            let txtEnd = wrapper.find('[name=endDate]');
            let endDate = txtEnd.val() || null;
            let placeholder = $('<input type="text" class="form-control required" />');

            let options = {
                locale,
            };

            if (startDate && endDate) {
                options.startDate = startDate;
                options.endDate = endDate;
            }

            wrapper.append(placeholder);
            placeholder.daterangepicker(options, function (start, end, label) {
                txtStart.val(start.format(LOCALE.FORMAT__DATE));
                txtEnd.val(end.format(LOCALE.FORMAT__DATE));
            });

            if (!(startDate && endDate)) {
                placeholder.val('');
            }
        }
    });
};
