//提交试卷
$("#finished_button").on("click", function(){
  layer.confirm('确认要提交吗？', {
    btn: ['确定','取消']
  }, function(){
    $("#exstems_form").find("input[name='savetype']").val("2");
    var exstemidarr = $("#exstems_form").find("input[name='exstemid[]']");
    var exstemtypearr = $("#exstems_form").find("input[name='exstemtype[]']");
    var thereiserror = 0;
    $(exstemidarr).each(function(index, elem) {
      var onestemid = $(elem).val();
      var onetypeid = $(exstemtypearr).eq(index).val();
      if(onetypeid==1){ //单选
        var answernum = $("#exstems_form").find("input[name='stem_"+ onestemid +"_answer']:checked").length;
        if(answernum==0){thereiserror = 1;}
      }else if(onetypeid==2){ //多选
        var answernum = $("#exstems_form").find("input[name='stem_"+ onestemid +"_answer[]']:checked").length;
        if(answernum==0){thereiserror = 1;}
      }else if(onetypeid==3){ //判断
        var answernum = $("#exstems_form").find("input[name='stem_"+ onestemid +"_answer']:checked").length;
        if(answernum==0){thereiserror = 1;}
      }else if(onetypeid==4){ //填空
        $("#exstems_form").find("input[name='stem_"+ onestemid +"_answer[]']").each(function(index2, elem2){
          if($(elem2).val()==""){thereiserror = 1;}
        });
      }else if(onetypeid==5){ //问答
        var answers = $("#exstems_form").find("input[name='stem_"+ onestemid +"_answer']");
        if($(answers).val()==""){thereiserror = 1;}
      }
    });
    if(thereiserror==1){
      layer.msg("您还有题目没有做完", {icon:2});
      return false;
    }
    $("#exstems_form").ajaxSubmit({
      url: '/doexam/question/finishedok', 
      type: 'POST',
      dataType: "json", 
      beforeSend: function(){
        layer.load(0, {shade: [0.2, '#393D49']});
      },
      success: function(data){
        if(data.code=="success"){
          layer.msg("成功", {icon:1}, function(){window.location.href="/doexam/default/index";});
        }else{
          layer.msg(data.msg, {icon:2});
        }
      },
      error: function(){
        layer.msg("系统错误，请联系管理员", {icon:2});
      },
      complete: function(){
        layer.closeAll('loading');
      }
    });
  }, function(){
    //do nothing
  });
  return false;
});

//保存做题进度
$("#savepoints_button").on("click", function(){
  $("#exstems_form").find("input[name='savetype']").val("1");
  $("#exstems_form").ajaxSubmit({
    url: '/doexam/question/finishedok', 
    type: 'POST',
    dataType: "json", 
    beforeSend: function(){
      layer.load(0, {shade: [0.2, '#393D49']});
    },
    success: function(data){
      if(data.code=="success"){
        layer.msg("成功", {icon:1});
      }else{
        layer.msg(data.msg, {icon:2});
      }
    },
    error: function(){
      layer.msg("系统错误，请联系管理员", {icon:2});
    },
    complete: function(){
      layer.closeAll('loading');
    }
  });
  return false;
});

//检查时间格式
function isDateTime(str)
{
  var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/; 
  var r = str.match(reg); 
  if(r==null)return false; 
  var d= new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]); 
  return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]&&d.getHours()==r[5]&&d.getMinutes()==r[6]&&d.getSeconds()==r[7]);
}

//计算时间差
//结果说明days+"天"+hours+"小时"+minutes+"分钟"+seconds+"秒"
function GetDateDiff(begintime, endtime){
  msg = "";
  var date1=new Date(begintime.replace(/-/g, "/"));
  var date2=new Date(endtime.replace(/-/g, "/"));
  var date3=date2.getTime()-date1.getTime();
  var days=Math.floor(date3/(24*3600*1000));
  var leave1=date3%(24*3600*1000);
  var hours=Math.floor(leave1/(3600*1000));
  var leave2=leave1%(3600*1000);
  var minutes=Math.floor(leave2/(60*1000));
  var leave3=leave2%(60*1000);
  var seconds=Math.round(leave3/1000);
  //alert(days+"天"+hours+"小时"+minutes+"分钟"+seconds+"秒");
  if(days>0){ msg += days+"天"; }
  if(hours>0){ msg += hours+"小时"; }
  if(minutes>0){ msg += minutes+"分钟"; }
  //if(seconds>0){ msg += seconds+"秒"; }
  return msg;
}

//计算剩余时间
function ShowTimeLeft(){
  $.ajax({
    url: '/time/index', 
    type: 'GET',
    dataType: "html", 
    data: {rnum:Math.random()},
    success: function(data){
      if(isDateTime(data)){
        //console.log(data);
        var timeleft = GetDateDiff(data, endtime);
        if(timeleft==""){
          $("#timer_box").find("span").html("考试已结束");
          clearInterval(exam_timer);
        }else{
          $("#timer_box").find("span").html("剩余"+ timeleft);
        }
      }else{
        console.log("获取时间失败");
      }
    },
    error: function(){
      console.log("计时器错误");
    }
  });
}

//显示计时器
ShowTimeLeft();
var exam_timer = setInterval(function (){ShowTimeLeft();}, 30000);