(function () {

    var u = navigator.userAgent;
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);

    function AppOaFormUtil() {

        this.confirmAssignees = null;
        this.fields = null;
        this.formButton = null;
        this.currentUser = null;
        this.form = null;

        /**
         * 初始化APP数据，H5 -> APP
         * @param dataObj
         */
        this.initAppFormData = function (dataObj) {
            this.confirmAssignees = dataObj.confirmAssignees;
            this.fields = dataObj.fields;
            this.formButton = dataObj.formButton;
            this.currentUser = dataObj.currentUser;

            if (isAndroid) {
                window.android.invokeNative('initFormData', JSON.stringify(dataObj))
            }
        }

        /**
         * 初始化APP附件
         * @param attachments
         */
        this.initAppAttachments = function(attachments) {
            if(!attachments) {
                return;
            }
            if (isAndroid) {
                window.android.invokeNative('echoAccessory', attachments)
            }
        }

        /**
         * 提交回调
         * @param submitStatus
         */
        this.callbackAppSubmitStatus = function(submitStatus) {
            if (isAndroid) {
                window.android.invokeNative('submitState', submitStatus +'');
            }
        }

        /**
         * 返回
         */
        this.goBackApp = function() {
            if (isAndroid) {
                window.android.invokeNative('back');
            }
        }

        /**
         * 提交表单
         * @param btnIndex
         */
        this.submitFormReport = function (btnIndex) {
            document.getElementById('submitFormBtn'+ btnIndex).click();
        }

        /**
         * 设置字段值
         * @param fieldName
         * @param fieldValue
         */
        this.setFieldValue = function (fieldName, fieldValue) {
            var obj = {};
            obj[fieldName] = fieldValue;
            this.form.val('dataForm', obj);
        }

        /**
         * 设置附件
         * @param attachments
         */
        this.setAttachments = function (attachments) {
            this.form.val('dataForm', {attachments: attachments});
        }

        /**
         * 设置抄送人
         * @param setCcs
         */
        this.setCcs = function (ccs) {
            this.form.val('dataForm', {ccs: ccs});
        }

        this.setForm = function (form) {
            this.form = form;
        }

        this.getConfirmAssignees = function () {
            return this.confirmAssignees;
        }

        this.getFields = function () {
            return this.fields;
        }

        this.getFormButton = function () {
            return this.formButton;
        }

        this.getCurrentUser = function () {
            return this.currentUser;
        }

    }

    window.appOaFormUtil = new AppOaFormUtil();

})();