import React, { useEffect, useState } from 'react';
import { Popover , Tooltip } from 'antd';
import './Component.scss';
import axios from 'axios';
import ShareModal from './SiderBarShareModal';

const $ = window.$;

$(window).scroll(function () {
  if ($(".gotop").length > 0) {
    if ($(document).scrollTop() > 0) {
      $(".-task-sidebar .gotop").show();
      $(".gotop").click(function () {
        $("html,body").scrollTop(0);
      });
    }
    if ($(document).scrollTop() === 0) {
      $(".-task-sidebar .gotop").hide();
    }
  }
});

function SiderBar() {
  const [ data , setData ] = useState([]);
  const [ visible , setVisible ] = useState(false);
  const [ login , setLogin ]= useState(false);

  useEffect(()=>{
    getFAQ();
    getCurrentUser();
    //页面加载完成之后隐藏回到顶点
    $(".-task-sidebar .gotop").hide();
  },[])

  function getFAQ(){
    const url = `/faqs.json`;
    axios.get(url).then(result=>{
      if(result && result.data){
        setData(result.data);
      }
    }).catch(error=>{})
  }

  //获取当前登录账号信息->用于建议反馈
  function getCurrentUser(){
    let url = `/users/get_user_info.json`;
    axios.get(url).then((response) => {
      if (response && response.data && response.data.login) {
        setLogin(response.data.login);
      }
    }).catch(error=>{})
  }

  function content(list){
    return <div>
      <ul className="faqUl">
        {
          list && list.map((i,k)=>{
            return(
              <li key={i.question+k}><a href={`${i.url}`} title={i.question} target="_blank">{i.question}</a></li>
            )
          })
        }
      </ul>
    </div>
  }

  function shareContent(){
    return <div>
      <ul className="shareUl">
        <p className="titlecontent">分享到</p>
        <li onClick={()=>setVisible(true)}><i className="iconfont icon-weixin2" style={{color:"#62b900"}}></i></li>
      </ul>
    </div>
  }

  return (
    <div className={"-task-sidebar"} >
      {/* 平台反馈 */}
      <Tooltip title="意见反馈" placement={"left"} overlayClassName="tooltipBox">
        <div className="consult">
          <a href={login ? `/Gitlink/forgeplus/issues/new?type=feedback`:`/login?go_page=/Gitlink/forgeplus/issues/new?type=feedback`}>
            <i className="iconfont icon-yijianfankui2"></i>
          </a>
        </div>
      </Tooltip>

      {/* 帮助 */}
      {
        data && data.length > 0 && (data[0] && data[0].question) ? 
        <Popover title="帮助" content={content(data)} overlayClassName="helpBox" placement={"left"}>
          <div className="feedback">
            <i className="iconfont icon-bangzhu1 font-22"></i>
          </div>
        </Popover>
        :""
      }

      {/* 分享 */}
      {/* <div className="scan pr" title="微信扫一扫">
        <span className="inline erweima"><i className="iconfont icon-erweima color-white font-22 fl"></i></span>
      </div>*/}
      <ShareModal visible={visible} urlValue={window.location.href} onCancel={()=>setVisible(false)}/>
      <Popover content={shareContent()} overlayClassName="helpBox shareContent" placement={"left"}>
        <div className="consult">
          <i className="iconfont icon-fenxiang"></i>
        </div> 
      </Popover>

      {/* 返回顶部 */}
      <Tooltip title="返回顶部" placement={"left"} overlayClassName="tooltipBox">
        <div className="gotop">
          <a><i className="iconfont icon-huidaodingbu1"></i></a>
        </div>
      </Tooltip>
    </div>
  )
}

export default SiderBar;
