//化学通用名维护
import React from 'react';
import {
    Form,Row,Card,Input,Button,Table,Pagination,message
} from 'antd';
import ChemistrySave from './ChemistrySave';
import '../../../assets/css/App.css'
import { $http } from '../../../containers/config/https';

class ChemistryMain extends React.Component {
    constructor(props) {
        super(props)
        // var userData = JSON.parse(sessionStorage.getItem('userData'))
        this.state = {
            chemisdesc:'',
            total: 0,
            page: 1,
            pageSize: 10,
        }
        this.columns = [
			{
                key: '1',
                title: '化学通用名代码',
                dataIndex: 'chemicCode',
                width: "20%",
                align: 'center'
            },{
                key: '2',
                title: '化学通用名描述',
                dataIndex: 'chemicDesc',
                width: "20%",
                align: 'center'
            },{
                key: '3',
                title: '药理学分类',
                dataIndex: 'phCatDesc',
                width: "20%",
                align: 'center'
            }];
    }

    componentDidMount() {
    }

    // 调用子组件的连接事件
    onRef = (ref) => {
        this.child = ref
    }

    // 查询条件 --- 描述
    onChangeDesc = (e) => {
        this.setState({ chemisdesc: e.target.value });
    }

    handleQueryAll = () =>{
        this.setState({
            page:1,
            pageSize:10,
        },()=>{
            this.handleQuery();
        })

    }

    // 查询
	handleQuery = () => {
		let {chemisdesc, page, pageSize} = this.state;
		let data = {}
        data = {
            params: [{
                catdr: this.props.phcatid ? this.props.phcatid:'',
                descripts: chemisdesc,
            }],
            pagination:[{
                pageSize: pageSize,
                currentPage: page,
                sort: "ID",
                dir: "desc"
            }]
        }
        //console.log(data)
		$http.post('urlS', this, {
			code: '02020004',
			data: data,
			success: function (res) {
                this.setState({
                    selectID:'',
                })
				if (+res.errorCode === 0 && res.result) {
					for(var i = 0; i < res.result.rows.length; i++) {
						res.result.rows[i].key = i ;
                    }
                    this.setState({data: res.result.rows, total: res.result.total})
                    if(res.result.rows.length===0)
                    {
                        message.success('没有查询到数据！！！');
                    }
				} else {
                    
				}
			}
        })
    }

    // 化学通用名新增
    handleAdd = () => {
        // 显示弹框
        this.child.showModal()
        // 初始化数据
        this.setState({
            flag: 'addChemis', 
            title: '新增化学通用名',
            phcatdesclabel:'药理学分类',
            codeLabel: '化学通用名代码',
            descLabel: '化学通用名描述',
            oldDescLabel: '改前化学通用名',
            oldDesc:'',
            code:'',
            desc:'',
            phcatid:this.props.phcatid ? this.props.phcatid:'',
            phcatdesc:this.props.phcatdesc ? this.props.phcatdesc:'',
        })
    }


    // 化学通用名修改
    handleModify = () => {
        if(parseInt(this.state.selectID)>0){
            // 显示弹框
            this.child.showModal()
            // 初始化数据
            this.setState({
                flag: 'modifyChemis', 
                title: '修改化学通用名',
                phcatdesclabel:'药理学分类',
                codeLabel: '化学通用名代码',
                descLabel: '化学通用名描述',
                oldDescLabel: '改前化学通用名',
                oldDesc:this.state.selectDesc,
                code:this.state.selectCode,
                desc:this.state.selectDesc,
                phcatid:'',
                phcatdesc:this.state.selectPhCatDesc ? this.state.selectPhCatDesc:'',
                ID:this.state.selectID,
            })
        }else{
            message.warning('请选择化学通用名再修改！！！');
            return; 
        }
    }

    // 关联药理学分类
    handleLinkPhCat = () => {
        if(parseInt(this.state.selectID)>0){
            
        }else{
            message.warning('请选择化学通用名再做关联操作！！！');
            return; 
        }
        if(parseInt(this.props.phcatid)>0){
            
        }else{
            message.warning('请选择药理学通用名再做关联操作！！！');
            return; 
        }
        let data = {}
        data={
            params: [{
                chemicID:this.state.selectID,
                phCatID:this.props.phcatid,
            }]
        }
        //console.log(data)
		$http.post('urlS', this, {
			code: '02020005',
			data: data,
			success: function (res) {
				if (+res.errorCode === 0) {
                    message.success('关联药理学分类成功');
                    this.handleQueryAll();
				} else {
                    
				}
			}
        })
    }

    // 页码改变的回调，参数是改变后的页码及每页条数
    PageChange = (page, pageSize)=> {
        this.setState({page: page, pageSize: pageSize}, () => {
            // 重新调用查询方法
            this.handleQuery();
        });
    }

    // 条数改变的回调
    onShowSizeChange = (current, size) => {
    this.setState({page: current, pageSize: size}, () => {
            // 重新调用查询方法
            this.handleQuery();
        });
    }

    setRowClassName = (record) => {
        return record.chemicID === this.state.selectID ? 'clickRowStyle' : '';
    }

    onClickRow = (record) => {
        //console.log(record)
        return {
            onClick: () => {
                if(this.state.selectID!==record.chemicID){
                    this.setState({
                        selectID: record.chemicID,
                        selectCode:record.chemicCode,
                        selectDesc:record.chemicDesc,
                        selectPhCatDesc:record.phCatDesc,
                    });
                }
            },
            onDoubleClick: () => {
                this.props.handelThis.setState({
                    selectChemistryID:record.chemicID,
                    selectChemistryDesc:record.chemicDesc,
                },()=>{

                })
                setTimeout(() => {this.props.handelThis.onChangeTabs(2)},100)
            }
        };
    }

    // 显示分页总条数
    showTotal(total) {
        return `共 ${total} 条`;
    }

    render() {
        return(
            <div>
                <Row gutter={24}>
                    <Card bodyStyle={{padding:'12px'}}>
                        <Row>
                            <Input placeholder="化学通用名···" value={this.state.chemisdesc} style={{width: '180px', marginLeft: '12px'}} onChange={this.onChangeDesc} />
                            <Button style={{ marginLeft: '40px', background: '#1999db',color:'#ffffff'}}  onClick={this.handleQueryAll}>查询</Button>
                            <Button style={{ marginLeft: '40px', background: '#1999db',color:'#ffffff'}}  onClick={this.handleAdd}>新增</Button>
                            <Button style={{ marginLeft: '40px', background: 'rgba(255,0,0,1)',color:'#ffffff'}}  onClick={this.handleModify}>修改</Button>
                            <Button style={{ marginLeft: '40px', background: '#1999db',color:'#ffffff'}}  onClick={this.handleLinkPhCat}>关联药理学分类</Button>
                        </Row>
                    </Card>
                </Row>
                <Row gutter={24}>  
                    <Card bodyStyle={{padding:'12px',paddingLeft:'24px'}}>
                        <Row>
                            <div>
                                <Table
                                    dataSource={this.state.data}
                                    columns={this.columns}
                                    rowClassName={this.setRowClassName}
                                    pagination={false}
                                    bordered
                                    onRow={this.onClickRow}
                                    style={{height:'520px'}}
                                    scroll={{ x: this.state.TableWidth, y: '470px' }}
                                />
                                 <Pagination
                                    showTotal={() => this.showTotal(this.state.total)}
                                    size="small"
                                    showQuickJumper
                                    showSizeChanger
                                    defaultCurrent={1}
                                    current={this.state.page}
                                    total={this.state.total}
                                    onChange={this.PageChange}
                                    onShowSizeChange={this.onShowSizeChange}
                                    style={{textAlign: "right", marginTop: '5px'}}
                                />
                            </div>
                        </Row>
                    </Card>
                </Row>
                {/* 新增弹框 */}
                <ChemistrySave
                    onRef={this.onRef}
                    flag={this.state.flag}
                    handelThis={this}
                    title={this.state.title}
                    codeLabel={this.state.codeLabel}
                    descLabel={this.state.descLabel}
                    oldDescLabel={this.state.oldDescLabel}
                    oldDesc={this.state.oldDesc}
                    code={this.state.code}
                    desc={this.state.desc}
                    phcatid={this.state.phcatid}
                    phCatDescLabel={this.state.phcatdesclabel}
                    phCatDesc={this.state.phcatdesc}
                    ID={this.state.ID}
                />
            </div>
        )
    }
}export default Form.create()(ChemistryMain);