$('body').on('click', '[data-auc]', function () {
    loadContentAuc($(this).attr('data-auc'));

});
function loadContentAuc(data) {
    $.ajax({
        type: 'POST',
        data: "auction=lot&id="+data,
        url: '/',
        success: function (data) {
            if(data == 'exit'){
                setTimeout("document.location.href='/'", 10);
                window.history.pushState(null, null, "/login");
            }
            setTimeout(function () {

                $('.auc-content').html(data);

                //$('.auc-content').fadeIn(200);

                //$('.preLoader').fadeOut(250);
            }, 200);

        }
    });
}

var Auction = {

    char_id:"", // Selected char when you make bet
    LotID:"", // Auction Selected lot
    lot_timer: [], // Auction Selected lot
    timers: [], // Auction Selected lot


    'AuctionList':function () {

        var filter_type = "&filter_type=" + $("select[name=filter_type]").val();
        var filter_grade = "&filter_grade=" + $("select[name=filter_grade]").val();

        console.log(filter_type, filter_grade);

        $.ajax({
            url:"/index?module=auction",
            data:"html=AuctionList" + filter_type + filter_grade,
            type:"POST",
            cache:false,
            beforeSend:function () {
                $('.ajax-content').html("Идёт загрузка...");
            },
            success:function (response) {
                $(".ajax-content").html("");
                $(".ajax-content").html(response);
            }
        });
    },

    'CharItems':function (event) {
        char_id = "&char_id=" + event.value;
        $.ajax({
            url:"/",
            data:"auction=CharItemList" + char_id,
            type:"POST",
            cache:false,
            beforeSend:function () {
                $('.message').html("Идёт загрузка...");
                $('.auction_item_lot select,input').each(function () {
                    this.disabled = true;
                });
            },
            success:function (response) {
                $(".message").html('');
                $("select[name=lot]").html(response);
                $('.auction_item_lot select, input').each(function () {
                    this.disabled = false;
                });
            }
        });
    },

    'ExchangeCountItem':function (event) {
        char_id = "&char_id=" + $("select[name=char_id]").val();
        type = "&type=" + $("select[name=type]").val();
        $.ajax({
            url:"/index?module=auction",
            data:"html=Exchnager" + char_id + type,
            type:"POST",
            cache:false,
            beforeSend:function () {
                $('.message').html("Идёт загрузка...");
                $('select,input').each(function () {
                    this.disabled = true;
                });
            },
            success:function (response) {
                $(".message").html(response);
                $("select[name=coins]").html(response);
                $('select, input').each(function () {
                    this.disabled = false;
                });
            }
        });
    },

    'RequestExchange':function (event) {
        $.ajax({
            url:"/index?module=auction",
            data:"action=RequestExchange&" + $(event).serialize(),
            type:"POST",
            cache:false,
            beforeSend:function () {
                $('.message').html("Идёт загрузка...");
                $('select,input').each(function () {
                    this.disabled = true;
                });
            },
            success:function (response) {
                $(".message").html(response);
//                $("select[name=coins]").html(response);
                $('select, input').each(function () {
                    this.disabled = false;
                });
            }
        });
    },


    "SendNewLot":function (event) {
        $.ajax({
            url:"/",
            data:"auction=AddLot&" + $(event).serialize(),
            type:"POST",
            cache:false,
            beforeSend:function () {
                $('.message').html("Идёт загрузка...");
                $('.auction_item_lot select,input').each(function () {
                    this.disabled = true;
                });
            },
            success:function (response) {
                if(response == 'block') {
                    $(".close").click();
                    $("#protection").click();
                }else {
                    $(".message").html(response);
                    $('.auction_item_lot select,input').each(function () {
                        this.disabled = false;
                    });
                }
            }
        });
    },

    ShowLot:function (event) {

        LotShow = "auction=lot";
        LotShow += "&id=" + $(event).attr('id');
        this.LotID = $(event).attr('id');

        $.ajax({
            url:"/",
            data:LotShow,
            type:"POST",
            cache:false,
            beforeSend:function () {
                $('.ajaxContent').html("Идёт загрузка...");
            },
            success:function (response) {
                $(".ajaxContent").html("");
                $(".ajaxContent").html(response);
            }
        });
    },


    make_bet:function (bet, lot_id, char_id) {

        $.ajax({
            url:"/",
            data:"auction=make_bet&lot=" +lot_id + "&bet=" + bet + "&char_id=" + char_id,
            type:"POST",
            cache:false,
            beforeSend:function () {
                $('#message').fadeIn(300).html("Идёт загрузка...");
//                $('#listBetError').html("Идёт загрузка...");
            },
            success:function (response) {

                if(response == 'block') {
                    $(".close").click();
                    $("#protection").click();
                }else {
                    var object = jQuery.parseJSON( response );
                    $("#message").html("");
                    $("#message").html(object.message);

                }


            }
        });
    },


    BuyNow :function (lot_id, char_id) {
        $.ajax({
            url:"/",
            data:"auction=buynow&lot=" + lot_id + "&char_id=" + char_id,
            type:"POST",
            cache:false,
            beforeSend:function () {
                $('#message').fadeIn(300).html("Идёт загрузка...");
//                $('#listBetError').html("Идёт загрузка...");
            },
            success:function (response) {

                if(response == 'block') {
                    $(".close").click();
                    $("#protection").click();
                }else {
                    var object = jQuery.parseJSON( response );
                    $("#message").html("");
                    $("#message").html(object.message);
                }

            }
        });
    }



};
