limit-add.js 10.7 KB
var $ = require('jquery'),
    common = require('../common/common');

require('../common/bootstrap-wysihtml5');
require('../util/datepicker');

var ENMA = {
    attachType: {
        1: "图片",
        2: "视频",
        3: "文本"
    },
    dia:{
        1:"img",
        2:"video",
        3:"wenZi"
    },
    bucket:{
        bucket:"goodsimg"
    }
}


//测试数据
var g = new common.grid({
    el: "#baseTable",
    columns: [{
        display: "标题",
        render: function(item) {
            if (item.isDefault == 1) {
                return "默认封面图片";
            }
            return ENMA.attachType[item.attachType];
        }
    }, {
        display: "内容",
        render: function(item) {
            item.orderBy = (item.__index+1);
            if (item.isDefault == 1) {
                return '<input type="file" name="coverImg" id="coverImg" value="' + item.attachUrl + '" placeholder="封面图" required/>';
            } else {
                if (item.attachType == 1) {
                    return "<img src='" + item.attachUrl + "' style='width:120px;height:80px' />";
                } else if (item.attachType == 2) {
                    return "<video src='" + item.attachUrl + "' controls></video>";
                } else {
                    return item.intro;
                }
            }
        }
    }, {
        display: "操作",
        render: function(item) {
            var htmlArr = [];
            if (!item.isDefault) {
                htmlArr.push("<button type='button' data-index='" + item.__index + "' class='delete btn btn-danger btn-xs'>删除</button>");
                htmlArr.push("<button type='button' data-index='" + item.__index + "' class='edit btn btn-info btn-xs'>编辑</button>");
            }
            return htmlArr.join(',');
        }
    }]
});

var __ac = $.trim($("#attachmentContent").val());
g.__rows = __ac ? JSON.parse(__ac) : [{
    "attachType": 1,
    "attachUrl": "",
    "isDefault": 1
}];

g.init(g.__rows);
var e1 = new common.edit('#baseTable',ENMA.bucket);
e1.on("callback", function(obj) {
    if (obj.key == "file_onComplete_coverImg") {
        g.__rows[0].attachUrl = obj.data;
    }
});
e1.init();

var isSave = false;
$(window).on('beforeunload', function() {
    if (!isSave) {
        return '您输入的内容尚未保存';
    }
});
//初始化form表单
var e = new common.edit('#editor-group',ENMA.bucket);


e.on("validate", function() {
    var param = {
        batchNo: $('#batchNo').val(),
        id: $("#id").val()
    };
    var _m="批次号接口接口异常";
    common.util.__ajax({
        url: '/limit/getLimitCodeByBatchNo',
        async: false,
        data: param
    }, function(res){
        console.log(res);
        if(res.data){
            _m="";
        }else{
            _m="批次号不存在";
        }
    }, true);
    if(_m){
        return _m;
    }else{
        common.util.__ajax({
            url: '/limit/checkBatch',
            async: false,
            data: param
        }, function(res){
             console.log(res);
             if(res.data){
                _m="";
            }else{
                _m=res.message;
            }
        }, true);
        
        if(_m){
            return _m;
        }
    }
});
///limit/getLimitCodeByBatchNo


e.on("validate", function() {
    if (!g.__rows[0].attachUrl) {
        return "请上传默认封面";
    }
});
e.on("validate", function() {
    var limitProductType = $("#limitProductType").val();
    var activityId = $.trim($("#activityId").val());
    if (limitProductType == 2) {
        if (activityId == "") {
            return "请填写排队限购ID";
        }else{
           var _m="排队限购ID接口异常";
           common.util.__ajax({
                url:'/limit/DrawlineActivityRest/checkActivityIdExists',
                async:false,
                data:{activityId:activityId}
            },function(res){
                if(res.data.exists){//true
                    _m="";
                }else{
                    _m="排队限购ID不存在";
                }
            },true);
           if(_m){
                return _m;
           }else{
            ///limit/checkActivityIdNoExist
                common.util.__ajax({
                    url:'/limit/checkActivityIdNoExist',
                    async:false,
                    data:{activityId:activityId,id: $("#id").val()}
                },function(res){
                    console.log("checkActivityIdNoExist",res);
                    if(res.data){//true
                        _m="";
                    }else{
                        _m="排队限购ID"+activityId+"已被关联";
                    }
                },true);

                if(_m){
                    return _m;
                }
           }
        }
    }
});
e.on("validate", function() {
    var orderBy = $.trim($("#orderBy").val());
    if (orderBy && !/^(?:[1-9][0-9]*|0)$/.test(orderBy)) {
        return '排序值输入格式不正确';
    }
});


function orderByIsRequired(val) {
    if (val == 1) {
        $('.order-by').find('.red').show().end().find('#orderBy').attr('required', true);
    } else {
        $('.order-by').find('.red').hide().end().find('#orderBy').attr('required', false);
    };
}
orderByIsRequired($('#hotFlag').val());
$('input[name="hotFlag"]').on('click', function() {
    orderByIsRequired($(this).val());
});


e.init();

//添加修改
$('.limit-add-btn').click(function(option) {
    isSave = true;
    e.submit($('#limit-add-form').attr('action'), function(option) {
        //option.debug=true;
        option.data.attachmentContent = JSON.stringify(g.__rows);

        /*if ($('input[name="dayFlag"]:checked').val() == 0) {
            saleTime = new Date(option.data.saleTime.slice(0, -3).replace(/-/g, '/'));
        }*/
        console.log(option.data);
        // option.debug=true;
        option.beforeSend = function() {
            $('#limit-add-btn').addClass('disabled');
        };
        option.success = function(res) {
            res = res.data;

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

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


// var $editor = $('#editor'),  //文字,图片,视频容器
//     type = $('#limit-add-form').attr('type');

var Bll = {
    img: function(title, item, index) {
        common.dialog.confirm(title, common.util.__template($("#picDialogTemp").html(), item), function() {
            var _html = $.trim($("#imgText").val());

            if (_html) {
                if (item.attachType) {
                    //修改
                    g.__rows[index].attachUrl = _html;
                } else {
                    //添加
                    g.__rows.push({
                        "attachType":1,
                        "attachUrl": _html,
                        "isDefault": 0,
                        "attachName": $("#attachName").val()
                    });
                }

                g.reload();
                e1.init();
            }
        });
        var a = new common.edit("#upload-wrapper",ENMA.bucket);
        a.on("callback", function(obj) {
            if (obj.key == "file_onComplete_imgfile") {
                $("#imgText").val(obj.data);
            }
        });
        a.init();
    },
    video:function(title, item, index){
        common.dialog.confirm(title, common.util.__template($("#vidioDialogTemp").html(), item), function() {
            var _html = $.trim($("#videoText").val());
            var _html1 = $.trim($("#imgText").val());
            if (_html&&_html1) {
                if (item.attachType) {
                    //修改
                    g.__rows[index].attachUrl = _html;
                    g.__rows[index].intro = _html1;
                } else {
                    //添加
                    g.__rows.push({
                        "attachType": 2,
                        "attachUrl": _html,
                        "intro":_html1,
                        "isDefault": 0,
                        "attachName": $("#attachName").val()
                    });
                }

                g.reload();
                e1.init();
            }
        });
        var a1 = new common.edit("#upload-wrapper1",ENMA.bucket);
        a1.on("callback", function(obj) {
            if (obj.key == "file_onComplete_videofile") {
                $("#videoText").val(obj.data);
            }
        });
        a1.init();
        var a2 = new common.edit("#upload-wrapper2",ENMA.bucket);
        a2.on("callback", function(obj) {
            console.log(obj);
            if (obj.key == "file_onComplete_imgfile") {
                $("#imgText").val(obj.data);
            }
        });
        a2.init();
    },
    wenZi: function(title, item, index) {
        common.dialog.confirm(title, common.util.__template('<textarea id="text-editor">{intro}</textarea>', item), function() {
            var _html = $.trim($("#text-editor").val());

            if (_html) {
                if (item.attachType) {
                    //修改
                    g.__rows[index].intro = _html;
                } else {
                    //添加
                    g.__rows.push({
                        "attachType": 3,
                        "intro": _html,
                        "isDefault": 0
                    });
                }

                g.reload();
                e1.init();
            }
        });
        $('#text-editor').wysihtml5();
    }
}

//添加文字
$('body').on('click', '#textDialog', function() {
    Bll.wenZi("添加文字", {});
    return false;
});
$('body').on('click', '#picDialog', function() {
    Bll.img("添加图片",{});
    return false;
});
$('body').on('click', '#videoDialog', function() {
    Bll.video("添加视频",{});
    return false;
});
//删除
$('body').on('click', ".delete", function() {
    var index = $(this).data("index");
    g.__rows.splice(index, 1);
    g.reload();
    e1.init();
});
//修改
$('body').on('click', ".edit", function() {
    var item = g.rows[$(this).data("index")];
    Bll[ENMA.dia[item.attachType]]("修改" + ENMA.attachType[item.attachType], item, $(this).data("index"));
});