index.js 3.98 KB
/**
 * 品牌管理
 */
'use strict';
var $ = require('jquery'),
    Handlebars = require('yoho.handlebars'),
    grid = require('../common/grid'),
    dropDown = require('../common/dropDown');

require('../util/jquery.simplePagination');
require('../util/datepicker')($);
require('../util/jquery.gritter');

exports.init = function() {

    //下拉框
    new dropDown({el: "#status"});
    new dropDown({el: "#brand-level"});
    new dropDown({
        el:'#brand-id',
        ajax:'brand'
    });
    
    //日期插件
    $('.hasDatepicker').fdatepicker({
        format: 'yyyy-mm-dd'
    });


    var btnAuthority=JSON.parse($("#btnAuthority").val());

    var g = new grid({
        el: '#basicTable',
        size: 50,
        parms: function() {

            return {
                brandId: $.trim($('#brand-id').val()) || '',
                status: $.trim($('#status').val()) || '',
                brandLevel: $.trim($('#brand-level').val()) || '',
                startTime: $.trim($('#starttime').val()) || '',
                endTime: $.trim($('#endtime').val()) || '',
            };
        },
        columns: [
            {display: "LOGO", name: "brandIco",render:function(item){
                return '<img src="'+item.brandIco+'"/>'
            }},
            {display: '品牌名称', name: "brandName"},
            {display: '英文名称', name: "brandNameEn"},
            {display: '品牌级别', name: "brandLevel"},
            {display: '调性分', name: "brandTonality"},
            {display: '创建时间', name: "createTime"},
            {display: '操作', name:"status", render: function(items) {
                var HtmArr = [];

                if (btnAuthority.edit) {
                    HtmArr.push('<a href="/erpproduct/brands/edit/'+ items.id+'"" class="btn btn-info btn-xs">编辑</a>');
                }
                if (items.status === 0) {
                    HtmArr.push('<a data-id="' + items.id + '" href="JavaScript:;" class="btn btn-warning btn-xs open-brand">开启品牌</a>');
                }
                if (items.status === 1) {
                    HtmArr.push('<a data-id="' + items.id + '" href="JavaScript:;" class="btn btn-danger btn-xs close-brand">关闭品牌</a>');
                }
                return HtmArr.join('');
            }}
        ]
    })
    
    g.init($("#gridurl").val());

    $("#filter-btn").click(function(){
        g.reload();
    });

    function operateAjax(options, callback) {
        $.ajax({
            type: 'POST',
            url: options.url,
            data: {
                id: options.id,
                status: options.status
            },
            dataType: 'json'
        }).then(function(res) {
            if (res.code === 200) {
                $.gritter.add({
                    title: '操作成功',
                    class_name: 'growl-success',
                    sticky: false,
                    time: '1000',
                    after_open: function() {
                        callback && callback();
                    }
                });
            } else {
                $.gritter.add({
                    title: '操作失败',
                    class_name: 'growl-danger',
                    sticky: false,
                    time: '1000'
                 });
            }
        })
    }

    //关闭品牌操作
    $('tbody').on('click', '.close-brand', function() {
        var that = this,
            id = $(that).data('id');

        $(that).attr('disabled', true);

        operateAjax({
            url: '/brand/edit',
            id: id,
            status: 0
        }, function() {
            g.reload();
        })
    });

    //开启品牌操作
    $('tbody').on('click', '.open-brand', function() {
        var that = this,
            id = $(that).data('id');

        $(that).attr('disabled', true);
        
        operateAjax({
            url: '/brand/edit',
            id: id,
            status: 1
        }, function() {
            g.reload();
        })
    });


    
}