index.js 8.74 KB
/**
 * 合同创建
 */
'use strict';
var $ = require('jquery'),
    Handlebars = require('yoho.handlebars');
require('../util/select2');
require('../util/jquery.simplePagination');
require('../util/jquery.gritter');
require('../util/datepicker')($);

exports.init = function() {

    var pageCount = $('#pagination').data('page');

    $('.select2-offscreen').select2();

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

    var authority =JSON.parse($("#btnAuthority").val());
    
    //创建供应商列表hepler
    Handlebars.registerHelper('pactList', function(items, options) {
        var i = 0,
            outHtml = '',
            item,
            status;

        var HtmlArr=[];

        for (i = 0; i < items.length; i++) {
            item = options.fn(items[i]);
            status = items[i].status + '';

            HtmlArr.push('<tr role="row" class="odd">' + item + '<td>' + statusCovert(status) + '</td>');
            HtmlArr.push('<td class="pact-operate operate' + status + '">');
            if(authority.look){
                HtmlArr.push('<a href="/supplier/agreement/info/'+items[i].agreementId+'" class="btn btn-info btn-xs btn-check">查看</a>');
            }
            if(authority.eidt){
                HtmlArr.push('<a href="/supplier/agreement/edit/'+items[i].agreementId+'" class="btn btn-success btn-xs btn-edit">编辑</a>');
            }
            if(authority.xuyue){
                HtmlArr.push('<a href="javascipt:;" class="btn btn-success btn-xs renew-btn">续约</a>');
            }
            if(authority.jieyue){
                HtmlArr.push('<a href="javascript:;" class="btn btn-danger btn-xs relieve-btn">解约</a>');
            }
            if(authority.updatecode){
                HtmlArr.push('<a href="javascipt:;" class="btn btn-success btn-xs modify-btn">修改合同编号</a>');
            }
            HtmlArr.push('</td>');

            outHtml+=HtmlArr.join('');
        }

        return outHtml + '</tr>';
    });

    //初始化列表ajax
    $.ajax({
        type: 'GET',
        url: '/pact/create/list',
        dataType: 'json',
        success: function(res) {
            console.log(res);
            var html = '\{{#pactList list}}' +
                    '<td>\{{agreementName}}<br><br>\{{agreementCode}}</td>' +
                    '<td>\{{supplierName}}<br><br>\{{# brand}}\{{.}}<br>\{{/ brand}}</td>' +
                    '<td>\{{businessModelName}}</td>' +
                    '<td>\{{agreementTypeName}}</td>' +
                    '<td>\{{agreementStartTime}}<br>\{{agreementEndTime}}</td>' +
                    '<td>\{{operationsName}}</td>' +
                '\{{/pactList}}',

                tpl = Handlebars.compile(html);

            $('tbody').html(tpl({
                list: res.data.list
            }));
        }
    });

        //解除合同
    $('tbody').on('click', '.relieve-btn', function() {
        var that = this;
        $('#relieve-pact').modal();

         $('#postBtn').on('click', function() {
            $.ajax({
                type: 'POST',
                url: '/pact/manage/relieve',
                dataType: 'json',
                success: function(res) {
                    if (res.code === 200) {
                        $.gritter.add({
                            title: '操作成功',
                            class_name: 'growl-success',
                            sticky: false,
                            time: '1000',
                            after_open: function() {
                                $('#relieve-pact').modal('hide');
                                $(that).parent().removeClass('status1').addClass('status0')
                                    .siblings('.td-operate').text('解约')
                                    .siblings('.td-status').text('审核中');
                            }
                         });
                    } else {
                        $.gritter.add({
                            title: '操作失败',
                            class_name: 'growl-danger',
                            sticky: false,
                            time: '1000'
                         });
                    }
                }
            });
        });
    });



    //续约
    $('tbody').on('click', '.renew-btn', function() {
        var that = this;
        $('#renew-pact').modal();

        $('#renew-pact-submit').on('click', function() {
            var v = $('#renew-date').val();
            
            if ($.trim(v) === '') {
                $.gritter.add({
                    title: '请填写续约日期',
                    class_name: 'growl-warning',
                    sticky: false,
                    time: '1000'
                 });
            } else {
                $.ajax({
                    type: 'POST',
                    url: '/pact/manage/renew',
                    data: {
                        date: v
                    },
                    dataType: 'json',
                    success: function(res) {
                        if (res.code === 200) {
                            $.gritter.add({
                                title: '操作成功',
                                class_name: 'growl-success',
                                sticky: false,
                                time: '1000',
                                after_open: function() {
                                    $('#renew-pact').modal('hide');
                                    $(that).parent().removeClass('status1').addClass('status0')
                                        .siblings('.td-operate').text('续约')
                                        .siblings('.td-status').text('审核中');
                                }
                             });
                        } else {
                            $.gritter.add({
                                title: '操作失败',
                                class_name: 'growl-danger',
                                sticky: false,
                                time: '1000'
                             });
                        }
                    }
                });
            }
        });
    });

//续约日期
    $('#renew-date').fdatepicker({
        format: 'yyyy-mm-dd'
    });

    
    

    //修改合同
    $('tbody').on('click', '.modify-btn', function() {
        var that = this;
        $('#modify-pact').modal();

        //修改合同 提交
        $('#modify-pact-submit').on('click', function() {
            
            var v = $('#agreement-code').val();

            if ($.trim(v) === '') {
                $.gritter.add({
                    title: '请填写合同编号',
                    class_name: 'growl-warning',
                    sticky: false,
                    time: '1000'
                 });
            } else {
                $.ajax({
                    type: 'POST',
                    url: '/pact/manage/modify',
                    dataType: 'json',
                    success: function(res) {
                        if (res.code === 200) {
                            $.gritter.add({
                                title: '操作成功',
                                class_name: 'growl-success',
                                sticky: false,
                                time: '1000',
                                after_open: function() {
                                    $('#modify-pact').modal('hide');
                                    console.log($(that).parent().siblings('.td-number'));
                                    $(that).parent().siblings('.td-number').find('span').text(v);
                                }
                             });
                        } else {
                            $.gritter.add({
                                title: '操作失败',
                                class_name: 'growl-danger',
                                sticky: false,
                                time: '1000'
                             });
                        }
                    }
                });
            }
            
        });
    });

    
    
    //分页
    $('#pagination').pagination({
        items: pageCount,
        itemsOnPage: 5,
        pages: pageCount,
        edges: 1,
        ellipsePageSet: false,
        onPageClick: function() {

        }
    });
}