/**
 * [OElove] (C)2010-2099 oelove.com biz
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2021/04/25 by Tang $
*/
$(function(){
    function OEEmsubject(){
        this.eventRadio(); //单选按钮 赋值
        this.sendTest(); // 提交题目
    }

    OEEmsubject.prototype={
        //单选按钮 赋值
        eventRadio:function(){
            $(document).on('click','[f="btn_oe_radio"]',function(){
                var id = $(this).attr('data-id');
                var val = $(this).find('input[name="itemno"]').val();
                $('#'+id).val(val);
                $(this).siblings('[f="btn_oe_radio"]').find('.oe_radio').removeClass('checked')
                $(this).find('.oe_radio').addClass('checked');
            });
        },
        // 提交题目
        sendTest:function(){
            $(document).on('click','[f="btn_send_test"]',function(){
                var inputArr = $('[name*="sub_"]');
                var arrData = [];
                var strData = '';
                inputArr.each(function(i,e){
                    if($(e).val() == '') return false;
                    var name = $(e).attr('name');
                    var value = $(e).val();
                    arrData.push(name);
                    if (strData.length == 0 || strData == "") {
                        strData += name+"="+encodeURIComponent(value);
                    }
                    else {
                        strData += "&"+name+"="+encodeURIComponent(value);
                    }
                });
                if(inputArr.length > arrData.length){
                    OEUI.toast({
                        text: '请填写全部题目后，再提交'
                    });
                    return false;
                }
                
                var tid = $('#tid').val();
                
                var loading = null;
                var timer = setTimeout(function () {
                    loading = OEUI.showLoading({
                        type:2
                    });
                }, 200);
                
                
                var url = WAP_APPFILE+'&c=emsubject&a=submit'+
                            "&tid="+tid+"&"+strData;
                            
                $.ajax({
                    type: "POST",
                    cache: false,
                    url: url,
                    dataType: "json",
                    success:function(data){
                        var response = data.response;
                        var result = data.result;
                        
                        clearTimeout(timer);
                        loading && loading.hideLoading();
                        timer = null;
                        
                        if (response == '1') {
                            window.location.href = WAP_APPFILE+'&c=mytest&a=result&id='+result;
                        }
                        else {
                            if (result == '' || result == null) {
                                result = '提交失败，请检查';
                            }
                            OEUI.toast({
                                text: result
                            });
                        }
                        
                    },
                    error: function() {
                        clearTimeout(timer);
                        loading && loading.hideLoading();
                        timer = null;
                    }
                });
            });
        }
    }

    new OEEmsubject();
});