// todo
layui.use(['element','form','layer'], function(){
  var element = layui.element;
  var form = layui.form;
  var layer = layui.layer;
  form.render();
});
$(function(){
  // 后台发布产品是有详细内容，前台发布产品包含5个项目，要分标签显示，这个只是产品详细页面要用的代码
  $('#context .item').mouseover(function(){
    $('#context .item').removeClass('current');
    $(this).addClass('current');
    let c=$(this).find('input').val();
    // layui.layer.msg(c);
    switch(c){
      case '1': $('#c1').show(); $('#c2').hide(); $('#c3').hide(); $('#c4').hide(); $('#c5').hide(); break;
      case '2': $('#c1').hide(); $('#c2').show(); $('#c3').hide(); $('#c4').hide(); $('#c5').hide(); break;
      case '3': $('#c1').hide(); $('#c2').hide(); $('#c3').show(); $('#c4').hide(); $('#c5').hide(); break;
      case '4': $('#c1').hide(); $('#c2').hide(); $('#c3').hide(); $('#c4').show(); $('#c5').hide(); break;
      case '5': $('#c1').hide(); $('#c2').hide(); $('#c3').hide(); $('#c4').hide(); $('#c5').show(); break;
    }   
  });
  
  // 控制热门标签刷新换一下
  $('#tag_refresh').click(function(){   
    let res=''
    var loading=null;
      $.ajax({
        type:     "post",
        // url:      "{:url('index/common/get_tag')}",
        url:      "/index/common/get_tag",
        dataType:   "json",
        data:     {}, 
        beforeSend: function(XMLHttpRequest){loading = layui.layer.load(2, {shade: false,time: 15*1000});},
        success:  function(data, textStatus){         
          for(let i=0;i<data.length;i++){
            if(i==0){
              res+='<button class="layui-btn layui-btn-primary layui-btn-sm" style="margin-left:10px;"><a target="_blank" href="/search-1-0-'+data[i]['title']+'.html">'+data[i]['title']+'</a></button>\n';
            }else{
              res+='<button class="layui-btn layui-btn-primary layui-btn-sm"><a target="_blank" href="/search-1-0-'+data[i]['title']+'.html">'+data[i]['title']+'</a></button>\n';
            }           
          }
          $('#tag_refresh').parent().parent().next().html(res);
        },
        complete: function(XMLHttpRequest, textStatus){layui.layer.close(loading);},
        error:    function(){}
      });//end ajax
  });
})

// 发布评论
function fabu(table_id,table_name,content,user_mobile,user_pic){
  if(user_mobile==''){ //"{$Think.cookie.user}"
    layer.msg('请您先登录~~');
    return;
  } 
  if(content==''){
    layui.layer.msg('请您写填写评论内容~~');
    return;
  } 
  var loading=null;
    $.ajax({
      type:     "post",
      // url:      "{:url('index/common_service/set_comment')}",
      url:      "/index/common_service/set_comment",
      dataType:   "text",
      data:     {"table_id":table_id,"table_name":table_name,"content":content},
      beforeSend: function(XMLHttpRequest){loading = layui.layer.load(2, {shade: false,time: 15*1000});},
      success:  function(data, textStatus){
        if(data==1){            
          let res='';
          let m=user_mobile;
          m=m.substr(0,3) + "****" + m.substr(7);
          if(table_name=='meet' || table_name=='activity'){
            res+='<div class="item" style="width:1180px;">';
            res+='<div class="pic">';
            res+='<img src="'+user_pic+'" style=" width: 50px; height: 50px; border-radius: 50%;object-fit: cover;" alt="">';
            res+='</div>';
            res+='<div class="cont" style="width:1100px;">';
            res+='<div class="s1" style="width:1100px;">'+m;
            res+='<span>刚刚</span></div>';
            res+='<div class="s2" style="width:1100px;">'+content+'</div>';
            res+='</div>';
          }else{
            res+='<div class="item">';
            res+='<div class="pic">';
            res+='<img src="'+user_pic+'" style=" width: 50px; height: 50px; border-radius: 50%;object-fit: cover;" alt="">';
            res+='</div>';
            res+='<div class="cont">';
            res+='<div class="s1">'+m;
            res+='<span>刚刚</span></div>';
            res+='<div class="s2">'+content+'</div>';
            res+='</div>';
          }
          res+='<div class="clear"></div>';
          res+='</div>';
          $('#comment-box').prepend(res);
          $('.comment-form').children(0).find('b').text(parseInt($('.comment-form').children(0).find('b').text())+1);         
          $('#content').val('');
          layui.layer.msg('成功~~');
        }else{
          layui.layer.msg('请您先登录~~');
        }
      },
      complete: function(XMLHttpRequest, textStatus){layui.layer.close(loading);},
      error:    function(){}
    });//end ajax
}
// 回复评论
function reply(obj,table_id,table_name,pid,content,user_mobile,user_pic){
  if(user_mobile==''){
    layer.msg('请您先登录~~');
    return;
  }
  if(content==''){
    layui.layer.msg('请您写回复内容~~');
    return;
  }  
  var loading=null;
  $.ajax({
    type:     "post",
    // url:      "{:url('index/common_service/set_reply')}",
    url:      "/index/common_service/set_reply",
    dataType:   "text",
    data:     {"table_id":table_id,"table_name":table_name,"content":content,'pid':pid},
    beforeSend: function(XMLHttpRequest){loading = layui.layer.load(2, {shade: false,time: 15*1000});},
    success:  function(data, textStatus){
      if(data==1){
        let res='';
        let m=user_mobile;
        m=m.substr(0,3) + "****" + m.substr(7);
        if(table_name=='meet' || table_name=='activity'){
          res+='<div class="reply-content" style="display:block; width:1080px;">';
          res+='<div class="r-pic">';
          res+='<img src="'+user_pic+'" style=" width: 50px; height: 50px; border-radius: 50%;object-fit: cover;" alt="">';
          res+='</div>';
          res+='<div class="r-cont" style=" width:1000px;">';
          res+='<div class="r-s1">'+m+' <span>刚刚</span></div>';
          res+='<div class="r-s2">';
        }else{
          res+='<div class="reply-content" style="display:block">';
          res+='<div class="r-pic">';
          res+='<img src="'+user_pic+'" style=" width: 50px; height: 50px; border-radius: 50%;object-fit: cover;" alt="">';
          res+='</div>';
          res+='<div class="r-cont">';
          res+='<div class="r-s1">'+m+' <span>刚刚</span></div>';
          res+='<div class="r-s2">';
        }
        res+=content;
        res+='</div>';
        res+='</div>';
        res+='<div class="clear"></div>';
        res+='</div>';
        // $(obj).parent().after(res);
        $(obj).parent().parent().find('.reply-box').prepend(res);
        $(obj).parent().find('textarea').val('');
        $(obj).parent().hide();
        $(obj).parent().parent().find('.reply-box').slideDown();
        let c=parseInt($(obj).parent().parent().find('.s3').find('.show_reply').find('a').find('span').text());
        c++;
        $(obj).parent().parent().find('.s3').find('.show_reply').find('a').find('span').text(c);
        layui.layer.msg('成功~~');
      }else{
        layui.layer.msg('请您先登录~~');
      }
    },
    complete: function(XMLHttpRequest, textStatus){layui.layer.close(loading);},
    error:    function(){}
  });//end ajax
}
let now="";
// 这里的id是要点赞的评论条目id
function set_comment_good(obj,id,user_mobile){
  if(now==''){
    now=new Date(); 
  }else{
    n=new Date()-now;
    s=n/1000;
    if(s<5){
      layer.msg('你操作太频繁了，休息一下吧~~');
      now=new Date(); 
      return;
    }
    now='';
  } 
  if(user_mobile==''){
    layer.msg('请您先登录~~');
    return;
  } 
  var loading=null;
  $.ajax({
    type:     "post",
    // url:      "{:url('index/common_service/set_comment_good')}",
    url:      "/index/common_service/set_comment_good",
    dataType: "text",
    data:     {"id":id}, 
    beforeSend: function(XMLHttpRequest){loading = layui.layer.load(2, {shade: false,time: 15*1000});},
    success:  function(data, textStatus){
      if(data==1){
        layer.msg('OK');
        $(obj).parent().find('span').text(parseInt($(obj).parent().find('span').text())+1)
        // layer.msg($(obj).parent().find('span').text());
      }else{
        layer.msg('请您先登录~~'); 
      }
    },
    complete: function(XMLHttpRequest, textStatus){layui.layer.close(loading);},
    error:    function(){}
  });//end ajax
}
// 点赞
function good(table_id,table_name,user_mobile){
  if(user_mobile==''){
      layer.msg('请您先登录~~');
      return;
    }   
    var loading=null;
    $.ajax({
      type:     "post",
      // url:      "{:url('index/common_service/set_good')}",
      url:      "/index/common_service/set_good",
      dataType:   "text",
      data:     {"table_id":table_id,"table_name":table_name},
      beforeSend: function(XMLHttpRequest){loading = layui.layer.load(2, {shade: false,time: 15*1000});},
      success:  function(data, textStatus){
        if(data==1){
          $('#btn_good').find('img').attr("src","/static/index/images/good-active.png");
          $('#btn_good').find('span').text(parseInt($('#btn_good').find('span').text())+1);
          layui.layer.msg('感谢点赞~~');
        }else if(data==-1){
          $('#btn_good').find('img').attr("src","/static/index/images/good.png");
          $('#btn_good').find('span').text(parseInt($('#btn_good').find('span').text())-1);
          layui.layer.msg('我们会努力的~~');
        }else{
          layui.layer.msg('请您先登录~~');
        }
      },
      complete: function(XMLHttpRequest, textStatus){layui.layer.close(loading);},
      error:    function(){}
    });//end ajax
}
// 反对
function bad(table_id,table_name,user_mobile){
  if(user_mobile==''){
      layer.msg('请您先登录~~');
      return;
    }    
    var loading=null;
      $.ajax({
        type:     "post",
        // url:      "{:url('index/common_service/set_bad')}",
        url:      "/index/common_service/set_bad",
        dataType:   "text",
        data:     {"table_id":table_id,"table_name":table_name}, 
        beforeSend: function(XMLHttpRequest){loading = layui.layer.load(2, {shade: false,time: 15*1000});},
        success:  function(data, textStatus){
          if(data==1){
            $('#btn_bad').find('img').attr("src","/static/index/images/bad-active.png");
            $('#btn_bad').find('span').text(parseInt($('#btn_bad').find('span').text())+1);
            layui.layer.msg('我们会努力的~~');
          }else if(data==-1){
            $('#btn_bad').find('img').attr("src","/static/index/images/bad.png");
            $('#btn_bad').find('span').text(parseInt($('#btn_bad').find('span').text())-1);
            layui.layer.msg('感谢取消~~');
          }else{
            layui.layer.msg('请您先登录~~');
          }
        },
        complete: function(XMLHttpRequest, textStatus){layui.layer.close(loading);},
        error:    function(){}
        });//end ajax
}

// 收藏PPT  meet_doc_show
function collect_ppt(obj,table_id,table_name,user_mobile){
  if(user_mobile==''){
    layer.msg('请您先登录~~');
    return;
  }  
  var loading=null;
  $.ajax({
    type:     "post",
    // url:      "{:url('index/common_service/set_collect')}",
    url:      "/index/common_service/set_collect",
    dataType: "text",
    data:     {"table_id":table_id,"table_name":table_name}, 
    beforeSend: function(XMLHttpRequest){loading = layui.layer.load(2, {shade: false,time: 15*1000});},
    success:  function(data, textStatus){
        var src="";
        var r=0;
      if(data==1){
        src="/static/index/images/ppt/collect-active.png";
        r=parseInt($(obj).find('span').text())+1;
        $('.btn_collect').find('img').attr("src",src);
        $('.btn_collect').find('span').text(r);
        layui.layer.msg('感谢收藏~~');
      }else if(data==-1){
        src="/static/index/images/ppt/collect.png";
        r=parseInt($(obj).find('span').text())-1;
        $('.btn_collect').find('img').attr("src",src);
        $('.btn_collect').find('span').text(r);
        layui.layer.msg('我们会努力的~~');
      }else{
        layui.layer.msg('请您先登录~~');
      }
    },
    complete: function(XMLHttpRequest, textStatus){layui.layer.close(loading);},
    error:    function(){}
    });//end ajax
}


// 收藏企业名录corporation
function collect_cor(table_id,table_name,user_mobile){
  if(user_mobile==''){
    layer.msg('请您先登录~~');
    return;
  }  
  var loading=null;
  $.ajax({
    type:     "post",
    // url:      "{:url('index/common_service/set_collect')}",
    url:      "/index/common_service/set_collect",
    dataType: "text",
    data:     {"table_id":table_id,"table_name":table_name}, 
    beforeSend: function(XMLHttpRequest){loading = layui.layer.load(2, {shade: false,time: 15*1000});},
    success:  function(data, textStatus){
      if(data==1){
        $('#btn_collect').find('i').removeClass('layui-icon-star').addClass('layui-icon-star-fill');
        layui.layer.msg('感谢收藏~~');
      }else if(data==-1){
        $('#btn_collect').find('i').removeClass('layui-icon-star-fill').addClass('layui-icon-star');
        layui.layer.msg('我们会努力的~~');
      }else{
        layui.layer.msg('请您先登录~~');
      }
    },
    complete: function(XMLHttpRequest, textStatus){layui.layer.close(loading);},
    error:    function(){}
    });//end ajax
}


// 收藏产品详细页面，对应的店铺 user
function collect_user(table_id,table_name,user_mobile){
  if(user_mobile==''){
    layer.msg('请您先登录~~');
    return;
  }  
  var loading=null;
  $.ajax({
    type:     "post",
    // url:      "{:url('index/common_service/set_collect')}",
    url:      "/index/common_service/set_collect",
    dataType: "text",
    data:     {"table_id":table_id,"table_name":table_name}, 
    beforeSend: function(XMLHttpRequest){loading = layui.layer.load(2, {shade: false,time: 15*1000});},
    success:  function(data, textStatus){
      if(data==1){
        $('#btn_collect_user').find('i').removeClass('layui-icon-star').addClass('layui-icon-star-fill');
        layui.layer.msg('感谢收藏~~');
      }else if(data==-1){
        $('#btn_collect_user').find('i').removeClass('layui-icon-star-fill').addClass('layui-icon-star');
        layui.layer.msg('我们会努力的~~');
      }else{
        layui.layer.msg('请您先登录~~');
      }
    },
    complete: function(XMLHttpRequest, textStatus){layui.layer.close(loading);},
    error:    function(){}
    });//end ajax
}





// 收藏 用于meet_doc_sheet.html页面列表中的单个ppt收藏功能,以及顶部会议收藏功能
// 2023-2-8
function collect_doc(obj,table_id,table_name,user_mobile){
  if(user_mobile==''){
    layer.msg('请您先登录~~');
    return;
  }  
  var loading=null;
  $.ajax({
    type:     "post",
    // url:      "{:url('index/common_service/set_collect')}",
    url:      "/index/common_service/set_collect",
    dataType: "text",
    data:     {"table_id":table_id,"table_name":table_name}, 
    beforeSend: function(XMLHttpRequest){loading = layui.layer.load(2, {shade: false,time: 15*1000});},
    success:  function(data, textStatus){
      if(data==1){
        $(obj).find('img').attr("src","/static/index/images/collect-active.png");
        $(obj).find('span').text(parseInt($(obj).find('span').text())+1);
        layui.layer.msg('感谢收藏~~');
      }else if(data==-1){
        $(obj).find('img').attr("src","/static/index/images/collect.png");
        $(obj).find('span').text(parseInt($(obj).find('span').text())-1);
        layui.layer.msg('我们会努力的~~');
      }else{
        layui.layer.msg('请您先登录~~');
      }
    },
    complete: function(XMLHttpRequest, textStatus){layui.layer.close(loading);},
    error:    function(){}
    });//end ajax
}
// 收藏
function collect(table_id,table_name,user_mobile){
  if(user_mobile==''){
    layer.msg('请您先登录~~');
    return;
  }  
  var loading=null;
  $.ajax({
    type:     "post",
    // url:      "{:url('index/common_service/set_collect')}",
    url:      "/index/common_service/set_collect",
    dataType: "text",
    data:     {"table_id":table_id,"table_name":table_name}, 
    beforeSend: function(XMLHttpRequest){loading = layui.layer.load(2, {shade: false,time: 15*1000});},
    success:  function(data, textStatus){
      if(data==1){
        $('#btn_collect').find('img').attr("src","/static/index/images/collect-active.png");
        $('#btn_collect').find('span').text(parseInt($('#btn_collect').find('span').text())+1);
        layui.layer.msg('感谢收藏~~');
      }else if(data==-1){
        $('#btn_collect').find('img').attr("src","/static/index/images/collect.png");
        $('#btn_collect').find('span').text(parseInt($('#btn_collect').find('span').text())-1);
        layui.layer.msg('我们会努力的~~');
      }else{
        layui.layer.msg('请您先登录~~');
      }
    },
    complete: function(XMLHttpRequest, textStatus){layui.layer.close(loading);},
    error:    function(){}
    });//end ajax
}


// 需求详细页面 关注方法，就是收藏方法，由于图片使用了新的，创建新方法，就是把图片文件名修改一下
// 收藏
function gz(table_id,table_name,user_mobile){
  if(user_mobile==''){
    layer.msg('请您先登录~~');
    return;
  }  
  var loading=null;
  $.ajax({
    type:     "post",
    // url:      "{:url('index/common_service/set_collect')}",
    url:      "/index/common_service/set_collect",
    dataType: "text",
    data:     {"table_id":table_id,"table_name":table_name}, 
    beforeSend: function(XMLHttpRequest){loading = layui.layer.load(2, {shade: false,time: 15*1000});},
    success:  function(data, textStatus){
      if(data==1){
        $('#btn_collect').find('img').attr("src","/static/index/images/gz-active.png");
        $('#btn_collect').find('span').text(parseInt($('#btn_collect').find('span').text())+1);
        layui.layer.msg('感谢收藏~~');
      }else if(data==-1){
        $('#btn_collect').find('img').attr("src","/static/index/images/gz.png");
        $('#btn_collect').find('span').text(parseInt($('#btn_collect').find('span').text())-1);
        layui.layer.msg('我们会努力的~~');
      }else{
        layui.layer.msg('请您先登录~~');
      }
    },
    complete: function(XMLHttpRequest, textStatus){layui.layer.close(loading);},
    error:    function(){}
    });//end ajax
}

// 列表收藏方法 2023-6-4
function sheet_collect(obj,table_id,table_name,user_mobile){
  if(user_mobile==''){
    layer.msg('请您先登录~~');
    return;
  }  
  var loading=null;
  $.ajax({
    type:     "post",
    // url:      "{:url('index/common_service/set_collect')}",
    url:      "/index/common_service/set_collect",
    dataType: "text",
    data:     {"table_id":table_id,"table_name":table_name}, 
    beforeSend: function(XMLHttpRequest){loading = layui.layer.load(2, {shade: false,time: 15*1000});},
    success:  function(data, textStatus){
      if(data==1){
        $(obj).find('img').attr("src","/static/index/images/collect-active.png");
        $(obj).find('span').text(parseInt($(obj).find('span').text())+1);
        layui.layer.msg('感谢收藏~~');
      }else if(data==-1){
        $(obj).find('img').attr("src","/static/index/images/collect.png");
        $(obj).find('span').text(parseInt($(obj).find('span').text())-1);
        layui.layer.msg('我们会努力的~~');
      }else{
        layui.layer.msg('请您先登录~~');
      }
    },
    complete: function(XMLHttpRequest, textStatus){layui.layer.close(loading);},
    error:    function(){}
    });//end ajax
}


// 举报
function report(table_id,table_name,user_mobile){
  if(user_mobile==''){
      layer.msg('请您先登录~~');
      return;
    }
    layer.open({
      type:1,
      title:'我要举报',
      content: $('#mybox'),
      area: ['500px', '340px'],
      btn:['确认提交'],
      yes:function(index,layerno){
        let content=$('#report_content').val();
        let phone=$('#report_phone').val();
        if(content==''){
          layer.msg('举报问题必填~~');
          return;
        }
        if(phone==''){
          layer.msg('联系方式必填~~');
          return;
        }
        write_report(table_id,table_name,content,phone);
        $('#report_content').val('');
        $('#report_phone').val('');
        layer.close(index);
      }
    });
}
// 举报写入数据库
function write_report(table_id,table_name,content,phone){
  var loading=null;
  $.ajax({
    type:     "post",
    // url:      "{:url('index/common_service/set_report')}",
    url:      "/index/common_service/set_report",
    dataType: "text",
    data:     {"table_id":table_id,"table_name":table_name,"content":content,"phone":phone},
    beforeSend: function(XMLHttpRequest){loading = layui.layer.load(2, {shade: false,time: 15*1000});},
    success:  function(data, textStatus){
      if(data==1){
        $('#btn_report').find('span').text(parseInt($('#btn_report').find('span').text())+1);
        layer.msg('感谢您的举报，我们会及时改进，谢谢您~~');
      }else{
        layer.msg('请您先登录');
      }
    },
    complete: function(XMLHttpRequest, textStatus){layui.layer.close(loading);},
    error:    function(){}
  });//end ajax
}
// 分享数据写入数据库
function go_share(table_id,table_name,m){
  var loading=null;
  $.ajax({
    type:     "post",
    // url:      "{:url('index/common_service/set_share')}",
    url:      "/index/common_service/set_share",
    dataType: "text",
    data:     {"table_id":table_id,"table_name":table_name,"method":m},
    beforeSend: function(XMLHttpRequest){loading = layui.layer.load(2, {shade: false,time: 15*1000});},
    success:  function(data, textStatus){
      if(data==1){
        $('#btn_share').find('span').text(parseInt($('#btn_share').find('span').text())+1);
        share_to(m);
      }
    },
    complete: function(XMLHttpRequest, textStatus){layui.layer.close(loading);},
    error:    function(){}
    });//end ajax
}
// 分享函数
function share_to(stype){
    var ftit = '';
    var flink = '';
    var lk = '';
    //获取文章标题
    ftit = document.title;
    //获取网页中内容的第一张图片地址作为分享图
    flink = document.images[0].src;
    if(typeof flink == 'undefined'){
        flink='';
    }
    //当内容中没有图片时，设置分享图片为网站logo
    if(flink == ''){
        lk = 'http://'+window.location.host+'/static/index/images/logo.jpg';
    }
    //如果是上传的图片则进行绝对路径拼接
    if(flink.indexOf('/uploads/') != -1) {
        lk = 'http://'+window.location.host+flink;
    }
    //百度编辑器自带图片获取
    if(flink.indexOf('ueditor') != -1){
        lk = flink;
    }
    //qq空间接口的传参
    if(stype=='qzone'){
        window.open('https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url='+document.location.href+'?sharesource=qzone&title='+ftit+'&pics='+lk+'&summary='+document.querySelector('meta[name="description"]').getAttribute('content'));
    }
    //新浪微博接口的传参
    if(stype=='sina'){
        window.open('http://service.weibo.com/share/share.php?url='+document.location.href+'?sharesource=weibo&title='+ftit+'&pic='+lk+'&appkey=2706825840');
    }
    //qq好友接口的传参
    if(stype == 'qq'){
        window.open('http://connect.qq.com/widget/shareqq/index.html?url='+document.location.href+'?sharesource=qzone&title='+ftit+'&pics='+lk+'&summary='+document.querySelector('meta[name="description"]').getAttribute('content')+'&desc=中冶有色技术网');
    }
    //生成二维码给微信扫描分享，php生成，也可以用jquery.qrcode.js插件实现二维码生成
    if(stype == 'wechat'){
        window.open('https://api.qrserver.com/v1/create-qr-code/?size=300x300&data='+document.location.href);
    }
}