info.js 9.45 KB
//商家管理
'use strict';
var $ = require('jquery'),
Handlebars = require('yoho.handlebars');

var dropDown = require('../common/dropDown');
var grid = require('../common/grid');
var edit = require('../common/edit');
require('../util/jquery.gritter');


exports.init = function() {
    new edit('#shopLogo').init();

    $('.urlAdd').on('click',
    function() {
        var type = $(this).data('type'),
        inputAdd = '';
        switch (type) {
        case 0:
            inputAdd = '<input type="text" name="homepage" class="form-control" style="margin-bottom:15px;" placeholder="官方网址">';
            break;
        case 1:
            inputAdd = '<input type="text" name="homeurl" class="form-control" style="margin-bottom:15px;" placeholder="其他平台网址">';
            break;
        case 2:
            inputAdd = '<input type="text" name="homeaddresslist" class="form-control" style="margin-bottom:15px;" placeholder="代表实体店">';
            break;
        }
        $('.Add').eq(type).append(inputAdd);
    });
    new dropDown({
        el: "#shopNature"
    });
    new dropDown({
        el: "#shopsType"
    });
    new dropDown({
        el: "#brand-name",
        ajax: "brand"
    });
    $('#modalId').on('click',function(){
        if($('#shopsType').val()!= ''){
            $('#modal-id').modal('show')
        }else{
            alert('请选择店铺模式');
        }
    })
    $('#addBrand').on('click',function() {
        var storeModel = $('#shopsType').val();
        console.log();

        if (storeModel == 1) {
            if($('#updateBrand tbody tr').length>0){
                alert('单品店只能选择一个供应商');
            }else{
                $('#savebrand .checkbox').each(function() {
                    if ($(this).prop('checked')) {
                        var check = $(this).closest('tr').html();
                        var brand = check.split('<td>')[check.split('<td>').length - 2];
                        var supplier = check.split('<td>')[check.split('<td>').length - 1];
                        var table = '<tr><td>' + brand + '<td>' + supplier + '<td><button type="button" class="delete btn btn-danger btn-xs">删除</button></td></tr>';
                        $('#updateBrand tbody').append(table);
                    }
                })
            }
        } else if (storeModel == 2){
            $('#savebrand .checkbox').each(function() {
                if ($(this).prop('checked')) {
                    var check = $(this).closest('tr').html();
                    var brand = check.split('<td>')[check.split('<td>').length - 2];
                    var supplier = check.split('<td>')[check.split('<td>').length - 1];
                    var table = '<tr><td>' + brand + '<td>' + supplier + '<td><button type="button" class="delete btn btn-danger btn-xs">删除</button></td></tr>';
                        console.log(supplier);
                        console.log($('#updateBrand tbody').html().indexOf(supplier));
                        if($('#updateBrand tbody').html().indexOf(supplier)>1){
                            alert('不能重复添加');
                        }else{
                            $('#updateBrand tbody').append(table);
                        }
                }
            })
        }
    });
    var e = new edit("#storeForm");
    e.on("validate",
    function() {});
    if ($('#shopsId').val() > 0) {
        var submit = function(callback) {
            e.submit('/store/update',
            function(option) {
                option.success = function(res) {
                    if (res.data.code == "200") {
                        history.go(-1);
                    } else {
                        e.$tip("提交失败");
                    }
                    return false;
                },
                option.error = function(res) {
                    e.$tip("提交失败");
                }
                callback && callback(option.data);
            });
        }

    } else {
        var submit = function(callback) {
            e.submit('/store/add',
            function(option) {
                option.success = function(res) {
                    if (res.data.code == "200") {
                        history.go(-1);
                    } else {
                        e.$tip("提交失败");
                    }
                    return false;
                },
                option.error = function(res) {
                    e.$tip("提交失败");
                }
                callback && callback(option.data);
            });
        }
    }

    $(".check").click(function() {
        var shopsId = $('#shopsId').val();
        var onCheck = $(this).data('oncheck');
        var homepage = $("input[name='homepage']");
        var hplist = [];
        homepage.each(function(index) {
            hplist.push(homepage[index].value);
        });
        console.log('hplist:'+hplist);
        var homeurl = $("input[name='homeurl']");
        var homeurlist = [];
        homeurl.each(function(index) {
            homeurlist.push(homeurl[index].value);
        });
        console.log('homeurlist:'+homeurlist);
        var homeaddress = $("input[name='homeaddresslist']");
        var homeaddresslist = [];
        homeaddress.each(function(index) {
            homeaddresslist.push(homeaddress[index].value);
        });
        var updateBrand =$('#updateBrand tbody tr');
        var shopRelation=[];
        shopRelation.push('[')
        $.each(updateBrand,function (index) {
             shopRelation.push('{')
             shopRelation.push('"agreementEndTime":'+$('#updateBrand tbody tr').eq(index).find('td').eq(1).find('.agreementendtime').val()+',');
             shopRelation.push('"brandId":'+$('#updateBrand tbody tr').eq(index).find('td').eq(1).find('.brandid').val()+',');
             shopRelation.push('"supplierId":'+$('#updateBrand tbody tr').eq(index).find('td').eq(1).find('.supplierid').val());
            if(index==updateBrand.length-1){
             shopRelation.push('}')
                }else{
             shopRelation.push('},')
            }
        });
        shopRelation.push(']')
        submit(function(data) {
            data.otherUrl= hplist.toString();
            data.websiteUrl = homeurlist.toString();
            data.shopAddress = homeaddresslist.toString();
            data.onCheck = onCheck;
            data.shopsId = shopsId;
            data.shopRelation = shopRelation.join('');
        });
        return false;
    });
    $(function() {
        if ($('#otherUrl').val().split(',').length > 1) {
            var homepage = $('#otherUrl').val().split(',');
            $.each(homepage,
            function() {
                $('#homepage').append('<input value="' + this + '" type="text" name="homepage" class="form-control" style="margin-bottom:15px;">')
            })
        }else{
             $('#homepage').append('<input type="text" value="'+$('#otherUrl').val()+'" name="homepage" class="form-control" style="margin-bottom:15px;">')
        }

        if ($('#websiteUrl').val().split(',').length > 1) {
            var websiteUrl = $('#websiteUrl').val().split(',');
            $.each(websiteUrl,
            function() {
                $('#homeurl').append('<input value="' + this + '" type="text" name="homeurl" class="form-control" style="margin-bottom:15px;">')
            })
        }else{
            $('#homeurl').append('<input type="text" value="'+$('#websiteUrl').val()+'" name="homeurl" class="form-control" style="margin-bottom:15px;">')
        }

        if ($('#shopAddress').val().split(',').length > 1) {
            var shopAddress = $('#shopAddress').val().split(',');
            $.each(shopAddress,
            function() {
                $('#homeaddress').append('<input value="' + this + '" type="text" name="homeaddresslist" class="form-control" style="margin-bottom:15px;">')
            })
        }else{
            $('#homeaddress').append('<input type="text" value="'+$('#shopAddress').val()+'" name="homeaddresslist" class="form-control" style="margin-bottom:15px;">')
        }
    });
    $('#brand-name').on('change',function() {
        $.ajax({
            url: '/supplier/storeadmin/supplierlist',
            dataType: "json",
            type: 'POST',
            data: {
                brandId: $('#brand-name').val()
            },
            success: function(data) {
                var savebrandlist = [];
                var brandselelt = $('#brand-name').find("option:selected").text();
                $.each(data.data.data,function(index) {
                    savebrandlist.push('<tr>');
                    savebrandlist.push('<td><input class="checkbox" type="checkbox"></td>');
                    savebrandlist.push('<td>' + brandselelt + '</td>');
                    savebrandlist.push('<td>' + data.data.data[index].supplierName);
                    savebrandlist.push('<input type="hidden" class="agreementendtime" value="'+data.data.data[index].agreementEndTime+'">');
                    savebrandlist.push('<input type="hidden" class="brandid" value="'+data.data.data[index].brandId+'">');
                    savebrandlist.push('<input type="hidden" class="supplierid" value="'+data.data.data[index].supplierId+'">');
                    savebrandlist.push('</td>');
                    savebrandlist.push('</tr>')
                });
                $('#savebrand tbody').html(savebrandlist.join(''));
            }
        })
    })
    $('#updateBrand').on('click','.delete',function(){
        $(this).parents('tr').remove()
    })
}