import React, {Component} from 'react'
import Group from './group/group'
import Post from './post/post'
import File from './file/file'
import './message.scss'
import SearchPanel from '../../common/searchPanel/searchPanel'
import SearchDate from '../../common/searchDate/searchDate'
import getSearchTime from "../../../utils/getSearchTime";
import Nothing from '../nothing/nothing'
import {Modal, Load} from 'karaui'
import MessageModal from "../msgModal";

class Message extends Component {
  constructor(props) {
    super(props);
    this.state = {
      groupInfo: {},
      msgPageNum: 1,
      postInfo: {},
      fileInfo: {},
      msgModelShow: false,//消息弹窗是否显示
      modalType: "",//消息弹窗的类型：file,msg,group,video.bot
      load:true
    }
  }

  componentDidMount() {
    // console.log('message did mount',this.props)
    const {kw,searchTime} = this.props
    if (kw) {
      this.search(1, kw, "group",searchTime.start, searchTime.end);
      this.search(1, kw, "msg",searchTime.start, searchTime.end);
      this.search(1, kw, "file",searchTime.start, searchTime.end);
    }
  }

  componentWillReceiveProps(next) {
    const {kw, clearSearchCount} = this.props
    const {searchTime} = next
    if (kw && kw !== next.kw) {
      this.search(1, next.kw, "group", searchTime.start, searchTime.end);
      this.search(1, next.kw, "msg", searchTime.start, searchTime.end);
      this.search(1, next.kw, "file", searchTime.start, searchTime.end);
      clearSearchCount()
    }
  }

  //根据时间查询
  searchByTime = (type, s, e, label) => {
    const {startTime, endTime} = getSearchTime(type, s, e)
    const {kw, changeSearchTime} = this.props;
    this.search(1, kw, "group", startTime, endTime);
    this.search(1, kw, "msg", startTime, endTime);
    this.search(1, kw, "file", startTime, endTime);
    // console.log('set time',type,s,e,startTime,endTime)
    changeSearchTime({type,label,start:startTime,end:endTime})
  }

  search = (pageNo, kw, type, from = '', to = '') => {
    const {httpAgent, language} = this.props
    this.lastKeyWord = kw;
    let body = {
      keywords: kw,
      from,
      to,
      pageInfo: {pageNo: pageNo, pageSize: 1},
      language: language === 'en' ? 'en' : 'cn',
      isHighlight: true,
      platform: 'web',
      isShowRich: true
    }
    switch (type) {
      case "group":
        body.pageInfo.pageSize = 5;
        httpAgent.post('/search/global/channel/list', body, kw).then(res => {
          // console.log("群组列表", res)
          if (res && res.pageInfo && res.pageInfo.result && res.resultCode === '000000' && this.lastKeyWord === res.backParams) {
            this.setState({
              groupInfo: res.pageInfo,
              load:false
            })
          }
        })
        break;
      case "msg":
        body.pageInfo.pageSize = 3;
        httpAgent.post('/search/global/chatRecord/list', body, kw).then(res => {
          // console.log("消息列表", res)
          if (res && res.pageInfo && res.pageInfo.result && res.resultCode === '000000' && this.lastKeyWord === res.backParams) {
            this.setState({
              postInfo: res.pageInfo,
              load:false
            })
          }
        })
        break;
      case "file":
        body.pageInfo.pageSize = 4;
        httpAgent.post('/search/file/gatherFile', body, kw).then(res => {
          // console.log("文件列表", res)
          if (res && res.pageInfo && res.pageInfo.result && res.resultCode === '000000' && this.lastKeyWord === res.backParams) {
            this.setState({
              fileInfo: res.pageInfo,
              load:false
            })
          }
        })
        break;
    }

  }


  //消息中的类别的查看更多
  msgGetMore = (type) => {

    this.setState({
      msgModelShow: true,
      modalType: type
    })

  }

  msgModelHide = () => {
    this.setState({
      msgModelShow: false
    })
  }


  render() {
    const parent = document.getElementById('app')
    const {contentRef, lang, httpAgent, kw, history, karaui, closeSearchModal, searchTime,onClose} = this.props;
    const {groupInfo, postInfo, fileInfo, msgModelShow, modalType,load} = this.state;
    const count = (groupInfo.count || 0) + (postInfo.count || 0) + (fileInfo.count || 0);
    const millis = [(groupInfo.millis || 0), (postInfo.millis || 0), (fileInfo.millis || 0)].sort(function (a, b) {
      return b - a
    })[0]
    const selectDate = <SearchDate chooseTime={this.searchByTime} searchTime={searchTime} lang={lang}/>;
    return !load ? count > 0 ?
      <SearchPanel num={count} time={millis} selectDate={selectDate} lang={lang}>

        <Group data={groupInfo} lang={lang} history={history} closeSearchModal={closeSearchModal}
               listArr={groupInfo.result}
               httpAgent={httpAgent}
               onMore={groupInfo.count > 5 ? () => {
                 this.msgGetMore('group');
               } : null}
               kw={kw}
        />
        {/*消息--聊天记录，显示3条*/}
        <Post kw={kw} data={postInfo} lang={lang} httpAgent={httpAgent} history={history} karaui={karaui}
              listArr={postInfo.result}
              onMore={postInfo.count > 3 ? () => {
                this.msgGetMore('msg');
              } : null} onClose={onClose}
              closeSearchModal={closeSearchModal}
        />
        {/*消息--文件，显示4条*/}
        <File data={fileInfo} contentRef={contentRef} lang={lang}
              httpAgent={httpAgent}
              onMore={fileInfo.count > 4 ? () => {
                this.msgGetMore('file');
              } : null}
              kw={kw}
        />

        {msgModelShow ?
          <MessageModal modalType={modalType} parent={parent} msgModelHide={this.msgModelHide} contentRef={contentRef}
                        lang={lang} httpAgent={httpAgent} kw={kw}
                        history={history}
                        karaui={karaui} msgModelShow={msgModelShow} closeSearchModal={closeSearchModal} searchTime={searchTime}/> : null}

      </SearchPanel> : <Nothing lang={lang}/> : <Load/>


  }

}

export default Message;