import React, { useState,useEffect} from 'react';
import SelectOptions from '../SelectOptions';

import { connect } from 'umi'

const index = (props:any) => {

  const {dispatch, CommunityList,value,onChange , checklist} = props;

  // console.log(props)

  const tosCommunityget = (values:any) => {dispatch({type: 'Init/tosCommunityget',playload:values})};  
  useEffect(() => {
    if (CommunityList == null) {
      tosCommunityget(null)
    }    
  }, []); 
  // 给已选值默认  如果有默认就传递默认 否则传递列表数据
  useEffect(() => {
    if (checklist != null) {
      onChange({ index:1,value: checklist})
    }else{
      onChange({ index:1,value: CommunityList})
    }
  }, [checklist]);

  // 点击选择的时候传递给子组件 已选列表
  const printContent = (values:any) => {
    onChange({ index:CommunityList.length==values.length?1:0,value: values})
  }
  return (
    <>
          {
            CommunityList != null ?
            <SelectOptions  list={CommunityList.sort()} checklist={checklist} show={true} onSubmit={printContent} />:  
            null
          }
             
    </>
  );
};

function mapStateToProps(state:any) {
  const { CommunityList } = state.Init;  
  return {
    CommunityList,
  };
}
export default connect(mapStateToProps)(index);
