limit-add.js 4.71 KB
/*
 *@time: 2016/2/15
 *@author: chenglong
 */


var $ = require('jquery'),
    common=require('../common/common'),
    edit = require('../common/edit');

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

// var uploadImg = new edit('#coverImgBox');
// //
// uploadImg.init();



/*var editor = new wangEditor('editor-container');
editor.config.menus = [
    'source',
    '|',
    'img',
    'vedio'
];
editor.create();*/


//日期插件
$('#saleTime').fdatepicker({
    format: 'yyyy-mm-dd'
});

$('#saleTime').on('change', function() {
    console.log($(this).val());
})
    
    var $editor = $('#editor');
    $('body').on('click', '#textDialog', function() {
        common.dialog.confirm("添加文本",'<textarea id="text-editor"></textarea>',function(){

            var textStr = $('#text-editor').val();
            var $oText = $('<div class="quota-content quota-text" data-type="3"><span>' + textStr + '</span><a href="javascript:;" class="editor-delete btn btn-default">删除</a></div>');

            $oText.appendTo($editor);

        });
        $('#text-editor').wysihtml5();
        return false;
    });
    $('body').on('click', '#picDialog', function() {
        common.dialog.confirm("添加图片",common.util.__template($("#picDialogTemp").html(),{name:"图片"}),function(){

            var imgStr = '<img src="http://img11.static.yhbimg.com/goodsimg/2015/07/07/08/01315b5bbfd72bc5b59fa2fda6c28b9ae6.jpg">';
            var $oImg = $('<div class="quota-content quota-img" data-type="1">' + imgStr + '<a href="javascript:;" class="editor-delete btn btn-default">删除</a></div>');

            $oImg.appendTo($editor);
        });
        return false;
    });
    $('body').on('click', '#videoDialog', function() {
        common.dialog.confirm("添加视频",common.util.__template($("#picDialogTemp").html(),{name:"视频"}),function(){

            /*var imgStr = '<img src="http://img11.static.yhbimg.com/goodsimg/2015/07/07/08/01315b5bbfd72bc5b59fa2fda6c28b9ae6.jpg">';
            var $oVideo = $('<div class="quota-imf"><img src="' + imgStr + '" alt="" /></div>');

            $('.quota-video').html(imgStr);*/
        });
        return false;
    });


// 表单验证
var limitAddForm = new edit("#limit-add-form");

limitAddForm.init();
$('.limit-add-btn').click(function () {


    var $this = $(this);

    limitAddForm.submit($('#limit-add-form').attr('action'), function(option, that) {
        option.beforeSend = function () {
            $this.attr('disabled', 'disabled');
        };

        option.success=function(res){
            console.log(res);
            if (res.data.code == 200) {

                limitAddForm.$tip('提交成功',function(){
                    //window.location.href = '/limit/product/index';
                }, 'growl-success');
                $this.removeAttr('disabled');

                
            }
        };
        option.error=function(res){

            limitAddForm.$tip(res.message);
            $this.removeAttr('disabled');
        };
        //option.data.time = $('#saleTime').val();
        option.data.attachmentContent = [];
        option.data.attachmentContent.push({
            attachUrl:option.data.coverImg,
            isDefault: 1,
            attachType: 1,
            orderBy: 0
        });
        if ($('.quota-content').size() > 0) {
            $('.quota-content').each(function(i) {
                var type = $(this).data('type'),
                    content = '',
                    obj = {
                        attachType: type,
                        isDefault: 0,
                        orderBy: i
                    }

                if (type == 3) {
                    content = $(this).find('span').text();
                    obj.intro = content;
                } else if (type == 1) {
                    content = $(this).find('img').attr('src');
                    obj.attachUrl = content;
                }
                option.data.attachmentContent.push(obj)
            });
        }
        console.log(option.data);
        option.data.attachmentContent = JSON.stringify(option.data.attachmentContent);


        option.data.saleTime = new Date(option.data.saleTime.replace('-',',')).getTime() / 1000; 
        
    });
});

$('#saleTime').val(function() {
    if ($(this).data('time')) {
        var date = new Date($(this).data('time') * 1000);
        return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
    }
    
})

$('#limit-add-form').on('click', '.editor-delete', function() {
    $(this).parents('.quota-content').remove();
});