index.js 6.53 KB
/**
 * 创建供应商
 */
'use strict';
var $ = require('jquery'),
    dropDown=require('../common/dropDown');
require('../util/jquery.gritter');


    /*
     * status数字转换文字
     * @param: status(type: String, 传入的status数值)
     */
    function statusCovert(status) {
        switch (status) {
            case 100:
                return '暂存';
                break;
            case 200:
                return '审核中';
                break;
            case 300:
                return '通过'; 
                break;
            case 900:
                return '驳回'; 
                break;
        }
    }

    function isFreezeCovert(status){
        switch (status) {
            case 'Y':
                return '已冻结';
                break;
            default:
                return '生效中'; 
                break;
        }
    }

    function operationsCovert(status){
        switch (status) {
            case 1:
                return '创建';
                break;
            case 2:
                return '编辑';
                break;
            case 3:
                return '冻结'; 
                break;
            case 4:
                return '解冻'; 
                break;
        }
    }

var grid=require('../common/grid');
exports.init = function() {

    new dropDown({
        el:'#supplier-name',
        ajax:'supplier'
    });
    new dropDown({el:"#status"});
    new dropDown({el:"#time-sort"});
    new dropDown({el:"#isFreeze"});

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

    var g=new grid({
        el:"#basicTable",
        parms:function(){

            return {
                id:!!~$.trim($('#supplier-name').val())?$('#supplier-name').val():"",
                status:!!~$.trim($('#status').val())?$('#status').val():"",
                timeSort:!!~$.trim($('#time-sort').val())?$('#time-sort').val():"",
                isFreeze:!!~$.trim($('#isFreeze').val())?$('#isFreeze').val():""
            };
        },
        columns:[
            {display:"供应商",name:"supplierName"},
            {display:"更新时间",name:"foundTime"},
            {display:"最近操作",name:"operations",hidden:btnAuthority.columnsHidOper,render:function(item){
                return operationsCovert(item.operations);
            }},
            {display:"状态",name:"status",render:function(item){
                return statusCovert(item.status);
            }},
            {display:"是否生效",name:"isFreeze",hidden:btnAuthority.columnsHidisFr,render:function(item){
                return isFreezeCovert(item.isFreeze);
            }},
            {display:"操作",name:"id",render:function(items){
                var HtmArr=[];
                // 是否冻结 Y-是,N-否
                // 状态 100-暂存,200-审核中,300-通过,900-驳回
                // 最近操作 1-创建,2-编辑, 3-冻结, 4-解冻
                if(btnAuthority.edit){
                    HtmArr.push('<a href="/supplier/infoopt/'+ items.id+'" class="btn btn-info btn-xs">查看</a>');
                }else{
                    HtmArr.push('<a href="/supplier/info/'+ items.id+'" class="btn btn-info btn-xs">查看</a>');    
                }
                if(items.status!="200"){
                    HtmArr.push('<a href="/supplier/update/'+ items.id+'" class="btn btn-success btn-xs btn-edit">编辑</a>');
                   
                }
                //暂存时,有提交
                if(items.status=="100"){
                    HtmArr.push('<a href="JavaScript:;" mid='+items.id+' moperations='+items.operations+' class="btn btn-primary btn-xs btn-submit">提交</a>');
                }
                
                if(btnAuthority.resetlock&&items.isFreeze=="Y"&&items.status=="300"){
                    HtmArr.push('<a href="javascript:;" mid='+items.id+' class="btn btn-warning btn-xs btn-recovery">恢复</a>');
                }
                if(btnAuthority.resetlock&&items.isFreeze=="N"&&items.status=="300"){
                    HtmArr.push('<a href="javascript:;" mid='+items.id+' class="btn btn-danger btn-xs btn-freeze">冻结</a>');
                }
                
                return HtmArr.join('');
            }}
        ]
    })
    
    g.init($("#gridurl").val());

    $("#filter-btn").click(function(){
        g.reload();
    });
    $("#all-btn").click(function(){
        $('#supplier-name').val("-1").trigger("change");
        $('#status').val("-1").trigger("change");
        $('#time-sort').val("-1").trigger("change");
        $('#isFreeze').val("-1").trigger("change");
        g.reload();
    });

    //提交操作
    function __ajax(options,callback){
        $.ajax({
            type: 'POST',
            url: options.url,
            dataType: 'json',
            data:options.data||{},
            success: function(res) {
                res=res.data;
                if (res.code === 200) {
                    $.gritter.add({
                        title: options.msg,
                        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'
                     });
                }
            }
        });
    }

    //提交操作
    $(document).on('click', '.btn-submit', function() {
        var that = this;
        __ajax({
            url:'/supplier/index/status',
            data:{id:$(that).attr("mid"),status:"200",operations:$(that).attr("moperations")},
            msg:'提交成功'
        },function(){
            g.reload();
        });
    });

    //冻结操作
    $(document).on('click', '.btn-freeze', function() {
        var that = this;
        __ajax({
            url:'/supplier/index/status',
            data:{id:$(that).attr("mid"),isFreeze:"Y",operations:"3"},
            msg:'提交成功'
        },function(){
            g.reload();
        });
    });

    //解冻操作
    $(document).on('click', '.btn-recovery', function() {
        var that = this;
        __ajax({
            url:'/supplier/index/status',
            data:{id:$(that).attr("mid"),isFreeze:"N",operations:"4"},
            msg:'提交成功'
        },function(){
            g.reload();
        });
    });

}