import React, {Component} from 'react'
import _ from 'lodash'
import css from './content.scss'
import Tab from '../common/tab/tab'
import SysContent from "./sys/sys";
import TodoContent from "./todo/todo";
import AllContent from "./all/all";
import MsgContent from "./message/message";
import TelContent from "./address/address";
import FriendContent from "./friendCircle/friendCircle";
import NewsContent from "./news/news";
import KnowledgeContent from "./knowledge/knowledge";

class KaraSearch extends Component {
  constructor(props) {
    super(props);
    this.state = {
      activeTab: props.tab,
      // activeTab: props.initTab,
      searchCount: {},
      pageNum: 1,
      pageSize: 20,
      clickTime: 0,
      msgType: '',
      searchTime: {
        type: null,
        label: null,
        start: '',
        end: '',
      }
    }
    this.contentRef = React.createRef()
  }

  //获取点击时间戳，用于重新渲染tab，出现滚动条
  getScroll = () => {
    //获取当前时间
    let timestamp = new Date().getTime();
    setTimeout(() => {
      this.setState({
        clickTime: timestamp
      });
    }, 10)
  }

  changeActiveTab = (activeTab, msgType = '') => {
    setTimeout(() => {
      this.setState({
        pageNum: 1,//切换tab，请求页码从1开始
        activeTab: activeTab,
        msgType: msgType
      });
    }, 10)

    const {searchTab} = this.props;
    searchTab(activeTab);

  }

  setSearchCount = (searchCount) => {
    this.setState({searchCount})
  }

  clearSearchCount = () => {
    this.setState({
      searchCount: {},
      pageNum: 1,
    })
  }

  //到底部后加载新的数据
  getMoreData = (active) => {
    //  更改页号
    const {pageNum} = this.state;
    // console.log("pageNum", pageNum)
    this.setState({
      pageNum: pageNum + 1
    })

  }

  // 修改搜索时间
  changeSearchTime = (searchTime) => {
    this.setState({searchTime, pageNum: 1})
  }


  //获取tab对应数据
  getTabData = () => {
    const {setSearchCount, changeActiveTab, clearSearchCount, getScroll, changeSearchTime} = this
    const {kw, searchEnable, httpAgent, lang, history, karaui, closeSearchModal, user, hideContent, onClose, disableAuth} = this.props;
    const {searchCount, pageNum, pageSize, msgType, searchTime} = this.state
    const {all = 0, sys = 0, todo = 0, msg = 0, tel = 0, friend = 0, news = 0, knowledge = 0} = searchCount
    const publicProps = {
      onClose,
      kw,
      searchEnable,
      httpAgent,
      clearSearchCount,
      lang,
      karaui,
      closeSearchModal,
      user,
      history,
      searchTime,
      changeSearchTime
    }
    const tabData = [
      {
        type: "all",
        title: lang.all,
        count: all,
        child: <AllContent {...publicProps} changeActiveTab={changeActiveTab} setSearchCount={setSearchCount}
                           contentRef={this.contentRef} history={history} hideContent={hideContent}
                           disableAuth={disableAuth}/>
      }
    ]

    if (!disableAuth || disableAuth.length === 0 || !disableAuth.includes('globalSearch.addressbook')) {
      // 通讯录
      tabData.push({
        type: "tel",
        title: lang.contact,
        count: tel,
        child: <TelContent {...publicProps} pageNum={pageNum} pageSize={pageSize} getScroll={getScroll}
                           history={history} hideContent={hideContent}/>
      })
    }

    if (!disableAuth || disableAuth.length === 0 || !disableAuth.includes('globalSearch.app')) {
      // 系统
      tabData.push({
        type: "sys",
        title: user.bgId === '30804' ? lang.sys : lang.service,
        count: sys,
        child: <SysContent {...publicProps}/>
      })
    }
    if (!disableAuth || disableAuth.length === 0 || !disableAuth.includes('globalSearch.tasks')) {
      // 代办
      tabData.push({
        type: "todo",
        title: lang.todo,
        count: todo,
        child: <TodoContent {...publicProps} pageNum={pageNum} pageSize={pageSize} getScroll={getScroll}/>
      })
    }
    if (!disableAuth || disableAuth.length === 0 || !disableAuth.includes('globalSearch.immessage') || !disableAuth.includes('globalSearch.imgroup') || !disableAuth.includes('globalSearch.imfile')) {
      // 消息
      tabData.push({
        type: "msg",
        title: lang.msg,
        count: msg,
        child: <MsgContent {...publicProps} pageNum={pageNum} pageSize={pageSize} msgType={msgType}
                           contentRef={this.contentRef} history={history} disableAuth={disableAuth}/>
      })
    }
    if (!disableAuth || disableAuth.length === 0 || !disableAuth.includes('globalSearch.moments')) {
      // 同事圈
      tabData.push({
        type: "friend",
        title: lang.group,
        count: friend,
        child: <FriendContent {...publicProps} pageNum={pageNum} pageSize={pageSize} getScroll={getScroll}/>
      })
    }
    if (!disableAuth || disableAuth.length === 0 || !disableAuth.includes('globalSearch.cms')) {
      // 资讯
      tabData.push({
        type: "news",
        title: lang.news,
        count: news,
        child: <NewsContent {...publicProps} pageNum={pageNum} pageSize={pageSize} getScroll={getScroll}/>
      })
    }
    if (!disableAuth || disableAuth.length === 0 || !disableAuth.includes('globalSearch.faq')) {
      // 知识库
      tabData.push({
        type: "knowledge",
        title: lang.knowledge,
        count: knowledge,
        child: <KnowledgeContent {...publicProps} pageNum={pageNum} pageSize={pageSize} getScroll={getScroll}/>
      })
    }

    return tabData
  }

  render() {
    console.log('content render',this.props)
    const {disableAuth} = this.props
    const {activeTab, clickTime, msgType} = this.state;
    const tabData = this.getTabData()
    return (
      <div className={css["ks-content"]} ref={this.contentRef}>
        <Tab clickTime={clickTime} data={tabData} active={activeTab} onClick={this.changeActiveTab}
             getMoreData={this.getMoreData} msgType={msgType}/>
      </div>
    )
  }

}

export default KaraSearch;