window.onload = function (ev) {
    init();
}

init = function () {
    getAttributeInfo();
}

getAttributeInfo = function () {
    var parameter = urlEncode(["SelectAllProductAttribute"]);
    $.ajax({
        type: "post",
        url: "/taiwan/php/index.php",
        data: {mode: parameter},
        dataType: "json",
        success: function (data) {
            if (data[0].length == 0 || data == -1) {
                var th = '<th colspan="9" scope="col" class="text-c f-20" style="padding: 50px 0;">暂无数据</th>';
                $("#warp").html(th);
                return;
            }
            printAttributeInfo(data);
            console.log(data);
        }
    });
}

printAttributeInfo = function (data) {
    var warp = $("#warp"),
        tr,
        href,
        title = "'查看商品信息'";
    warp.html("");
    for (var i = 0; i < data.length; i++) {
        href = "'product-add.html?pid=" + data[i].ProductId + "'";
        tr = $('<tr class="text-c">' +
            '<td>' + data[i].ProductInfoClassId + '</td>' +
            '<td>' + data[i].ProductInfoClassName + '</td>' +
            '<td>' + data[i].InfoClassIdAddTime + '</td>' +
            '<td>' +
            '<a class="btn btn-primary radius td-button" onclick=deleteAttribute("' + data[i].ProductInfoClassId + '")>删除</>' +
            '<a class="btn btn-primary radius td-button" onclick=showUpdateAttribute("' + data[i].ProductInfoClassName + '","' + data[i].ProductInfoClassId + '")>修改</>' +
            '</td></tr>'
        )
        ;


        warp.append(tr);
    }
    $("#loading").css({"display": "none"});
    $("#warp").css({"display": "table-row-group"});
}


showAddWarp = function () {
    $("#modal-demo").modal("show");
}

setAttribute = function (jugde, text, aid) {
    var _el = $("#addAttribute");
    if (jugde) {
        $(_el).bind("click", function () {
            addAttribute();
        });
    }
    else {
        $(_el).bind("click", function () {
            updateAttribute(text, aid);
        });
    }
}

addAttribute = function () {
    var name = $("#AttributeName").val();
    if (name == "" || name.trim() == "") {
        $.Huimodalalert('请输入属性名！', 2000);
    }
    else {
        var parameter = urlEncode(["InsertAttribute", name]);
        $.ajax({
            type: "post",
            url: "/taiwan/php/index.php",
            data: {mode: parameter},
            dataType: "json",
            success: function (data) {
                $("#AttributeName")[0].value = "";
                getAttributeInfo();
            }
        });
        $("#warp").css({"display": "none"});
        $("#modal-demo").modal("hide");
        $("#loading").css({"display": "block"});
    }
}

deleteAttribute = function (aid) {
    layer.confirm('是否删除属性？', {
        btn: ['确定', '取消'] //按钮
    }, function (e, a) {
        var parameter = urlEncode(["DeleteAttribute", aid]);
        $.ajax({
            type: "post",
            url: "/taiwan/php/index.php",
            data: {mode: parameter},
            dataType: "json",
            success: function (data) {
                if (data[0].mode == 1) {
                    getAttributeInfo();
                    $.Huimodalalert('删除成功', 2000);
                    console.log(data);
                }
            }
        });
        $(a).prev().remove();
        $(a).remove();
        $("#warp").css({"display": "none"});
        $("#loading").css({"display": "block"});
    });
}

showUpdateAttribute = function (text, aid) {
    showAddWarp();
    $("#AttributeName").val(text);
    setAttribute(false, text, aid);
}

updateAttribute = function (text, aid) {
    var name = $("#AttributeName").val();
    if (name == "" || name.trim() == "") {
        $.Huimodalalert('请输入属性名！', 2000);
    }
    else {
        var parameter = urlEncode(["UpdateAttributeName", aid, name]);
        $.ajax({
            type: "post",
            url: "/taiwan/php/index.php",
            data: {mode: parameter},
            dataType: "json",
            success: function (data) {
                $("#AttributeName")[0].value = "";
                getAttributeInfo();
            }
        });
        $("#warp").css({"display": "none"});
        $("#modal-demo").modal("hide");
        $("#loading").css({"display": "block"});
    }
}


urlEncode = function (arr) {
    var i; //循环初始化
    for (i = 0; i < arr.length; i++) {
        arr[i] = encodeURIComponent(encodeURIComponent(arr[i]));
    }
    return arr.join();
}

