﻿var Obj = {
    selectgood: function (goodId) {
        if (goodId > 0) {
            $.get("/tools/GetImageCountByGood", { goodId: goodId }, function (data) {

                $("#goodInfo").html(String.Format('Изображений у товара: {0}', data));

            });
        }
        else { $("#goodInfo").html(''); }
    },
    toGoodListForm: function () {
        $('#goToImgListBtn').click(function () {
            if ($("#goodId") && $("#goodId").val()) {
                $("#goToListForm").submit();
            }
            else {
                alert('Выберите, пожалуйста, товар');
            }
        });

        $('#goToBrandImgListBtn').click(function () {
            if ($("#brandId") && $("#brandId").val() && $("#brandId").val() != '0') {
                window.location = '/image/listbrand?brandId=' + $("#brandId").val();
            }
            else {
                alert('Выберите, пожалуйста, бренд');
            }
        });

        $('#uploadGoodSizesBtn').click(function () {
            if ($("#ImgId") && $("#ImgId").val()) {
                $.get("/load/UploadByImgId", { imgId: $("#ImgId").val() }, function (data) {
                    alert(data);
                });
            }
            else {
                alert('Изображение не определено');
            }
        });


        $('#uploadGoodBtn').click(function () {
            if ($("#goodId") && $("#goodId").val()) {
                $.get("/load/upload", { goodId: $("#goodId").val() }, function (data) {
                    alert(data);
                });
            }
            else {
                alert('Выберите, пожалуйста, товар');
            }
        });
        $('#uploadBrandBtn').click(function () {
            if ($("#brandId") && $("#brandId").val() && $("#brandId").val() != "0") {
                $.get("/load/upload", { brandId: $("#brandId").val() }, function (data) {
                    alert(data);
                });
            }
            else {
                alert('Выберите, пожалуйста, бренд');
            }
        });
        $('#downloadBtn').click(function () {
            $.get("/load/download", function (data) {
                alert(data);
            });
        });
    },
    goods: function () {

        $("#brandId").change(function () {

            if ($(this).val() != '0') {

                $.getJSON("/tools/getgoodsbybrandid", { brandid: $(this).val() }, function (json) {

                    var option_template = "<option value='{1}'>{0}</option>";
                    var opts = '';

                    for (var i = 0; i < json.length; i++) {
                        opts += String.Format(option_template, json[i].Name, json[i].ID);
                    }

                    $('#goodId').html(opts);

                    Obj.selectgood(json[0].ID);

                });

                $.get("/tools/GetModelList", { brandId: $(this).val() }, function (data) {

                    $("#modelList").html(data);

                });

            } else {
                $('#goodId').html('');
                $("#modelList").html('');
                Obj.selectgood(0);
            }
        });

        $("#goodId").change(function () {

            if ($('#goodInfo')) {
                Obj.selectgood($(this).val());
            }
        });
    },
    gall: function () {
        $(".thumbs a").fancybox({
            'hideOnContentClick': true,
            'type': 'image'
        });

    },
    start: function () {
        if ($("#brandId"))
            Obj.goods();
        //if ($("#thumbs"))
        Obj.gall();
        if ($('#goToListForm'))
            Obj.toGoodListForm();
    }
}
$(document).ready(Obj.start);
