function OaDocNo(layui) {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form
    var restAjax = layui.restajax;

    /**
     * 获取option
     * @param select
     * @param selectedValue
     * @return {null|*}
     */
    function getSelectOption(select, selectedValue) {
        if (!selectedValue) {
            return null;
        }
        var options = $(select).find('option');
        for (var i = 0, option; option = options[i++];) {
            if (option.value === selectedValue) {
                return option;
            }
        }
        return null;
    }

    /**
     * 获取最新文号
     * @param selectId
     * @param docnoId
     * @param isApp
     * @param token
     */
    function getLatestDocNo(selectId, docNoId, isApp, headers) {
        var url;
        if (!isApp) {
            url = 'api/doc-no/get-latest-doc-no/{docNoId}/{docNoUsedId}';
        } else {
            url = 'app/doc-no/get-latest-doc-no/{docNoId}/{docNoUsedId}';
        }
        restAjax.get(restAjax.path(url, [docNoId, 'df_'+ $('#formCode').val() +'_v'+ $('#formVersion').val()]), {}, {
            headers: headers
        }, function (code, data) {
            $('#' + selectId + 'Text').val(data.data);
        }, function (code, data) {
            layer.msg(data.msg);
        })
    }

    // 初始化
    this.init = function (opt) {
        var isApp = opt && opt.isApp ? opt.isApp : false;
        var headers = opt && opt.headers ? opt.headers : {};
        // 获取全部的文号组件
        var docnos = $('.docno');
        if (docnos.length == 0) {
            return;
        }

        // 添加监听事件
        $.each(docnos, function (index, item) {
            var selects = $(item).find('select');
            var select = selects[0];
            var selectId = select.id;
            form.on('select(' + selectId + 'Filter)', function (data) {
                $('#' + selectId + 'Text').val('');
                var docNoId = data.value;
                var option = getSelectOption(data.elem, docNoId);
                if (!option) {
                    return;
                }
                var type = option.dataset.type;
                if (type === 'manual') {
                    $('#' + selectId + 'Text').removeAttr('readonly');
                    return;
                }
                getLatestDocNo(data.elem.id, docNoId, isApp, headers);
            });
        });
    }

    /**
     * 设置文号
     * @param formData
     */
    this.setDocNo = function (formData) {
        var docnos = $('.docno');
        if (docnos.length == 0) {
            return;
        }
        var docNoArray = [];
        $.each(docnos, function (index, item) {
            var selects = $(item).find('select');
            var select = selects[0];
            var docNoId = select.value;
            docNoArray.push({
                fieldName: select.id,
                docNoId: docNoId,
                docNoText: $('#' + select.id + 'Text').val()
            })
            delete formData.field[select.id];
            delete formData.field[select.id + 'Text'];
        });
        formData.field.docNos = docNoArray;
    }

}