const client = require(path.join(__dirname, '../', 'js', 'module', 'client.js'))
var g_data = electron.remote.getGlobal('data');
var getRes

ipcRenderer.on('start', function (event, shareScreen) {
  client.init(g_data.test, g_data.sdkAppId, g_data.userId, g_data.token);
  if (g_data.is_checkin_ing == true) {
    sendIpcMessage('WIN_CHECKIN_DLG:END', 0);
    $("#checkin_bf").hide();
    $("#checkin_res").show();
    getRes = setInterval(checkResultByTime, 1000)
    // $("#checkin_ing").hide();
    // client.getCheckInResult(g_data.classId, g_data.classId.toString(), (ret, json) => {
    //   log.info("[XXX] getCheckInResult->rsp: " + JSON.stringify(json));
    // })
  }
});

$(".tic-btn-close").on('click', function () {
  sendIpcMessage('WIN_CHECKIN_DLG:CLOSE', 0);
});



$("#screen_share").on('click', function () {
  //alert("屏幕分享");
  // 屏蔽屏幕共享功能
  // showAlertMsg("敬请期待");
  sendIpcMessage('WIN_TOOL:CLOSE', 0);
  sendIpcMessage('TOOL:SCREEN', true);
});

function onCheckin() {
  $(".checkin_bf").hide();
}

function onClose() {
  sendIpcMessage('WIN_CHECKIN_DLG:CLOSE', 0);
  $("#checkin_end").show();
  $("#checkin_close").hide();
  clearInterval(getRes);
}

//开始签到
function onCheckinBegin() {
  $("#checkin_bf").hide();
  $("#checkin_res").show();
  getRes = setInterval(checkResultByTime, 1000);

  sendIpcMessage('WIN_CHECKIN_DLG:END', 0);
  g_data.is_checkin_ing = true;
  client.reportEvent(g_data.classId, "apply_check_in", {
    check_id: g_data.classId.toString(),
    time_limit: 30,
  }, (ret, json) => {
    log.info("[XXX] apply_check_in: " + JSON.stringify(json));
  })



}

//结束签到
function onCheckinEnd() {
  client.reportEvent(g_data.classId, "cancel_check_in", {
    check_id: g_data.classId.toString(),
  }, (ret, json) => {
    log.info("[XXX] cancel_check_in->rsp: " + JSON.stringify(json));
    sendIpcMessage('ComWin:Sys', 'close');
  })
  $("#checkin_end").hide();
  $("#checkin_close").show();
  g_data.is_checkin_ing = false;
  // clearInterval(getRes);

}

//定时获取签到结果
function checkResultByTime() {
  log.info("[XXX] checkResultByTime->rsp : ");
  client.getCheckInResult(g_data.classId, g_data.classId.toString(), (ret, json) => {
    $("#res_table").empty();
    log.info("[XXX] getCheckInResult->rsp: " + JSON.stringify(json));
    if (ret == true) {
      $("#all_member_num").text(json.all_number);
      $("#checkin_member_num").text(json.check_in_number);
      var uncheck_number = json.all_number-json.check_in_number
   
      $("#uncheckin_member_num").text(uncheck_number);
      var res = json;
      for (i in res.member_detail) {
        mem_status = res.member_detail[i]
        addRest(client.getQuickUserNick(mem_status.user_id), mem_status.check_state)
      }
    }

  })

}

function addRest(name, check) {
  var check_status;
  if (check == 1){
    check_status = "已签到"
    $("#res_table").append('<tr><td><div class=""><span class="text">'+name+'</span></div></td>\
    <td><div class=""><a href="javascript:;" class="text">'+check_status+'</a></div></td></tr>');
  }
  else{
    check_status = "未签到"
    $("#res_table").append('<tr><td><div class=""><span class="text">'+name+'</span></div></td>\
    <td><div class=""><a href="javascript:;" class="text is-error">'+check_status+'</a></div></td></tr>');
  }
 
}
