; jQuery(function ($) {
    function initColorPicker($input) {
        if ($input.data('wpColorPicker')) {
            $input.wpColorPicker('destroy');
        }
        $input.wpColorPicker();
    }

    $(document).on('click', '[data-micromodal-trigger="modal-new-tag"]', function () {
        setTimeout(function () {
            initColorPicker($('#tag-color-picker-new'));
        }, 100);
    });

    $(document).on('click', '.btn-edit-tag', function () {
        setTimeout(function () {
            initColorPicker($('#tag-color-picker-edit'));
        }, 100);
    });

    var reloadTags = function(){
        var container  = $('.tags-container');

        $.ajax({
            method: 'GET',
            url: anbarak.ajax_url,
            data: {
                action: "anbarak_get_expenses_tags",
                security: anbarak.nonce
            },
            dataType:'html'
        }).done(function (html) {
            container.html(html)
        }).fail(function (response) { })
    }

    $('form[name="frm-new-tag"]').submit(function (e) {
        e.preventDefault();
        var $form = $(this);
        $.ajax({
            method: 'POST',
            url: anbarak.ajax_url,
            data: $form.serialize(),
            dataType: 'json'
        }).done(function (data) {
            if (data.success) {
                $form.trigger("reset");
                var $colorInput = $form.find('input[name="color"]');
                $colorInput.val('');
                if ($colorInput.data('wpColorPicker')) {
                    $colorInput.wpColorPicker('color', '');
                }
                MicroModal.close('modal-new-tag');
                reloadTags();
            } else {
                alert(data.data.message || anbarak.i18n.error);
            }
        }).fail(function (response) { })
    })

    $(document).on('click', '.btn-remove-tag', function(){
        var $btn = $(this);
        var tagId = $btn.data('id');

        anbarakConfirmAsync({
            message: anbarak.i18n.delete_tag,
            type: 'ok-cancel',
        }).then(function (confirmed) {
            if (!confirmed) return false;

            $btn.addClass('loading');
            $.ajax({
                method: 'POST',
                url: anbarak.ajax_url,
                data: {
                    id: tagId,
	                    action: 'anbarak_remove_expenses_tag',
                    security: anbarak.nonce
                },
                dataType: 'json'
            }).done(function (data) {
                if (data.success) {
                    reloadTags();
                } else {
                    alert(data.data.message || anbarak.i18n.error);
                }
            }).fail(function (response) { })
            .always(function () {
                $btn.removeClass('loading');
            })
        })
    })

    $(document).on('click', '.btn-edit-tag', function(){
        var $row = $(this).closest('.tag-row');
        var id = $row.data('id');
        var name = $row.data('name');
        var color = $row.data('color');

        $('#edit-tag-id').val(id);
        $('form[name="frm-edit-tag"] input[name="name"]').val(name);
        $('form[name="frm-edit-tag"] input[name="color"]').val(color);

        MicroModal.show('modal-edit-tag');
    })

    $('form[name="frm-edit-tag"]').submit(function (e) {
        e.preventDefault();
        var $form = $(this);
        $.ajax({
            method: 'POST',
            url: anbarak.ajax_url,
            data: $form.serialize(),
            dataType: 'json'
        }).done(function (data) {
            if (data.success) {
                $form.trigger("reset");
                var $colorInput = $form.find('input[name="color"]');
                $colorInput.val('');
                if ($colorInput.data('wpColorPicker')) {
                    $colorInput.wpColorPicker('color', '');
                }
                MicroModal.close('modal-edit-tag');
                reloadTags();
            } else {
                alert(data.data.message || anbarak.i18n.error);
            }
        }).fail(function (response) { })
    })


    $('#frm-print-settings').submit(function (e) {
        e.preventDefault();
        var $form = $(this);
        $.ajax({
            method: 'POST',
            url: anbarak.ajax_url,
            data: $form.serialize(),
        }).done(function (html) {
            alert(anbarak.i18n.settings_saved)
        }).fail(function (response) { })
    })

	})
