var g_volume=100;


//显示底部版权信息
function show_copyright()
{
    document.title = '智能音乐打铃系统'
    document.getElementById("copyright").innerHTML="Copyright © 2022-2024";
}


//生成签名验证字符串
function get_sign_string()
{
    var username = localStorage.getItem("username"); 
    var password = localStorage.getItem("password");
    let md5_1 = md5(password); 
    var req_str = "u="+username+"&p="+md5_1;
    return req_str;
}

function on_sign_out()
{
    localStorage.removeItem("username");
    localStorage.removeItem("password");
    window.location.href="/sign-in.html";
}

//显示提示信息
function showToast(msg,duration)
{  
    duration=isNaN(duration)?3000:duration;  
    var m = document.createElement('div');  
    m.innerHTML = msg;  
    m.style.cssText=
    `position: absolute; top:30%; left:20%; z-index:999999;
    display:flex;align-items:center;justify-content:center;text-align:justify;
    width:60%; min-width:180px;
    background:#000; opacity:0.8;
    height:auto;min-height: 80px;
    color:#fff; line-height:30px; text-align:center; border-radius:4px;`;  
    
    document.body.appendChild(m);  
    setTimeout(function() {  
        var d = 0.5;  
        m.style.webkitTransition = '-webkit-transform ' + d + 's ease-in, opacity ' + d + 's ease-in';  
        m.style.opacity = '0';  
        setTimeout(function() { document.body.removeChild(m) }, d * 1000);  
    }, duration);  
}  


function show_page_doc(md)
{
  $.get(md, function (data) {
    document.getElementById("page-doc").innerHTML = marked.parse(data);
  });
}

function ring_get(want,query,func)
{
  var url = "/api/get?want="+ want +"&"+ query;
  $.getJSON(url, function (json) {
      if(json.result == 0){
        func(json.value);
      }
      else{
        alert("get "+want+"失败："+json.error);
      }
  });

}

function ring_set(want,query,func)
{
  var sign_str = get_sign_string();
  var url = "/api/set?want="+ want +"&"+ sign_str +"&"+ query;
  $.getJSON(url, function (json) {
    if (json.signin == 1) {
      if(json.result == 0){
        func(json.value);
      }
      else{
        alert("set "+wang+"失败："+json.error);
      }
    }
    else {
      alert("用户未登录,需先登录方可操作");
      window.location.href="/sign-in.html";
    }
  });
}

//修改数据操作带上传json数据
function ring_json_set(want,query,json,func)
{
  var sign_str = get_sign_string();
  var post_url = "/api/set?want="+want+"&"+query+"&"+sign_str;
  //ajax上传
  $.ajax({
      url:post_url,
      type:'post',
      data:JSON.stringify(json),
      contentType:'application/json',
      dataType:'json',
      success:function (json) {
        if(json.login == 0){
          window.location.href = '/sign-in.html';           
          return;    
        }
        if(json.result != 0)
          showToast(json.error,1000);
        else
          func(json.value);
      }
  });

}

//修改数据操作带上传json数据
function ring_post_set(want,query,data,func)
{
  var sign_str = get_sign_string();
  var post_url = "/api/set?want="+want+"&"+query+"&"+sign_str;
  //ajax上传
  $.ajax({
      url:post_url,
      type:'post',
      data:data,
      contentType:'application/json',
      dataType:'json',
      success:function (json) {
        if(json.login == 0){
          window.location.href = '/sign-in.html';           
          return;    
        }
        if(json.result != 0)
          showToast(json.error,1000);
        else
          func(json.value);
      }
  });

}





function set_system_clock(strtime){
  var query_str = "time="+strtime;
  ring_set("clock",query_str,function (json){if(json.result == 0){alert("时间设置成功"); } else{ alert("时间设置失败:"+json.error);}});
}

  function gen_time_string(h,m,s){
    h = h > 9 ? h : "0" + h;
    m = m > 9 ? m : "0" + m;
    s = s > 9 ? s : "0" + s;
    var str = h + ":" + m + ":" + s;
    return str;
  }
  function gen_date_string(y,m,d)
  {
    m = m > 9 ? m : "0" + m;
    d = d > 9 ? d : "0" + d;
    var str = y + "-" + m + "-" + d;
    return str;
  }

