Edit.js 7.1 KB
var $ = require('jquery');
var common = require('../../../common/common');
var editBean = require('../partials/limitcode-edit-bean');

var e = new common.edit2("#basicForm");
var type = $("#basicForm").attr("role"),
    action = $("#basicForm").data("action");
var isFirst = true;
var limitDateFromObj;
var limitDateToObj;
/**
 * 通用对象
 * @type {{__render: Bll.__render, __checkSkn: Bll.__checkSkn}}
 */
var Bll = {
    __render: function () {
        editBean.currTime = common.util.__dateFormat(new Date(), "yyyy-MM-dd hh:mm:ss");
        $("#basicForm").html(common.util.__template2($("#limitcodeEdit-template").html(), editBean));
        e.init();
        limitDateFromObj = $("#limitDateFrom").fdatepicker({
            format: 'yyyy-mm-dd hh:ii:ss',
            pickTime: true
        }).data("datepicker");
        limitDateToObj = $("#limitDateTo").fdatepicker({
            format: 'yyyy-mm-dd hh:ii:ss',
            pickTime: true
        }).data("datepicker");
        limitDateFromObj.setEndDate(editBean.limitDateTo);
        if (editBean.limitDateFrom) {
            limitDateToObj.setStartDate(editBean.currTime);
        } else if (new Date(editBean.currTime).getTime() < new Date(editBean.limitDateFrom)) {
            limitDateToObj.setStartDate(editBean.limitDateFrom);
        } else {
            limitDateToObj.setStartDate(editBean.currTime);
        }
        if (type == "edit") {
            $('input[name="skn_sku"]').prop('disabled', true);
            $(".noEdit").prop("disabled", true);
         
            if ((new Date(editBean.limitDateFrom).getTime() <= new Date().getTime()) && isFirst) {
                $("#limitDateFrom").prop("disabled", true);
            }
            isFirst = false;
        }
    },
    __checkSkn: function (callback) {
        var flag = false;
        if (editBean.limitSkn) {
            common.util.__ajax2({
                async: false,
                url: "/limitCode/checkSknValidity",
                data: {
                    skn: editBean.limitSkn,
                    id: editBean.id
                }
            }, function (res) {
                if (!res.data.isExist) {
                    flag = true;
                    common.util.__tip("SKN不存在");
                } else if (res.data.isConflict) {
                    flag = true;
                    common.util.__tip("SKN与其他限购冲突");
                } else if (res.data.isOnSales) {
                    flag = true;
                    common.util.__tip("SKN已上架,请先下架");
                }
                callback && callback(res);
            }, function () {
                flag = true;
                common.util.__tip("网络请求异常");
            });
        }
        return flag;
    }
};

if (type != "add") {
    var id = location.href.substring(location.href.lastIndexOf("/") + 1);
    common.util.__ajax({
        url: "/limitCode/getLimitCode",
        data: {id: id}
    }, function (res) {
        editBean = res.data;
        editBean.reqDepartment = editBean.reqDepartment?editBean.reqDepartment.split("/"):[];
        if (!editBean.reqDepartment.length) {
            editBean.reqDepartment1 = "零售部";
            editBean.reqDepartment2 = "营销策划"
        }else{
            editBean.reqDepartment1= editBean.reqDepartment[0];
            editBean.reqDepartment2= editBean.reqDepartment[1];
        }
        editBean.selected = editBean.skus ? "SKU" : "SKN";
        editBean.updateType = type == "edit" ? "1" : "2";
        editBean.skusInfo = [];
        if (editBean.skus) {
            var skuArr = editBean.skus.split(";");
            var skuNumArr = editBean.skuTimes.split(";");
            for (var i = 0; i < skuArr.length; i++) {
                editBean.skusInfo.push({
                    "sku": skuArr[i],
                    "skuTimes": skuNumArr[i]
                });
            }
        }
        Bll.__render();
    }, true);
} else {
    Bll.__render();
}
/**
 * 监控输入变化
 */
$(document).on("change", ".observe", function () {
    var $this = $(this);
    var name = $this.data("field");
    editBean = common.util.__buildobj(name, '.', editBean, function (obj, name) {
        obj[name] = $this.val();
    });
    if ($this.prop("id") == "limitDateFrom" && new Date($this.val()).getTime()) {
        if (new Date(editBean.currTime).getTime() > new Date(editBean.limitDateFrom)) {
            limitDateToObj.setStartDate(editBean.currTime);
        } else {
            limitDateToObj.setStartDate(editBean.limitDateFrom);
        }
    }
});

//输入限制
$(document).on("keyup", ".number", function () {
    $(this).val($(this).val().replace(/\D/g, ''));
});

//单选框点击事件
$(document).on("click", ".limitProduct", function () {
    editBean.limitCodeType = $(this).val();
    $("#limitSkn").attr("required", !(editBean.limitCodeType == "N"));
    Bll.__render();
});

$(document).on("click", ".delSku", function () {
    var index = $(this).data("index");
    editBean.skusInfo.splice(index, 1);
    Bll.__render();
    $(".addSku").attr("disabled", false);
});
$(document).on("click", ".addSku", function () {
    editBean.skusInfo.push({
        "sku": "",
        "skuTimes": ""
    });
    Bll.__render();
});

$(document).on("blur", "#limitSkn", function () {
    Bll.__checkSkn(function (res) {
        if (res.data.skuList) {
            editBean.skusInfo = [];
            for (var i = 0; i < res.data.skuList.length; i++) {
                editBean.skusInfo.push({
                    "sku": res.data.skuList[i],
                    "skuTimes": ""
                });
            }
            Bll.__render();
            $(".addSku").attr("disabled", false);
        }
    });
});

/**
 * 保存事件
 */
$(document).on("click", "#save_brand", function () {
    //  限购码状态0:待审核 1:审核通过 2:驳回 3:过期 4:作废
    if (e.validate() && (type == "add" ? (!editBean.limitSkn || !Bll.__checkSkn()) : true)) {
        if (editBean.limitCodeType == "U") {
            for (var i in editBean.skusInfo) {
                if (!editBean.skusInfo[i].skuTimes || editBean.skusInfo[i].skuTimes == "0") {
                    common.util.__tip("SKU数量不能为0");
                    return;
                }
            }
        }
        editBean.reqDepartment = [editBean.reqDepartment1,editBean.reqDepartment2].join("/");
        editBean.creatorId = $("#auth_id").val();
        editBean.creatorName = $("#auth_name").val();
        if (editBean.status != 1) {
            editBean.status = 0;
        }
        if (editBean.limitCodeType == "U") {
            editBean.skus = "";
            editBean.skuTimes = "";
            for (var item in editBean.skusInfo) {
                if (item != 0) {
                    editBean.skus += ";";
                    editBean.skuTimes += ";";
                }
                editBean.skus += editBean.skusInfo[item].sku;
                editBean.skuTimes += editBean.skusInfo[item].skuTimes;
            }
        }
        common.util.__ajax({
            url: action,
            data: editBean
        }, function () {
            location.href = "/market/limitcode/index";
        });
    }
    return false;
});