import {connect} from 'react-redux';
import SearchBox from '@commonComp/SearchBox';
import {getSearch} from '@store/async-actions/fetchModules.js';
import {CLEAR_ADD_SEARCH,SET_ADD_SEARCH,CHOOSE_SEARCH} from '@store/types/mainSuit';
import {ISREAD} from '@store/types/homePage.js';
import {billing} from '@store/async-actions/pushMessage';
import {Notify} from '@commonComp';
import {filterDataArr} from '@utils/tools.js';
import store from '@store';
import config from '@config/index.js';

function mapStateToProps(state){
  return{
    mainSearchData:state.mainSuit.addSearchData,//主诉-添加症状-搜索结果
    mainIds:state.mainSuit.mainIds,  //搜索去重id
  }
}

function mapDispatchToProps(dispatch){
  return{
    // 主诉-添加症状-搜索
    getSearchData:(item)=>{
      getSearch(item).then((res)=>{
          let result = res.data;
          if(+result.code == 0){
            let data = result.data;
            dispatch({
              type:SET_ADD_SEARCH,
              data:data
            })
          }else{
            console.log(result.msg);
          }
      });
    },
    chooseSearch(obj){
      // 主诉字数限制
      let state = store.getState();
      let mainSaveText = state.mainSuit.saveText;
      let text = filterDataArr(mainSaveText);
      let total = text.length + obj.item.name.length + 1; //+1是因为顿号
      if(total >= config.limited){
        Notify.info(config.limitText);
        return
      }
      dispatch({
        type: CHOOSE_SEARCH,
        info:obj
      })
      dispatch({    //自由文本标签数据更新
        type:ISREAD
      });
      //右侧推送
      setTimeout(function(){ 
        dispatch(billing());
      },200);
    },
    clearSearch:()=>{
      dispatch({
        type: CLEAR_ADD_SEARCH
      })
    },
  }
}

const SearchBoxContainer = connect(mapStateToProps,mapDispatchToProps)(SearchBox);
export default SearchBoxContainer;