//产品编辑规格帮助脚本，编辑时外部可以设置var prdocut_data =[产品品项数据{"字段":"值","data":[{规格对应值数据}]}]
var spec_json = "";
var product_typeid = "0";
var opHtml = null;
var skuTable = null;

$(document).ready(function () {
    $(".product_type").change(function () {
        InitSpec();
    })
    InitSpec();
})
//初始化
function InitSpec() {
    product_typeid = $(".product_type").val();
    $.get("/Ajax.aspx?op=LoadSpecsJson&typeid=" + product_typeid + "&r=" + Math.random(), function (result) {
        spec_json = eval("(" + result + ")");
        if (spec_json.Status == "0") {
            $(".list_spec").html("");
            $(".spec_list").hide();
        } else {
            LoadOperateButtom();
            LoadTableTitle();
            CreateDataByEdit();
            $(".list_spec").html("");
            $(".list_spec").append(opHtml);
            $(".list_spec").append(skuTable);
            $(".spec_list").show();
        }
    });
}

//生成规格功能键HTML
function LoadOperateButtom() {
    opHtml = $("<div style=\"margin:10px;\"></div>");
    opHtml.append($(" <input type=\"button\" value=\"添加一个规格\" onclick=\"CreateRow()\" />"));
    //    opHtml.append($(" <input type=\"button\" value=\"关闭规格\" />"));
}
//生成表格
function LoadTableTitle() {
    skuTable = $("<table class=\"table_list specs_table\" style=\"width:98%\"  cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><\/table>");
    var tableHeader = $("<tr class=\"SpecificationTh\"><\/tr>");
    $.each(spec_json.data, function (index, item) {
        tableHeader.append($("<th  style=\"width:50px\">" + item["SpecName"] + "<\/th>"));
    });
    tableHeader.append($("<th  style=\"width:100px\">品项名<\/th>"));
    tableHeader.append($("<th style=\"width:100px\">货号<\/th>"));
    tableHeader.append($("<th style=\"width:60px\"><em class=\"red\">*<\/em>销售价<\/th>"));
    tableHeader.append($("<th style=\"width:60px\"><em class=\"red\">*<\/em>成本价<\/th>"));
    tableHeader.append($("<th style=\"width:80px\"><em class=\"red\">*<\/em>经销商价格<\/th>"));
    tableHeader.append($("<th style=\"width:40px\"><em class=\"red\">*<\/em>库存<\/th>"));
    tableHeader.append($("<th style=\"width:80px\"><em class=\"red\">*<\/em>警戒库存<\/th>"));
    tableHeader.append($("<th style=\"width:40px\">重量<\/th>"));
    tableHeader.append($("<th style=\"width:40px\">上架<\/th>"));
    tableHeader.append($("<th  style=\"width:30px\">操作<\/th>"));
    skuTable.append(tableHeader);
}
//生成行
function CreateRow() {
    var indexTr = skuTable.find("tr").length;
    var row = $("<tr></tr>");
    $.each(spec_json.data, function (index, item) {
        row.append($("<td><div class=\"specdefault\" style=\"padding:3px;line-height:20px;border:solid 1px #cccccc;cursor:hand; background-color:#FFFFCC\" specid=\"" + item["SpecId"] + "\" onclick=\"OpenSpecValue(this);\"><span>请选择</span><input name=\"spec_" + indexTr + "_" + item["SpecId"] + "\" value=\"\" type=\"hidden\"  /></div><\/td>"));
    });
    row.append($("<td ><input class=\"text_box width100\" name=\"SKUName_" + indexTr + "\" type=\"text\" value=\"" + $(".txtProductName").val() + indexTr + "\"/><\/td>"));
    row.append($("<td ><input class=\"text_box width80\" name=\"SKUCode_" + indexTr + "\" type=\"text\" value=\"\"/><\/td>"));
    row.append($("<td ><input class=\"text_box width40\" name=\"SalePrice_" + indexTr + "\" type=\"text\" value=\"" + $(".txtSalePrice").val() + "\"/><\/td>"));
    row.append($("<td ><input class=\"text_box width40\" name=\"Cost_" + indexTr + "\" type=\"text\" value=\"" + $(".txtCost").val() + "\"/><\/td>"));
    row.append($("<td ><input class=\"text_box width40\" name=\"WholeSalePrice_" + indexTr + "\" type=\"text\" value=\"" + $(".txtWholeSalePrice").val() + "\"/><\/td>"));
    row.append($("<td ><input class=\"text_box width40\" name=\"Stock_" + indexTr + "\" type=\"text\" value=\"\"/><\/td>"));
    row.append($("<td ><input class=\"text_box width40\" name=\"AlertStock_" + indexTr + "\" type=\"text\" value=\"\"/><\/td>"));
    row.append($("<td ><input class=\"text_box width40\" name=\"Weight_" + indexTr + "\" type=\"text\" value=\"\"/><\/td>"));
    row.append($("<td ><input name=\"Active_" + indexTr + "\" type=\"checkbox\" class=\"active_check\" /><\/td>"));
    row.append($("<td ><a href=\"javascript:void(0)\" onclick=\"deleteRow(this)\">删除</a><input name=\"specindex" + indexTr + "\" value=\"" + indexTr + "\" type=\"hidden\" /><input name=\"spec_SkuId" + indexTr + "\" value=\"0\" type=\"hidden\" class=\"p_id\" /><\/td>"));
    skuTable.append(row);
}
//编辑时生成对应数据var prdocut_data =[产品品项数据{"字段":"值","data":[{规格对应值数据}]}]spec_" + indexTr + "_" + item["SpecId"]
function CreateDataByEdit() {
    if (typeof prdocut_data == "undefined" || prdocut_data == null) return;
    for (var i = 0; i < prdocut_data.length; i++) {
        CreateRow();
        var p_json = prdocut_data[i];
        var tr = skuTable.find("tr:last");
        $.each(tr.find("input"), function (index, item_input) {
            if ($(item_input).attr("name").indexOf("spec_" + tr.index() + "_") > -1) {
                $.each(p_json.data, function (index, p_data) {
                    var tr_specId = $(item_input).attr("name").replace("spec_" + tr.index() + "_", "");
                    if (tr_specId == p_data.SpecId) {
                        $(item_input).val(p_data.ValueId);
                        $(item_input).parent().find("span").text(p_data.ValueStr);
                    }
                })
            }
            else {
                $.each(p_json, function (name, value) {
                    if ($(item_input).attr("name").indexOf(name) > -1) {
                        if ($(item_input).attr("type") == "checkbox") {
                            $(item_input).attr("checked", value == "1"); 
                        } else {
                            $(item_input).val(value);
                        }
                    }
                })
            }
        })
    }
}
//删除行
function deleteRow(obj) {
    $(obj).parent().parent().remove();
}
//弹出属性值选择
function OpenSpecValue(objControl) {
    var obj = $(objControl);
    OpenSpecValueDialog('/Dialog/SelectSpecsValueDialog.aspx?specid=' + obj.attr("specid") + '', '选择属性值', function (name, value) {
        if (value == "") name = "请选择";
        obj.find("span").text(name);
        obj.find('input[type="hidden"]').val(value);
    });
}
function OpenSpecValueDialog(url, _title, CallbackFun) {
    art.dialog.open(url, {
        title: _title,
        ok: function () {
            var dBody = $(this.iframe.contentWindow.document.body);
            var hidName = dBody.find('input[name="hidName"]');
            var hidValue = dBody.find('input[name="hidValue"]');
            CallbackFun(hidName.val(), hidValue.val());
        },
        cancel: true
    }, false).lock();
    return false;
};