var base = $("base").attr("href");

$(document).ready(function () {

    $('.b-fav').click(function () {

        var Cid = $(this).data('id');
        var Mid = $('.global-container').data('mid');
        var Active = $(this).hasClass("active");
        if (Active) {
            var CheckActive = 'delete';
        } else {
            var CheckActive = 'insert';
        }

        $.ajax({
            url: base + "/member/action-favorite",
            type: 'POST',
            data: {
                Cid: Cid,
                Mid: Mid,
                CheckActive: CheckActive
            },
            dataType: "JSON",
            xhr: function () {
                var myXhr = $.ajaxSettings.xhr();
                return myXhr;
            },
            success: function (data) {
                if (data.code == 201) {
                    $('.b-fav[data-id=' + Cid + ']').toggleClass('active');
                } else {
                    $('#signup').modal('show');
                }
                return false;
            }
        });
        return false;
    });
});

// Show Password
$(document).on('click', '#showpass1', function () {
    if ($(this).text() == 'แสดงรหัสผ่าน') {
        $('input[name="inputPassword"]').attr('type', 'text');
        $(this).text('ซ่อนรหัสผ่าน');
    } else {
        $('input[name="inputPassword"]').attr('type', 'password');
        $(this).text('แสดงรหัสผ่าน');
    }
});
$(document).on('click', '#showpass2', function () {
    if ($(this).text() == 'แสดงรหัสผ่าน') {
        $('input[name="inputCfPassword"]').attr('type', 'text');
        $(this).text('ซ่อนรหัสผ่าน');
    } else {
        $('input[name="inputCfPassword"]').attr('type', 'password');
        $(this).text('แสดงรหัสผ่าน');
    }
});

// Forgot Email
$(document).validator().on('submit', '#forgotpass', function (e) {

    if (e.isDefaultPrevented()) {
        $('#forgotpass').validator('validate');
    } else {
        e.preventDefault();

        $('.submitforgotpass').html('<i class="fa fa-circle-o-notch fa-spin" style="font-size:32px; vertical-align: middle;"></i>');
        $(".submitforgotpass").attr("disabled", true);

        var type = "POST";
        var url = base + "/member/forgot-pass";
        var data = $(this).serialize();
        $.ajax({
            type: type, url: url, data: data,
            success: function (data) {
                var data = JSON.parse(data);

                
                if (data.code == 400) {
                    $('#msgforgotpass').show();
                    $('.submitforgotpass').html('ส่งลิงค์ตั้งรหัสผ่านใหม่');
                    $(".submitforgotpass").attr("disabled", false);
                    
                }
                if (data.code == 201) {
                    $('#signup').modal('hide');
                    $('#modalForgot').modal('show');
                    $('#modalForgot .msg').html(data.msg);
                }
                return false;
            }
        });
    }
});

$(document).on('hide.bs.modal', "#modalForgot", function () {
    location.reload();
});

// Login Email
$(document).validator().on('submit', '#loginemail', function (e) {

    if (e.isDefaultPrevented()) {
        $('#loginemail').validator('validate');
    } else {
        e.preventDefault();

        var type = "POST";
        var url = base + "/member/callback-loginemail";
        var data = $(this).serialize();
        $.ajax({
            type: type, url: url, data: data,
            success: function (data) {
                var data = JSON.parse(data);
                console.log(data);
                if (data.code == 400) {
                    $('#msglogin').show();
                }
                if (data.code == 200) {
                    window.location.replace(data.url);
                }
                return false;
            }
        });
    }
});

// Register Email
$(document).validator().on('submit', '#regisemail', function (e) {

    if (e.isDefaultPrevented()) {
        $('#regisemail').validator('validate');
    } else {
        e.preventDefault();

        $('.submitregisemail').html('<i class="fa fa-circle-o-notch fa-spin" style="font-size:32px; vertical-align: middle;"></i>');
        $(".submitregisemail").attr("disabled", true);

        var type = "POST";
        var url = base + "/member/regis-email";
        var data = $(this).serialize();
        $.ajax({
            type: type, url: url, data: data,
            success: function (data) {
                var data = JSON.parse(data);

                $('#signup').modal('hide');
                if (data.code == 201) {
                    $('#modalRegister').modal('show');
                    $('#modalRegister .msg').html(data.msg);
                } else {
                    $('#modalRegister').modal('show');
                    $('#modalRegister .msg').html(data.msg);
                }
                return false;
            }
        });
    }
});

$(document).on('hide.bs.modal', "#modalRegister", function () {
    location.reload();
});

// Function Main.js Edit
$(function () {

    // Birthday
    $('input[name="Birthday"]').daterangepicker({
        autoUpdateInput: false,
        "singleDatePicker": true,
        "showDropdowns": true,
        "autoApply": true,
        "locale": {
            "format": "DD-MM-YYYY",
        }
    });

    $('input[name="Birthday"]').on('apply.daterangepicker', function (ev, picker) {
        $(this).val(picker.startDate.format('DD-MM-YYYY'));
    });

    $('input[name="Birthday"]').on('cancel.daterangepicker', function (ev, picker) {
        $(this).val('');
    });

});