import React,{PropTypes} from 'react';  
import cx from 'classnames'; 
import l from './card.les';  
import { Card as MyCard,Button,Switch,Icon} from 'antd';
import Collapse from 'react-collapse';

class Card extends React.Component { 
	constructor(props) {
        super(props);  
        this.renderTools=this.renderTools.bind(this);
        this.collapseCard=this.collapseCard.bind(this);
        this.state={
        	cardStatus:true,
            closeChecked:true,
        }
    }
    static propTypes = {  
    	style: PropTypes.object,
    	extra: PropTypes.oneOfType([
			    	PropTypes.element,
			    	PropTypes.string
			    ]),
		title:PropTypes.oneOfType([
			    	PropTypes.element,
			    	PropTypes.string
			    ]),
		add:PropTypes.func,
		son:PropTypes.func,
		edit:PropTypes.func,
		closeBnt:PropTypes.func,
		turnIn:PropTypes.func,
		turnOut:PropTypes.func,
		showAdd:PropTypes.bool,
		showEdit:PropTypes.bool,
		showSon:PropTypes.bool,
		showCloseBtn:PropTypes.bool,
		showCollapse:PropTypes.bool,
		showTurnOut:PropTypes.bool,
		showTurnIn:PropTypes.bool,
	}   
	static defaultProps ={  
        "style":{marginBottom:'16px'}, 
        extra:<a href="#">More1</a>,
        add:()=>{},
		edit:()=>{},
		closeBnt:()=>{},
		son:()=>{},
		turnIn:()=>{},
		turnOut:()=>{},
		showAdd:false,
		showSon:false,
		showEdit:false,
		showCloseBtn:false,
		showTurnIn:false,
		showTurnOut:false,
		showCollapse:true,
        customize:null
    }  
    collapseCard(){
    	const {cardStatus}=this.state;

    	if(typeof this.props.cardStatus === 'boolean' && this.props.cardStatus === false){
    		return ;
		}
    	this.setState({
    		cardStatus:!cardStatus
    	});
    }

    componentWillReceiveProps(props){

    	if(typeof props.cardStatus === 'boolean'){
            this.setState({cardStatus:props.cardStatus});
		}

        if(typeof props.closeChecked === 'boolean'){
            this.setState({closeChecked:props.closeChecked});
        }
	}

	componentDidMount(){
    	const {cardStatus = null,closeChecked = null} = this.props;

    	if(typeof cardStatus === 'boolean'){
    		this.setState({cardStatus});
		}

        if(typeof closeChecked === 'boolean'){
            this.setState({closeChecked});
        }
	}

	renderTools(){
		const {add,edit,closeBnt,showAdd,showEdit,showCloseBtn,showCollapse,showSon,son,showTurnIn,showTurnOut,turnIn,turnOut,customize}=this.props;
		const {cardStatus,closeChecked}=this.state;
		// console.log('showTurnOut',showTurnOut);
		return (
			<div className={cx(l.tools_wrap)}>{/**/}
				{customize}
				{showAdd&&<Button onClick={add.bind(null,this.props.children)} className={cx(l.tool)} size="small"  icon="plus">添加</Button>}
				{showCloseBtn&&<Switch onChange={closeBnt} checked={closeChecked} defaultChecked className={cx(l.tool)} size="small"  checkedChildren={<Icon type="check" />} unCheckedChildren={<Icon type="cross" />}/>}
				{showEdit&&<i onClick={edit} className={cx(l.tool,l.tool_icon_edit,"upvi-icon")}>&#xe637;</i>}
				{showSon&&<Button onClick={son} className={cx(l.tool)} size="small"  icon="plus" style={{marginLeft:'10px'}}>子部门</Button>}
				{showTurnIn&&<Button type="primary" onClick={turnIn} className={cx(l.tool,l.turn_btn)} size="small"  icon="plus">调入</Button>}
				{showTurnOut&&<Button onClick={turnOut} className={cx(l.tool)} size="small"  icon="minus">调出</Button>}
				{showCollapse&&<i onClick={this.collapseCard} className={cx(l.tool_icon_dropdown,"upvi-icon")}>&#xe625;</i>}  
			</div>
		)
	}
    render(){ 
    	const {style,extra,title}=this.props;
    	const {cardStatus}=this.state;
    	return (
			<MyCard className={cx(l.my_card)} title={title} 
				  bordered={false} extra={this.renderTools()} style={style}>
				  <Collapse  isOpened={cardStatus}>
				  	<div className={cx(l.center_wrap)}>
				  		{this.props.children}
				  	</div>
				  </Collapse>
			</MyCard> 
    	)
    }
} 

export default Card;