import React, { Component, PropTypes } from 'react';
import { Checkbox } from 'antd';
import { connect } from 'dva';
import styles from './style.less';
const CheckboxGroup = Checkbox.Group;

const CheckGroup=({
                    checkId,
                    checkAll,
                    checkedList,
                    checkAllValue,
                    checkAllLable,
                    hasChildren,
                    children,
                    dispatch,
                    character,
                    allListId,
                    indeterminate,
                    checkAllId
      })=>{
  function onCheckAllChange(e){
    let newList=character.newList;
    newList[checkId]={
      ['allListId'+checkId]:allListId,
      ['checkAllId'+checkId]:checkAllId,
      ['checkedList'+checkId]: e.target.checked ? allListId : [],
      // ['indeterminate'+checkId]: !indeterminate,
      ['checkAll'+checkId]: e.target.checked,
    };
    dispatch({
      type:'character/getAuthorityList',
      payload:{
        newList:newList
      }
    })
  }
  function selectCheckbox(checkedList){
    let newList=character.newList;
    newList[checkId]={
      ['allListId'+checkId]:allListId,
      ['checkAllId'+checkId]:checkAllId,
      ['checkedList'+checkId]: checkedList,
      // ['indeterminate'+checkId]: !!checkedList.length && (checkedList.length < allListId.length),
      ['checkAll'+checkId]: checkedList.length >0,
    };
    dispatch({
      type:'character/updateState',
      payload:{
        newList:newList
      }
    })
  }

  return(
    <div className={styles.allCheckbox} id={checkId}>
  <Checkbox checked={checkAll } value={checkAllValue} onChange={(e)=>onCheckAllChange(e)} >{ checkAllLable}</Checkbox >
  {
    hasChildren ?
      <CheckboxGroup className={styles.subCheckbox}   value={checkedList} onChange={selectCheckbox}>
    {
      children.map((sub, subKey) => {
        return  <Checkbox value={sub.menuid} id={`${sub.menuid}`} key={subKey} >{sub.name}</Checkbox >
      })
    }
    </CheckboxGroup > : null
  }
  </div >
)

}


CheckGroup.propTypes = {
  // searchValue: PropTypes.string,
  // characterList: PropTypes.array,
  // dispatch: PropTypes.func,
  // showSubModal: PropTypes.bool,
  // deteleSubModal: PropTypes.bool
};

export default connect(({ character, loading }) => ({ character, loading }))(CheckGroup);
