Edit.js 6.42 KB
'use strict';
var $ = require('jquery'), common = require('../../../common/common');

var dropDown = new common.dropDown({
    el: '#shop',
    ajax: 'shopsRest'
});

var e = new common.edit('#editor-group');

e.on("validate", function () {

    var discount = $("#discount").val();

    if (!/^1|0\.[0-9]{0,2}$/.test(discount) || discount > 1.0 || discount <= 0.0) {
        return "折扣格式不正确";
    }

    var skns = '';

    var iptSkns = $("input[name='productSkn']");

    for (var i = 0, l = iptSkns.length; i < l; i++) {
        var $thiz = $(iptSkns[i]);
        var skn = $.trim($thiz.val());
        if (/^[1-9][0-9]{4,10}$/.test(skn)) {
            skns = skns + skn + ",";
        } else if(skn.length != 0) {
            return "第 " + (i + 1) + " 个 SKN 格式错误";
        }
    }

    /*var lio = skns.lastIndexOf(",");

    if (lio == -1) {
        return "请填写商品 SKN";
    }*/

    // $("#productSkn").val(skns.substring(0, lio));

});

e.init();

if($("#id").val()){
    var sknArray = $("#productSkn").val().split(",");
    var shopName = $("#shopName").val();
    $.each(sknArray,function(index,item){
        $($("#budle-table").find("input[name='productSkn']")[index]).val(item);
    })
    $("#budle-table").find("input[name='bundleDiscount']").val($("#edit-discount").val());    

    $("#shopLable").text(shopName);
    $("#discount, input[name=bundleDiscount]").attr('disabled', 'disabled');
}


// submit
$('#add-btn').click(function(option) {

    var bundleName = $("#bundleName").val();
    var startTime = toSeconds($("#beginTimeStr").val());
    var endTime = toSeconds($("#endTimeStr").val());
    var shopId = $("#shopId").val();
    var id = $("#id").val();
    

    var bundleArray = [];
    $("#budle-table").find("tr").each(function(){
        var bundle = {}; 
        var sknList = [];   
        $(this).find("input[name='productSkn']").each(function(){
            sknList.push($(this).val());
        });
        bundle.productSkn = sknList.join(",");
        bundle.discount = $(this).find("input[name='bundleDiscount']").val();
        bundle.bundleName=bundleName;
        bundle.startTime=startTime;
        bundle.endTime=endTime;
        bundle.shopId=shopId;
        bundle.id=id;
        bundleArray.push(bundle);
    })
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: '/product/bundle/save',
        data: {"boListStr":JSON.stringify(bundleArray)},
        success: function (rs) {            
            var data = rs;
            // $('.historyDetail').html($(common.util.__template2($("#template2").html(), data.data)));
           
            if(data.code == 200){
                common.util.__tip(data.message, 'success');
                history.go(-1)
            }else{
                common.util.__tip(data.message, 'warning');
            }
        },
        error: function (rs) {
            // $('.historyDetail').html($(common.util.__template2($("#template2").html(), {})));
            common.util.__tip(rs.message, 'danger');
        }
    });



    /*e.submit($('#add-form').attr('action'), function(option) {

        var data = option.data;

        // convert date
        data.startTime = toSeconds(data.beginTimeStr);
        data.endTime = toSeconds(data.endTimeStr);

        option.beforeSend = function() {
            $('#add-btn').addClass('disabled');
            console.log(data);
            data.zw="123";
            // analyzeBundleTable();
        };

        option.success = function(res) {
            if (res.code == "200") {
                e.$tip('提交成功', function() {
                    location.href = "/product/bundle/index";
                }, 'growl-success');
            } else {
                $('#add-btn').removeClass('disabled');
                e.$tip(res.message);
            }

            return false;
        }
        option.error = function(res) {
            e.$tip("提交失败");
        };
    });*/
});


$(".btn-up").click(function() {
    var tr = $(this).parents("tr");
    var ipt = tr.find("input[name='productSkn']");
    var prev = tr.prev().find("input[name='productSkn']");
    var val = ipt.val();
    ipt.val(prev.val());
    prev.val(val);
});

$(".btn-down").click(function() {
    var tr = $(this).parents("tr");
    var ipt = tr.find("input[name='productSkn']");
    var next = tr.next().find("input[name='productSkn']");
    var val = ipt.val();
    ipt.val(next.val());
    next.val(val);
});

function toSeconds(strDate) {
    var seconds = new Date(strDate).getTime() / 1000;
    return seconds;
}



$(".add-bundle-btn").click(function() {
    
    var bundleHtml = "";
    bundleHtml += "<tr>";
    bundleHtml += "<td>";
    bundleHtml += "<input  type=\"text\" name=\"productSkn\" value=\"\" style=\"width:23%\"><input name=\"productSkn\" type=\"text\" value=\"\" style=\"margin-left:5px;width:23%\">";
    bundleHtml += "<input  type=\"text\" name=\"productSkn\" value=\"\" style=\"margin-left:5px;width:23%\"><input name=\"productSkn\" type=\"text\" value=\"\" style=\"margin-left:5px;width:23%\"></td>";
    bundleHtml += "<td align=\"center\"><input type=\"text\"  name=\"bundleDiscount\" placeholder=\"折扣\" class=\"form-control\" required maxlength=\"10\" value=\"0.9\" style=\"width:40%\">";
    bundleHtml += "</td><td align=\"center\"><input class=\"btn btn-danger delete-bundle-btn\" type=\"button\" value=\"删除\" style=\"margin:5px\"></input>";
    bundleHtml += "</td></tr> ";
    $("#budle-table").append(bundleHtml);
    $(".delete-bundle-btn").click(function() {    
        $(this).parent().parent().remove(); 
    });

});



$("#shop").change(function(){
    // console.log(dropDown.getValue().text);
    $("#shopId").val(dropDown.getValue().id);    
    $("#shopLable").text(dropDown.getValue().text);
})

$("#discount").blur(function(){
    
    var discount = $("#discount").val();
    if (!/^1|0\.[0-9]{0,2}$/.test(discount) || discount > 1.0 || discount <= 0.0) {
        common.util.__tip("折扣格式不正确", 'warning');
        return;
    }

    $("input[name='bundleDiscount']").each(function(){
        $(this).val($("#discount").val());
    })
})


// function analyzeBundleTable(){

//     $("#budle-table").find("tr").each(function(){

//     })
// }



//新版本控制
//$("#basicInfo").html(common.util.__template2($("#template2").html(),RESPONSEDATA));

// bundleTemplate


/*[
    {"bundleDiscount":12,"bundleConfig":[{"skn":1234,"discount":0.9},{"skn":1234,"discount":"0.8"}]}
]*/

/*var bundleTemplateData = [];
*/