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);

  useEffect(()=>{
    getFAQ();
  },[])

  function getFAQ(){
    const url = `/faqs.json`;
    axios.get(url).then(result=>{
      if(result && result.data){
        setData(result.data);
      }
    }).catch(error=>{})
  }
  function content(list){
    return <div>
      <p className="titlecontent">帮助</p>
      <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"} >
      <ShareModal visible={visible} urlValue={window.location.href} onCancel={()=>setVisible(false)}/>
      {
        data && data.length > 0 && (data[0] && data[0].question) ? 
        <Popover content={content(data)} overlayClassName="helpBox" placement={"left"}>
          <div className="feedback">
            <i className="iconfont icon-bangzhu 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>*/}
      <Popover content={shareContent()} overlayClassName="helpBox shareContent" placement={"left"}>
        <div className="consult">
          <i className="iconfont icon-fenxiang1"></i>
        </div> 
      </Popover>
      <div className="gotop">
        <Tooltip title="返回顶部" placement={"right"}>
          <a><i className="iconfont icon-huidaodingbu1"></i></a>
        </Tooltip>
      </div>
    </div>
  )
}

export default SiderBar;
