import React, { useEffect, useState } from "react";
import { Button, Input, Pagination, Spin } from "antd";
import { getImageUrl } from 'educoder'
import './index.scss';
import { Link } from "react-router-dom";
import {getAllBotCategory, getContentsLikeNameAndFunc} from '../api.js';
import Nodata from '../../forge/Nodata'

function SoftBot(props){
  const {mygetHelmetapi, current_user, history, showLoginDialog} = props;
  const [current, setCurrent] = useState(1);
  const [pageSize, setPageSize] = useState(12);
  const [total, setTotal] = useState(undefined);
  const [activeKet, setActiveKey] = useState(-1);
  const [keyWord, setKeyWord] = useState(undefined);
  const [botList, setBotList] = useState([])
  const [navList, setNavList] = useState([]);
  const [loading, setLoading] = useState(false);

  useEffect(()=>{
    document.title = "Bot市场"
    // 获取分类列表
    getAllBotCategory().then(res=>{
      if(res && res.code === 200){
        setNavList(res.data && res.data.category_list);
      }
    })
  }, [])

  useEffect(()=>{
    // 获取softbot列表
    setLoading(true);
    getContentsLikeNameAndFunc({
      func: activeKet === -1 ? "" : activeKet,
      name: keyWord || "",
      page_no: current,
      page_size: pageSize
    }).then(res=>{
      if(res && res.code === 200){
        setTotal(res.data && res.data.total);
        setBotList(res.data && res.data.bot_list);
      }
      setLoading(false);
    })
  }, [activeKet, keyWord, pageSize, current])

  // 点击左侧类别导航栏
  function changeType(key){
    setActiveKey(key);
  }
  // 分页函数
  function onShowSizeChange(current, pageSize) {
    setCurrent(1);
    setPageSize(pageSize);
  }
  function onChange(pageNumber) {
    setCurrent(pageNumber);
  }

  // 立即注册跳转
  function registerBot(){
    if(current_user && current_user.login){
      history.push('/settings/mybot/new');
    }else{
      showLoginDialog();
    }
  }
  return <div className="softbotBox">
    <div className="softbotHead">
      <div className="softheadContent">
        <img src={getImageUrl(mygetHelmetapi.login_logo_url)} alt="" width={180}/>
        <div className="bigTit mt30">欢迎来到Bot市场</div>
        <div className="bigTit">快来挑选一个心仪的Bot加入到您的项目吧!</div>
        <Button type="primary" style={{width: '152px', height:'40px'}} className="font-18 gotoRegister" onClick={registerBot}>立即注册<i className="iconfont icon-youjiantou font-15"></i></Button>
      </div>
    </div>
    <div className="botListBox">
      <div className="botListNav">
        <div className={`navItem font-16 ${activeKet === -1 ? 'active' : ''}`} onClick={()=>changeType(-1)}>全部分类</div>
        {navList.map((item, index)=>{return <div className={`navItem font-16 ${activeKet === item ? 'active' : ''}`} key={index} onClick={()=>changeType(item)}>{item}</div>})}
      </div>
      <div className="botListCont">
        <Input.Search enterButton={<i className="iconfont icon-sousuo_icon"></i>} placeholder="请输入关键字搜索bot" className="botListSearch" onSearch={(value)=>{setKeyWord(value)}} allowClear style={{marginLeft: '33px', width: '96.5%'}}/>
        <div className="botList">
          {!loading && botList && botList.length > 0 && botList.map((item, index)=>{
            return <div className="oneBotItem mt30" key={index} onClick={()=>{window.location.href=`/softbot/${item.bot_id}`}}>
                <div className="oneItemHead">
                  <div className="oneBotImgBox"><img src={getImageUrl(item.logo)} alt="" className="oneBotImg"/></div>
                  <div className="oneBotLine">
                    <p className="font-16 oneBotName oneBotLine">{item.market_name}</p>
                    <p>{item.developer_name}</p>
                  </div>
                </div>
                <div className="botIntro">{item.market_desc}</div>
                <div className="degreeBox font-16"><i className="iconfont icon-a-zu1404 mr5 font-16"></i>{item.install_num || 0}<span>&nbsp;次</span></div>
              </div>
          })}
          {!loading && botList && !botList.length && <Nodata _html="暂无数据"/>}
          {loading && <Spin size="large" className="botLoading"/>}
        </div>
      </div>
    </div>
    {total > pageSize && <Pagination showSizeChanger onShowSizeChange={onShowSizeChange} showQuickJumper current={current} total={total} pageSize={pageSize} pageSizeOptions={['12','24','36','48']} onChange={onChange} className="botListPagination mt30"/>}
  </div>
}
export default SoftBot;