import React,{Component} from 'react';
import {
    Button, Col, Row, Select, message
} from 'antd';
import { $http } from '../../containers/config/https';
import CBData from './CBData'
const Option = Select.Option;

class CBDataAuth extends Component {
    constructor(props) {
        super(props);
        this.state = {
            productCatData:[],
            productModuleData:[],
            TableData:[],
            ClassId:"",
            ClassName:"",
            productModule:"",
            productCat:""
        };
    }
    SelectChange=()=>{

    }
    getAuthData=()=>{
		const userData=sessionStorage.userData?JSON.parse(sessionStorage.userData):{}
        let data = {
            params: [{
                hospID:userData.hospID,
                groupID:userData.groupID
            }]
        }
        $http.post("urlS",this, {
            code: "4023",
            data: data,
            success: function (res) { 
                if (+res.errorCode === 0) {
                    var productCatData=res.ProductCatData
                    var productModuleData=res.ProductModuleData
                    var tableData=res.TableData
                    productCatData=productCatData.map((item,key)=>{
                        return <Option key={item.id} alias={item.descriptsSPCode}>{item.descripts}</Option>
                    })
                    this.setState({
                        productCatData:productCatData,
                        productModuleData:productModuleData,
                        TableData:tableData
                    })

                } else {
                    message.error('失败：' + res.errorMessage);
                }
            },
            error:function (error) {
                message.error('失败：' + error);
            }
        })        
    }
    //componentWillMount() {
    componentDidMount(){
        this.getAuthData()
    } 
    FindData=()=>{
        
    } 
    ClassChange=(value, option)=>{
        this.setState({
            ClassId:value,
            ClassName:option.props.className
        })
    }
    componentDidUpdate(){
        
    }
    GetTableEdit=()=>{
		const userData=sessionStorage.userData?JSON.parse(sessionStorage.userData):{}
        const hospDr=userData.hospID
        const groupDr=userData.groupID
        if(this.state.ClassName===""){
             return (<div>请选择要编辑的表</div>)
        }else{
                
            return (
                 <CBData  ClassName={this.state.ClassName} hospDr={hospDr} groupDr={groupDr}  ref="CBData" />
            )
        }
    }
    render() {
        
        return (
            <div>
                <Row type="flex" style={{"padding":"20px 20px 0px 20px"}}>
                    <Col span={6}>
                    {"产品分类: "}
                    <Select
                        allowClear={true}
                        ref={dom=>this.productCat=dom}
                        showSearch
                        style={{ width: '80%' }}
                        placeholder=""
                        optionFilterProp="children"
                        onChange={(value, option)=>{
                            this.setState({
                                productCat:value,
                                productModule:""
                            })
                        }}
                        filterOption={(input, option) => {
                            var desc=option.props.children
                            var alias=option.props.alias
                            var flag=(desc.toLowerCase().indexOf(input.toLowerCase()) >= 0)||(alias.toLowerCase().indexOf(input.toLowerCase()) >= 0)
                            return flag
                        }}
                    >
                    { 
                       this.state.productCatData
                    }   
                    </Select>
                    </Col>
                    <Col span={6}>
                    {"产品模块: "}
                    <Select
                        value={this.state.productModule}
                        allowClear={true}
                        showSearch
                        style={{ width: '80%' }}
                        placeholder=""
                        optionFilterProp="children"
                        onChange={(value, option)=>{
                            this.setState({
                                productModule:value
                            })
                        }}
                        filterOption={(input, option) => {
                            var desc=option.props.children
                            var alias=option.props.alias
                            var flag=(desc.toLowerCase().indexOf(input.toLowerCase()) >= 0)||(alias.toLowerCase().indexOf(input.toLowerCase()) >= 0)
                            return flag
                        }}
                    >
                    { 
                        this.state.productModuleData.map((item)=>{
                            var productCatDr=this.state.productCat
                            if(productCatDr===item.catDr){
                                return <Option key={item.id} alias={item.descriptsSPCode}>{item.descripts}</Option>
                            }else{
                                return null
                            }
                            
                        })
                    }   
                    </Select>
                    </Col>
                    <Col span={6}>
                    {"表名: "}
                    <Select
                        showSearch
                        style={{ width: '80%' }}
                        placeholder=""
                        value={this.state.ClassId}
                        optionFilterProp="children"
                        onChange={this.ClassChange}
                        filterOption={(input, option) => {
                            var desc=option.props.children
                            var alias=option.props.alias
                            var flag=(desc.toLowerCase().indexOf(input.toLowerCase()) >= 0)||(alias.toLowerCase().indexOf(input.toLowerCase()) >= 0)
                            return flag
                        }}
                    >
                    { 
                        this.state.TableData.map((item)=>{
                            var productModule=this.state.productModule
                            var productCat=this.state.productCat
                            if((productModule!=="")&&(productModule!==item.moduleDr)){
                                return null
                            }
                            if((productCat!=="")&&(productCat!==item.catDr)){
                                return null
                            }
                            return (
                                <Option key={item.ID} alias={item.Alias} className={item.ClassName} catDr={item.catDr} moduleDr={item.moduleDr}>{item.Desc}</Option>
                            )
                        })
                    }   
                    </Select>
                    </Col>
                    <Col span={2}>
                        <Button type="primary" onClick={this.FindData}>查询</Button> 
                    </Col>
                </Row>
                <Row>
                {/*<CBData  ClassName={this.state.ClassName} ClassTitle={this.state.ClassTitle} ref="CBData" />*/}
                {
                   this.GetTableEdit() 
                }
                </Row>
            </div>
        );
    }
}

export default CBDataAuth;