import React, { Component } from 'react'
import { connect } from 'dva';
import { config, request, permission, util } from 'utils'
import { SearchBar, Modal, Menu, Toast } from 'antd-mobile';
import router from 'umi/router';
import styles from './index.less';
const { APIV3, WXMpAPI } = config
class ClassPage extends Component {
  constructor(props) {
    super(props)
    this.state = {
        columnId:"",
        searchlist:[],
        latelySearchlist:[]
    }
  }
  componentDidMount() {
    this.setState({
      columnId:this.props.location.query.columnId
    },()=>{
        this.requestSearchText();
    })
    this.requestSearch()
  }

  requestSearchText=()=>{
    let self = this
    let list ={
       "pageIndex":1,
        "pageSize":10,
          "filters": [
              {
                  "field": "search_term_column_id",
                  "operate": "equal",
                  "value": self.state.columnId
              }
      ],
      }
        request({
            url: WXMpAPI + '/wx/content/searchTerm/queryAllSearchTerms',
            method: "post",
            data: list
        }).then(function (getListRes) {
            if (getListRes.success == 1) {
              self.setState({
                searchlist:getListRes.items
              })
            }
        });
  }
  //查询最近搜索词
  requestSearch=()=>{
        request({
            url: WXMpAPI + '/wx/content/searchTerm/queryUserSearchTerms',
            method: "post",
        }).then(function (getListRes) {
            if (getListRes.success == 1) {
              this.setState({
                latelySearchlist:getListRes.items
              })
              }
        });
  }
  //插入最近搜索词
  insertSearch=(value)=>{
    let list ={
       "searchTermName":value
      }
        request({
            url: WXMpAPI + '/wx/content/searchTerm/queryAllSearchTerms',
            method: "post",
            data: list
        }).then(function (getListRes) {
            if (getListRes.success == 1) {
              console.log("添加成功");
              }
        });
  }
  onSubmitSearchText=(value)=>{
    this.insertSearch(value);
    router.push({pathname:'/class',query:{columnId:this.state.columnId,searchText:value}})
  }
  render() {
    let self =this 
    return (
      <div className='search_cart'>
        <div className="search">
          <div className="search-input">
            <SearchBar
              placeholder="搜索"
              onSubmit={(value)=>this.onSubmitSearchText(value)}
            />
          </div>
        </div>
        <div className="search_history">
          <h2>最近搜索</h2>
          <div className="search_content">
            {
              self.state.latelySearchlist.length<=0?[]:self.state.latelySearchlist.map((item,index)=>{
                return(
                      <div>{item.searchTermNam}</div>
                )
              })
            }
          </div>
          <h2>热门搜索</h2>
          <div className="search_content">
            {
              self.state.searchlist.lengtg<=0?[]:self.state.searchlist.map((item,index)=>{
                return(
                <div>{item.searchTermName}</div>
                )
              })
            }
          </div>
        </div>
      </div>
    )
  }
}


export default ClassPage;
