import React, {Component} from 'react'
import Group from '../message/group/group'
import Post from '../message/post/post'
import File from '../message/file/file'
import getSearchTime from "../../../utils/getSearchTime";
import {Modal, Scroll} from 'karaui'
import css from "./index.scss"
import _ from 'lodash'

class MessageModal extends Component {
  constructor(props) {
    super(props);
    this.state = {
      pageInfo: {},
      pageNo: 1,//页码
      pageCount: 1,//总共多少页
      listArr: []//列表数据
    }
  }

  componentDidMount() {
    const {kw, modalType, searchTime} = this.props
    if (kw) {
      //文件，消息，文件，视频，bot
      this.search(1, kw, modalType, searchTime.start, searchTime.end);
    }
  }

  componentWillReceiveProps(next) {
    const {kw, modalType} = this.props
    const {searchTime} = next
    if (kw && kw !== next.kw) {
      this.search(1, next.kw, modalType, searchTime.start, searchTime.end);
    }
  }

  //根据时间查询
  searchByTime = (type, s, e, label) => {
    const {startTime, endTime} = getSearchTime(type, s, e)
    const {kw, msgtype, changeSearchTime} = this.props;//"group""msg""file"
    this.search(1, kw, msgtype, 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'
    }
    switch (type) {
      case "group":
        body.pageInfo.pageSize = 15;
        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({
              pageInfo: res.pageInfo,
              listArr: pageNo === 1 ? res.pageInfo.result : _.concat(this.state.listArr, res.pageInfo.result),
              pageCount: res.pageInfo.pageCount
            })
          }
        })
        break;
      case "msg":
        body.pageInfo.pageSize = 5;
        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({
              pageInfo: res.pageInfo,
              listArr: pageNo === 1 ? res.pageInfo.result : _.concat(this.state.listArr, res.pageInfo.result),
              pageCount: res.pageInfo.pageCount
            })
          }
        })
        break;
      case "file":
        body.pageInfo.pageSize = 12;
        httpAgent.post('/search/file/gatherFile', body, kw).then(res => {
          // console.log("文件列表", res, this.state.pageInfo)
          if (res && res.pageInfo && res.pageInfo.result && res.resultCode === '000000' && this.lastKeyWord === res.backParams) {
            this.setState({
              pageInfo: res.pageInfo,
              listArr: pageNo === 1 ? res.pageInfo.result : _.concat(this.state.listArr, res.pageInfo.result),
              pageCount: res.pageInfo.pageCount
            },()=>{
              let allFile = this.state.listArr;
              let fileInfo = this.state.pageInfo;
              fileInfo.result = allFile;
              this.setState({
                pageInfo:fileInfo,
              })
            })


          }
        })
        break;
    }

  }

  //滚动加载
  getMoreData = (type) => {
    const {pageNo, pageCount} = this.state;
    const {kw,searchTime} = this.props;

    if (pageNo <= pageCount) {
      this.setState({
        pageNo: pageNo + 1
      }, () => {
        this.search(this.state.pageNo, kw, type, searchTime.start, searchTime.end)
      })
    }
  }


  render() {
    const {msgModelShow, contentRef, parent, lang, httpAgent, kw, history, karaui, closeSearchModal, modalType, msgModelHide, onClose} = this.props;
    const {pageInfo, listArr} = this.state;
    return <div>
      <Modal show={msgModelShow} parent={parent} onMask={msgModelHide}>
        <div className={css['msg-modal']}>
          <i className={`${css['model-close']} ks-icon-guanbi ks-iconfont s-secondary`} onClick={msgModelHide}> </i>
          <Scroll onDragEndBottom={() => {
            this.getMoreData(modalType)
          }} onWheelBottom={() => {
            this.getMoreData(modalType)
          }}>
            {
              modalType === "group" ? <Group data={pageInfo} listArr={listArr} lang={lang} history={history}
                                             closeSearchModal={closeSearchModal} isLast
                                             isFixed/> : modalType === "msg" ?
                <Post kw={kw} data={pageInfo} listArr={listArr} lang={lang} httpAgent={httpAgent} history={history}
                      karaui={karaui} onClose={onClose}
                      closeSearchModal={closeSearchModal} isLast isFixed/> : modalType === "file" ?
                  <File data={pageInfo} contentRef={contentRef} lang={lang} isLast isFixed/> : null
            }
          </Scroll>

        </div>
      </Modal>
    </div>


  }

}

export default MessageModal;