function DocNo($, form) {
    var docNo = {
        /**
         * 获取编码
         * @return {string}
         */
        getNu: function() {
            var startNu = $('#startNu').val();
            var nuLength = $('#nuLength').val();
            var prefixCount = nuLength - startNu.length;
            var prefix = '';
            for(var i = 0; i < prefixCount; i++) {
                prefix += '0';
            }
            return prefix + startNu;
        },

        /**
         * 生成文号示例
         */
        refreshDocNoDemo: function() {
            var template = $('#template').val();
            if(!template) {
                $('#docNoDemo').text('');
                return;
            }

            var date = new Date();
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var day = date.getDate();
            var docNo = template;
            if(docNo.indexOf('yyyy') > -1) {
                docNo = docNo.replace('yyyy', year);
            } else if(docNo.indexOf('yy') > -1) {
                docNo = docNo.replace('yy', year % 100);
            }
            docNo = docNo.replace('MM', month < 10 ? ('0'+ month) : month);
            docNo = docNo.replace('dd', day < 10 ? ('0'+ day) : day);
            var nu = this.getNu();
            docNo = docNo.replace('nn', nu);
            $('#docNoDemo').text(docNo);
        },
        addKeyup: function() {
            var self = this;
            // 模板输入
            $(document).on('keyup', '#template', function() {
                self.refreshDocNoDemo();
            });

            // 开始编号更新
            $(document).on('keyup', '#startNu', function() {
                var value = this.value;
                if(value === '') {
                    top.dialog.msg('开始编号不能为空');
                    return;
                }
                self.refreshDocNoDemo();
            })

            // 长度更新
            $(document).on('keyup', '#nuLength', function() {
                var value = this.value;
                if(value === '') {
                    top.dialog.msg('编号长度不能为空');
                    return;
                }
                self.refreshDocNoDemo();
            })
        },
        addSelect: function() {
            var self = this;
            form.on('select(typeSelect)', function(data) {
                var options = $(data.elem).children();
                var isManual = true;
                for(var i = 0, option; option = options[i++];) {
                    if(option.value == data.value) {
                        if(option.value != 'manual') {
                            isManual = false;
                        }
                        $('#template').val(option.dataset.template);
                        break;
                    }
                }
                self.refreshDocNoDemo();
                if(isManual) {
                    $('#template').removeAttr('lay-verify');
                }
            });
        }
    }
    return docNo;
}

