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

var edit = require('../../../common/edit');


var mainData = window.ViewModel;
console.log(mainData);
var e = new edit("#basicForm", {bucket: "adpic"});
e.on("validate", function () {    
    if ($.trim($('#caption').val()) === ''){
        return "名称不能为空";
    }
    
    return true;
});
e.init();

$(document).on('click','#save_banner',function () {
    e.submit($("#basicForm").attr("action"), function (option) {
        var select=tabTree.getAddress();  // 获取选择的品类信息     
        option.data.maxSortId = select[0]?select[0].id:0;
        option.data.middleSortId = select[1]?select[1].id:0;
        option.data.smallSortId = select[2]?select[2].id:0;
        option.data.sortId = select[3]?select[3].id:0;

        option.data.style = option.data.style ? option.data.style.replace(/\|/g,',') : "0";  // 后台存放的风格分隔符为逗号       如果未选择则默认为0
        option.data.sizeId = option.data.sizeId ? option.data.sizeId : 0;  //如果未选择则默认为0
        option.data.brand = option.data.brand ? option.data.brand : 0;  //如果未选择则默认为0
        option.data.price = option.data.price ? option.data.price : 0;  //如果未选择则默认为0
        option.data.color = option.data.color ? option.data.color : 0;  //如果未选择则默认为0
        //option.data.price = option.data.price ? option.data.price : 0;  //如果未选择则默认为0
        
        selectChange.changeTempletValue(option.data);

        option.success = function (res) {
            if (res.code == "200") {
                e.$tip("提交成功", function () {
                    location.href = "/search/bannerIndex"
                }, 'growl-success');
            } else {
                e.$tip(res.message);
            }
            return false;
        };
        option.error = function (res) {
            e.$tip(res.message);
        };
        console.log(option.data);
    });
    return false;

});

/*品类初始化*/
var SORTDATA={
    url:"/operations/search/querySortBySortId",
    datas:[
        {id:mainData.maxSortId, sortName:mainData.maxSortName},
        {id:mainData.middleSortId, sortName:mainData.middleSortName},
        {id:mainData.smallSortId, sortName:mainData.smallSortName},
        {id:mainData.sortId, sortName:mainData.sortName}
    ],
    otherParam:function(){
        return {}
    },
    complete:function(data, bool){
        if(!bool){
            new common.dropDown({
                el: "#sizeId",
                ajax: "sortsize2",
                params: function(){
                    return {
                        "sortId" : Bll.validateSort()
                    };
                }
            });   // 重置尺码下拉框 并将原来的选择的尺码清空
            $('#select2-sizeId-container').attr('title','');
            $('#select2-sizeId-container').html('');
            // $('#sizeId').find('option[selected]').remove();
            $("#sizeId").val(-1);
        }
    },
};

new common.dropDown({
    el: "#sizeId",
    ajax: "sortsize2",
    params: function(){
        return {
            "sortId" : mainData.smallSortId
        };
    }
});

var tabTree=new common.tabTree("#sortTree",SORTDATA);
tabTree.isfeeze=false;
tabTree.init(SORTDATA.datas);

// 初始化品类
/*公共模块*/
var Bll={
    /*判断类目级别,如果符合三级四级,返回对应id*/
    validateSort:function(){
        var id=false;
        var select=tabTree.getAddress();
        if(select.length>2){
            if(select[2]&&select[2].id){
                id=select[2].id;
            }
            if(select[3]&&select[3].id){
                id=select[3].id;
            }
        }
        return id;
    }
};

var selectChange = {
    changeValue : function(curValue){
        if (10 == curValue) {
            $('.logo_div, .url_div').show();
            $('.title_div, .subtitle_div, .intro_div, #keywordDiv').hide();
        } else if (11 == curValue) {
            $('.title_div, .subtitle_div, .intro_div, #keywordDiv').show();
            $('.logo_div, .url_div').hide();
        } else if (12 == curValue) {
            $('.title_div, .subtitle_div, .intro_div, #keywordDiv, .logo_div, .url_div').show();
        } else {
            $('.title_div, .subtitle_div, .intro_div, #keywordDiv, .logo_div, .url_div').hide();
        }
    },
    changeTempletValue : function(dataObj) {
        var curValue = dataObj.templetId;
        if (10 == curValue ) {
            dataObj.data_title = null;
            dataObj.data_subtitle = null;
            dataObj.data_intro_div = null;
        } else if (11 == curValue) {
            dataObj.data_keywordStr = tableTtem.getTableItemListStr('templateKeywordTable', 'word', 'url');
            dataObj.logo = null;
            dataObj.data_url = null;
        } else if (12 == curValue) {
            dataObj.data_keywordStr = tableTtem.getTableItemListStr('templateKeywordTable', 'word', 'url');
        }
    }
};


new common.dropDown({el:"#positionId", ajax:"searchPostion", params:function(){
    return {positionId:$("#positionId").val()};
}});


selectChange.changeValue(mainData.templetId);  // 初始化
new common.dropDown({el:"#templetId",ajax:"searchTemplet",params:function(){
    return {templetId:$("#templetId").val()};
}});
$('#templetId').on('change', function(){
    var curValue = $("#templetId").val();
    selectChange.changeValue(curValue);
});

var styleEidt = new common.edit('#styleDiv');
// 初始化风格的多选框
common.util.__ajax({
    url: '/operations/search/queryAllStyle',
    data: {
        //categoryId: basicInfo.smallSortId
    }
},function(res){
        var data = res;
        data.selectStyle = mainData.style ? mainData.style.replace(/,/g, "|") : '';
        $('#styleDiv').html(common.util.__template2($('#templateStyle').html(), data));
        styleEidt.init();
},true);

var eColor = new common.edit('#colorDiv');
// 初始化颜色单选选项列表
common.util.__ajax({
    url: '/basegoods/queryProductColors',
    data: {
    }
},function(res){
        var colorList = res.data;
        colorList.selectColor = mainData.color;
        $('#colorDiv').html(common.util.__template2($('#templateColor').html(), colorList));
        eColor.init();
},true);


// 初始化关键字列表
 $('#keywordDiv').html(common.util.__template2($('#templateKeyword').html(), mainData.bannerData));

// 添加关键字
 $(document).on('click','.btn-addKeyword',function () {
    $("#templateKeywordTable").append($('#templateKeywordItem').html());
    return false;
});

 // 初始化品牌选项列表
 var brandSelected = {        // 处理选中之前选择的品牌列表
    init : function(selectEdItem, selectEdDivId) {        // 多选的是用逗号分隔
        if (selectEdItem && selectEdItem.length > 0) {
            var arr = selectEdItem.split(',');
            for(var item in arr) {
                $('#' + selectEdDivId).find("option[value='" + arr[item] + "']").attr("selected", "selected");
            }
        }
        //$('#' + selectEdDivId).on('change', function(){

        //});         // 多选框修改事件触发选中的
    }
}
common.util.__ajax({
    url: '/erpproduct/brands/queryShopsBrandsByStatus',
    data: {
    }
},function(res){
        var brandList = res;
        brandList.selectBrand = mainData.brand;
        $('#brandDiv').html(common.util.__template2($('#templateBrandItem').html(), brandList));
        brandSelected.init(mainData.brand, 'brand');
},true);

var tableTtem = {
    getTableItemListStr : function (tableId, ItemNameOne, ItemNameTwo){
        var tableItemObj = $('#' + tableId);
        var attr = [];
        tableItemObj.find('tr').find('input')
        $.each(tableItemObj.find('tr'), function(){
            var tableObj = {};  tableObj.word = $(this).find('input[name=' + ItemNameOne + ']').val(); 
            tableObj.url = $(this).find('input[name=' + ItemNameTwo + ']').val();
            attr.push(tableObj);
        });
        
        return JSON.stringify(attr);
    }
};