Edit.js 8.03 KB
//商家管理
'use strict';
var $ = require('jquery'),
    common=require('../../common/common');
var e=new common.edit("#storeForm",{bucket:"yhb-img01"});

var Bll={
    toast:function(content,url,id){
        common.dialog.confirm("温馨提示",content,function(){
            common.util.__ajax({
                url:url,
                data:{shopsId:id}
            },function(){
                g.reload();
            });
        });
    },
    addInput:function(name,label,value){
        var $input=$("<input />");
        $input.attr("type","text");
        $input.attr("name",name);
        $input.attr("placeholder",label.replace(':',''));
        $input.addClass("form-control");
        $input.css("margin-bottom","15px");
        value&&$input.val(value);
        $("#"+name+"-list").append($input);
    }
}

var g=new common.grid({
    el:"#baseTable",
    hash:false,
    parms:function(){
        return {brandId: $('#brandname').val()}
    },
    columns:[
        {display:"选择","type":"checkbox"},
        {display:"品牌",render:function(item){
            //option js
            item.brandName=$.trim($("#brandname").find("option:selected").text().replace(/选择品牌/g,''));
            return item.brandName;
        }},
        {display:"供应商",name:"supplierName"}
    ]
});

var g2=new common.grid({
    el:"#updateBrand",
    hash:false,
    columns:[
        {display:"品牌","name":"brandName"},
        {display:"供应商","name":"supplierName"},
        {display:"操作",render:function(item){
            if(item.method == "add"){
                return '<button type="button" data-index="'+item.__index+'" class="delete btn btn-danger btn-xs">删除</button>';
            }else{
                return '';
            }
        }}
    ]
});
g2.__rows=[],g2.__delrows=[];

e.on("render",function(){
    $("#otherUrl").add("#websiteUrl").add("#shopAddress").each(function(){
        var label=$(this).parents(".form-group").find("label").text();
        var name=$(this).attr("id");
        var value=$(this).val()?$(this).val():'[]';

        var data=JSON.parse(value);
        if(data.length>0){
            $.each(data,function(i,item){
                Bll.addInput(name,label,item);
            });
        }else{
            Bll.addInput(name,label);
        }
    });

    (!!~$("#shopsType").val())?$("#modalId").prop("disabled",false):$("#modalId").prop("disabled",true);
    //修改
    if($("#shopRelationList").val()){
        var a=JSON.parse($("#shopRelationList").val());
        //编辑的时候删除delete
        $.each(a,function(index,item){
            if(item.method!="delete"){
                g2.__rows.push(item);
            }else{
                g2.__delrows.push(item);
            }
        });
        // Array.prototype.push.apply(g2.__rows, a);
        g2.init(g2.__rows);
    }
});

e.on("bind",function(){
    $(".urlAdd").click(function(){
        var label=$(this).parents(".form-group").find("label").text();
        var name=$(this).parent("div").next(":hidden").attr("id");
        Bll.addInput(name,label);
    });

    $("#shopsType").change(function(){
        
        if(!!~$("#shopsType").val()){
            $("#modalId").prop("disabled",false);
        }else{
            $("#modalId").prop("disabled",true);
        }
        if(g2.grid){
            g2.__rows.length=0;
            g2.reload();
        }
    });

    $("#modalId").click(function(){
        var __feild="brandId";
        if((+$("#shopsType").val()==1)){
            g.options.columns[0].type="checkbox";
            __feild="brandId";
            g2.__rows.length=0;
        }else{
            g.options.columns[0].type="checkbox";
            // __feild="supplierId";
            __feild="unqid";
        }
        //common.util.__template($("#template").html(),{})
        common.dialog.confirm("添加供应商关系",$("#template").html(),function(){
            $.each(g.selected,function(index,item){
                //item.supplierId
                var i=0;
                $.each(g2.__rows,function(index1,item1){
                    // if(__feild=="unqid"){
                    //     if((item1["brandId"]+"_"+item1["supplierId"])==(item["brandId"]+"_"+item["supplierId"])){
                    //         i++;
                    //     }
                    // }else{
                    //     if(item1["brandId"]==item["brandId"]){
                    //         i++;
                    //     }
                    // }
                    if((item1["brandId"]+"_"+item1["supplierId"])==(item["brandId"]+"_"+item["supplierId"])){
                        i++;
                    }
                });
                if(i==0){
                    item.method="add";
                    g2.__rows.push(item);
                }
            });
            g2.init(g2.__rows);
        });
        new common.dropDown({
            el: "#brandname",
            ajax: "brand",
            params:{status:1}
        });
        g.init("/supplier/storeadmin/supplierlist");
    });

    $(document).on("change","#brandname",function(){
        g.reload();
    });

    $("#updateBrand").on("click",".delete",function(){
        g2.__rows.splice($(this).data("index"),1);
        g2.reload();
    });
});

var _arr={
    websiteUrl:"[]",
    otherUrl:"[]",
    shopAddress:"[]"
};
var urlRegx="[a-zA-z]+://[^s]*";
var _regx={
    websiteUrl:urlRegx,
    otherUrl:urlRegx
};

e.on("validate",function(){
    if(+$("#shopNature").val()==-1){
        return "请选择店铺类型";
    }
});

e.on("validate",function(){
    _arr={
        websiteUrl:[],
        otherUrl:[],
        shopAddress:[]
    };
    var _count=0;
    for(var key in _arr){
        $("#"+key+"-list").find("input:text").each(function(){
            var _v=$.trim($(this).val());
            if(_v){
                if(_regx[key]){
                    var url=new RegExp(_regx[key]);
                    if(!url.test(_v)){
                        
                        _count++;
                       return;
                    }
                }
                _arr[key].push(_v);
            }
        });
        _arr[key]=JSON.stringify(_arr[key]);
    }
    if(_count>0){
        return "网址输入错误";
    }
});

e.on("validate",function(){
    var i=0;
    for (var key in g2.rows) {
        i++;
    }
    if(!i){
        return "请选择关联品牌";
    }

    if($("#shopRelationList").val()){
        var _l1=JSON.parse($("#shopRelationList").val());
        var _l2=g2.rows;
        var map={};
        $.each(_l2,function(index,item){
            var __id=item.brandId+"-"+item.supplierId;

             map[__id]=item;
        });
        $.each(_l1,function(index,item){
            var __id=item.brandId+"-"+item.supplierId;
            if(map[__id]){
                //存在
                // item.method="";
                map[__id]=item;
            }else{
                //删除
                item.method="delete";
                map[__id]=item;
            }
        });
        var __shopRelation=[];
        $.each(map,function(index,item){
            __shopRelation.push(item);
        });

        _arr.shopRelation=JSON.stringify(__shopRelation);

    }else{
        _arr.shopRelation=JSON.stringify(g2.__rows);
    }
    
});

e.init();

var submit=function(callback){
    e.submit($("#storeForm").attr("action"),function(option){
        option.data=$.extend({},option.data,_arr);
        console.log(option.data);
        option.success=function(res){
            if(res.code=="200"){
                e.$tip('提交成功',function(){
                  //location.href="/supplier/store/index";
                  history.go(-1);
                },'growl-success');
            }else{
                e.$tip(res.message);
            }
            return false;
        },
        option.error=function(res){
            e.$tip("提交失败");
        }
        callback&&callback(option.data);
    });
};

//提交
$(".btn-success").click(function(){
    submit(function(item){
        item.onCheck=1;
    });
});
//暂存
$(".btn-cun").click(function(){
    submit(function(item){
        item.onCheck=0;
    });
});