//处方通用名维护
import React from 'react';
import {
    Form,Row,Card,Input,Button,Table,Pagination,message,Tag
} from 'antd';
import GenericSave from './GenericSave';
import '../../../assets/css/App.css'
import { $http } from '../../../containers/config/https';
var _queryChemisFlag=0;

class GenericMain extends React.Component {
    constructor(props) {
        super(props)
        // var userData = JSON.parse(sessionStorage.getItem('userData'))
        this.state = {
            genericdesc:'',
            total: 0,
            page: 1,
            pageSize: 10,
        }
        this.columns = [
			{
                key: '1',
                title: '处方通用名代码',
                dataIndex: 'generCode',
                width: "20%",
                align: 'center'
            },{
                key: '2',
                title: '处方通用名描述',
                dataIndex: 'generDesc',
                width: "20%",
                align: 'center'
            },{
                key: '3',
                title: '化学通用名',
                dataIndex: 'chemicDesc',
                width: "20%",
                align: 'center'
            },{
                key: '4',
                title: '剂型',
                dataIndex: 'formDesc',
                width: "20%",
                align: 'center'
            }];
    }

    componentDidMount() {
        this.props.onRef(this)
    }

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

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


    // 查询全部
	handleAllQuery = () => {
        this.setState({
            chemistryDesc:'',
        })
        _queryChemisFlag=0;
        this.handleQuery(_queryChemisFlag);
    }

    // 查询
	handleQuery = (flag) => {
        console.log('this.props.chemistryID:'+this.props.chemistryID)
		let {genericdesc, page, pageSize} = this.state;
		let data = {}
        data = {
            params: [{
                chemistryDr: this.props.chemistryID && flag ? this.props.chemistryID:'',
                descripts: genericdesc,
            }],
            pagination:[{
                pageSize: pageSize,
                currentPage: page,
                sort: "ID",
                dir: "desc"
            }]
        }
        //console.log(data)
		$http.post('urlS', this, {
			code: '02020008',
			data: data,
			success: function (res) {
				if (+res.errorCode === 0 && res.result) {
                    this.setState({
                        selectID:'',
                    })
					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: 'addGeneric', 
            title: '新增处方通用名',
            oldDesc:'',
            code:'',
            desc:'',
            ID:'',
            chemisDr:'',
            chemisDesc:'',
            formDr:'',
            formDesc:'',
        })
    }

    // 处方通用名修改
    handleModify = () => {
        if(parseInt(this.state.selectID)>0){
            
            // 初始化数据
            this.setState({
                flag: 'modifyGeneric', 
                title: '修改处方通用名',
                oldDesc:this.state.selectDesc,
                code:this.state.selectCode,
                desc:this.state.selectDesc,
                ID:this.state.selectID,
                chemisDr:this.state.selectChemisDr,
                chemisDesc:this.state.selectChemisDesc,
                formDr:this.state.selectFormDr,
                formDesc:this.state.selectFormDesc,
            },()=>{
                // 显示弹框
                this.child.showModal()
            })
        }else{
            message.warning('请选择处方通用名再修改！！！');
            return; 
        }
    }

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

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

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

    onClickRow = (record) => {
        //console.log(record)
        return {
            onClick: () => {
                if(this.state.selectID!==record.generID){
                    this.setState({
                        selectID: record.generID,
                        selectCode:record.generCode,
                        selectDesc:record.generDesc,
                        selectChemisDr:record.chemicID,
                        selectChemisDesc:record.chemicDesc,
                        selectFormDr:record.formID,
                        selectFormDesc:record.formDesc,
                    });
                }
            },
            onDoubleClick: () => {
                this.props.handelThis.setState({
                    selectGenericID:record.generID,
                    selectGenericDesc:record.generDesc,
                },()=>{

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

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

    render() {
        return(
            <div>
                <Row gutter={24}>
                    <Card bodyStyle={{padding:'12px'}}>
                        <Row>
                            <Input placeholder="处方通用名···" value={this.state.genericdesc} style={{width: '180px', marginLeft: '12px'}} onChange={this.onChangeDesc} />
                            <Button style={{ marginLeft: '40px', background: '#1999db',color:'#ffffff'}}  onClick={this.handleAllQuery}>查询</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>
                            <Tag style={{ marginLeft: '40px',fontSize:"12pt"}} color="green">{'化学通用名：' + (this.state.chemistryDesc?this.state.chemistryDesc:'全部')}</Tag>
                        </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>
                {/* 新增弹框 */}
                <GenericSave
                    onRef={this.onRef}
                    flag={this.state.flag}
                    handelThis={this}
                    title={this.state.title}
                    oldDesc={this.state.oldDesc}
                    code={this.state.code}
                    desc={this.state.desc}
                    ID={this.state.ID}
                    chemisDr={this.state.chemisDr}
                    chemisDesc={this.state.chemisDesc}
                    formDr={this.state.formDr}
                    formDesc={this.state.formDesc}
                />
            </div>
        )
    }
}export default Form.create()(GenericMain);