// 处方点评
import React from 'react';
import { Card, Row, Col, Form, Modal, Select, Button, Tabs , Table, message, Tree, Input, Tooltip } from 'antd';
import { $http } from 'containers/config/https';
import CommonPagination from 'components/pagination/index';
import OrderDetails from './component/OrderDetails';
import PrescriptionReviewCheck from './component/PrescriptionReviewCheck';
import Preview from './component/Preview'
import Moment from 'moment';
import store from 'store';
import AllergyRecords from "./component/AllergyRecords";
import ThisTimeCharge from "./component/ThisTimeCharge";
import InspectionRecords from "./component/InspectionRecords";
import Unreasonable from "./component/unreasonable";
import TestRecord from "./component/TestRecord";
const { TabPane } = Tabs;
const { Option } = Select;
const FormItem = Form.Item;
const formItemLayout = {    // label 和input宽度
    labelCol: { span:8 },
    wrapperCol: { span: 16 },
};
const formItemItem  = {
    labelCol: {span: 10},
    wrapperCol: {span: 14}
};
const { TextArea } = Input;
const { TreeNode, DirectoryTree } = Tree;
const PrescriptionReview = Form.create()(
    class extends React.Component {
        constructor() {
            super();
            this.state = {
                userInfo: {}, //表单数据详情
                // SelectData: {}, //下拉数据
                ReciewData: [], //处方点评数据
                pageSize: 20,
                page: 1,
                total:0,
                rowKey: '', //行选中id
                rowData: {}, //当前行数据
                language: JSON.parse(window.sessionStorage.userData),
                ChineseEnglishSize: [],
                columns:[],
                detailWidthTotal:'',
                PrescriptionNO: '', //处方ID
                titleKey: 1, //头部切换
                visible: false ,//不合理弹出显示情况
                LeftKey: '', //行id
                RowsId: '', //双击弹窗id
            }
            // 库存单的数据表头
            this.columns = [

                {
                    key: 1,
                    title: '合理用药',
                    dataIndex: 'tkCode',
                    align: "center",
                    width: '20%'
                },
                {
                    key: 2,
                    title: '预审',
                    dataIndex: 'tkDesc',
                    align: "center",
                    width: '20%'
                },
                {
                    key: 3,
                    title: '处方号',
                    dataIndex: 'prescNo',
                    align: "center",
                    width: '20%'
                },
                {
                    key: 4,
                    title: '结果',
                    dataIndex: 'state',
                    align: "center",
                    width: '20%',
                }
            ];
            this.columnsPopup = [
                {
                    key: 1,
                    title: '医嘱名称',
                    dataIndex: 'Desc',
                    align: "center",
                    width: '100%'
                }
                ];
            this.transitColumns = [

                {
                    key: 1,
                    title: '组号 ',
                    dataIndex: 'stockCode',
                    align: "center",
                    width: '150px'
                },
                {
                    key: 2,
                    title: '点评原因',
                    dataIndex: 'reason',
                    align: "center",
                    width: '150px'
                },
                {
                    key: 3,
                    title: '点评日期',
                    dataIndex: 'auditDate',
                    align: "center",
                    width: '150px'
                },
                {
                    key: 4,
                    title: '点评时间',
                    dataIndex: 'auditTime',
                    align: "center",
                    width: '100px'
                },
                {
                    key: 5,
                    title: '点评人',
                    dataIndex: 'auditUserName',
                    align: "center",
                    width: '100px'
                },
                {
                    key: 6,
                    title: '点评结果',
                    dataIndex: 'state',
                    align: "center",
                    width: '100px'
                },
                {
                    key: 7,
                    title: '不合格警示值',
                    dataIndex: 'purUnitDesc',
                    align: "center",
                    width: '150px'
                },
                {
                    key: 8,
                    title: '药师建议',
                    dataIndex: 'baseQty',
                    align: "center",
                    width: '150px'
                },
                {
                    key: 9,
                    title: '药师备注',
                    dataIndex: 'baseUnitDesc',
                    align: "center",
                    width: '100px'
                },
                {
                    key: 10,
                    title: '医生备注',
                    dataIndex: 'sp',
                    align: "center",
                    width: '100px'
                },
                {
                    key: 11,
                    title: '有效',
                    dataIndex: 'state',
                    align: "center",
                    width: '100px'
                }
            ]
        }

        componentDidMount() {
            // 初始化数据
            // this.getDetailTableColumns();
            // this.callback();
        }

        componentWillUnmount() {
            // 组件销毁前将静止让setState修改state的状态，防止内存泄漏
            this.setState = (state,callback)=>{ return; }
        }

        //清空表格内容
        resetFields = () => {
            this.props.form.resetFields();
            this.setState({userInfo: {}, rowKey: '', rowData: ''});
        };
        //双击查询数据返回的数据
        getCheckData = (val) => {
            if(val){
                this.setState({RowsId: val})
            }
            let {language} = this.state;
            let data = {
                params: [{
                    id: val?val:this.state.RowsId
                }]
            }
            $http.post('urlS', this, {
                code: '02020315',
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        for(var i=0; i<res.data.length; i++){
                            res.data[i].key = i;
                        }
                        this.setState({ReciewData: res.data})
                    }else {
                        message.error('没数据')
                    }
                }
            })
        };
        // 设置行属性
        onClickRow = (record) => {
            return {
                // 单击行选中
                onClick: (e) => {
                    e.stopPropagation();
                    // this.resetFields();
                    if (this.state.rowKey === '') {
                        // 保存行数据以及ID
                        this.setState({rowKey: record.id, PrescriptionNO: record.prescNo},()=>{
                            this.onClickTable();
                        });
                    } else {
                        if (this.state.rowKey !== record.id) {
                            this.setState({rowKey: record.id, PrescriptionNO: record.prescNo},()=>{
                                this.onClickTable();
                            });
                        }
                        // else {
                        //     this.setState({rowKey: '', PrescriptionNO: {}});
                        //     // this.resetFields();
                        // }
                    }
                }
            }
        };

        // 表格行的类名
        setRowClassName = (record) => {
            return record.id === this.state.rowKey ? 'clickRowStyle' : '';
        };

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

        onShowSizeChange = (current, size) => {
            this.setState({page: current, pageSize: size}, () => {
                // 重新调用查询方法
                this.onChange(current,size);
            })
        };

        //页面中英文翻译
        // getDetailTableColumns = () => {
        //     let {language} = this.state;
        //     let data = {
        //         params: [{
        //             reactCode: ['StkTakeMaintenance'],
        //             compontName: 'StkTakeMaintenance',
        //             type: 'C',
        //         }]
        //     }
        //     $http.post('urlS', this, {
        //         code: '01040073',
        //         data: data,
        //         success: function (res) {
        //             if (+res.errorCode === 0) {
        //                 let arr = res.result.C&&res.result.C.length > 0 ? JSON.parse(JSON.stringify(res.result.C)).map((item, index) => {
        //                     return  {
        //                         ...item,
        //                         key: index + 1,
        //                         title: <div>{language.language== 'EN' ? item.enDescripts : item.title}</div>
        //                     }
        //                 }) : '';
        //                 let filterArr = arr.filter(function(item){ return item.display === 'Y'});
        //                 // this.setState({columns: filterArr, detailWidthTotal: res.totalWidth,ChineseEnglishSize:language.language=='EN' ? res.result.language.EN : res.result.language.CN})
        //             }
        //         }
        //     })
        // };

        callback=(key)=> {
            this.setState({LeftKey: key})
        }

        onClickTable=()=>{
            let {PrescriptionNO} =this.state;
            if(this.state.LeftKey == 2){
                console.log(key);
            }else {
                this.childOrder.getPrescriptionData(PrescriptionNO)
                this.childOrder.getStockList(PrescriptionNO)
                this.childOrder.getDiaryData(PrescriptionNO)
            }
        }

        reasonableData=(flag, val)=>{
            if(!this.state.rowKey){message.error('请选择数据'); return}
            let {language} = this.state;
            let data = {
                params: [{
                    prescNo: this.state.PrescriptionNO ? this.state.PrescriptionNO : '',
                    id: this.state.rowKey ? this.state.rowKey : '',
                    auditFlag: flag,
                    remark: val  ? val.remark : '',
                    reasonInfo:  val ? val.reasonInfo: undefined
                }]
            }
            $http.post('urlS', this, {
                code: '02020306',
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        message.success(res.errorMessage);
                        this.childUn.handleCancel(true);
                        this.setState({PrescriptionNO: '', rowKey: ''},()=>{
                            this.getCheckData();
                            this.onClickTable();
                        })
                    }
                }
            })
        }
        onOrderRef = (ref) => {
            this.childOrder = ref;
        };
        onReviewRef = (ref) => {
            this.childReview = ref;
        };
        onUnreasonableRef = (ref) => {
            this.childUn = ref;
        };
        onPreviewRef = (ref) => {
            this.childP = ref;
        }
        onAllergyRef =(ref)=>{
            this.childAllergy = ref;
        }
        onInspectionRef =(ref)=>{
            this.childInspection = ref;
        }
        onTabPaneRef=(ref)=>{
            this.childTabPane = ref;
        }
        onChargeRef=(ref)=>{
            this.childCharge = ref;
        }
        //显示入库辅助制单弹框
        checkedReview = () => {
            this.childReview.showModal(this.state.ChineseEnglishSize);
        };
        //显示不合理弹框
        checkedUnreasonable = () => {
            if(!this.state.PrescriptionNO){
                message.error('请选择点评处方数据')
               return
            }
            this.childUn.showModal(this.state.ChineseEnglishSize);
        };

        // 关闭弹窗
        handleCancel = (e) => {
            this.setState({
                visible: false
            });
        };
        // onSelect = (keys, event) => {
        //     console.log('Trigger Select', keys, event);
        // };
        //
        // onExpand = () => {
        //     console.log('Trigger Expand');
        // };

        render() {
            const { getFieldDecorator} = this.props.form;
            const {userInfo,ChineseEnglishSize,columns } = this.state;
            return (
                <div className="PrescriptionReview">
                    <Col span={4}>
                        <Row>
                            <Card
                                size="small"
                                title={<div className="card-title-left-icon"><div></div>{ChineseEnglishSize.StkTakeMaintenancelist ? ChineseEnglishSize.StkTakeMaintenancelist.descripts:'点评处方'}</div>}
                            >
                                    <Row >
                                        <Col style={{textAlign: 'center'}}>
                                            <Button onClick={this.checkedReview}>{ChineseEnglishSize.StkTakeMaintenancesearch ? ChineseEnglishSize.StkTakeMaintenancesearch.descripts:'查询'}</Button>
                                            <Button type="primary"  style={{margin: '0 20px'}} onClick={()=>{this.reasonableData(true)}}>{ChineseEnglishSize.StkTakeMaintenanceclean ? ChineseEnglishSize.StkTakeMaintenanceclean.descripts:'合理'}</Button>
                                            <Button type="primary" onClick={this.checkedUnreasonable}>{ChineseEnglishSize.StkTakeMaintenanceclean ? ChineseEnglishSize.StkTakeMaintenanceclean.descripts:'不合理'}</Button>
                                        </Col>
                                    </Row>
                            </Card>
                        </Row>
                        <Row>
                            <Col>
                                <Card
                                    size="small"
                                >
                                    <div  style={{height: store.getState().tableHeight.y +94 + 'px'}}>
                                        <Table
                                            columns={this.columns}
                                            dataSource={this.state.ReciewData}
                                            pagination={false}
                                            bordered
                                            style={{height: store.getState().tableHeight.y +61 + 'px'}}
                                            scroll={{ y: store.getState().tableHeight.y +10 }}
                                            rowKey={record => record.key}
                                            onRow={this.onClickRow}
                                            rowClassName={this.setRowClassName}
                                        />
                                        {/*<CommonPagination size='small' current={this.state.page} total={this.state.total} search= 'search'*/}
                                                          {/*onShowSizeChange={this.onShowSizeChange.bind(this)} complete={true} dataSource={this.state.ReciewData}*/}
                                                          {/*loading={this.state.loading} onChange={this.onChange.bind(this)} pageSize={true} />*/}
                                    </div>
                                </Card>
                            </Col>
                        </Row>
                    </Col>
                    <Col span={20}>
                        <Tabs onChange={this.callback} type="card">
                            <TabPane tab="医嘱明细" key="1">
                               <OrderDetails onRef={this.onOrderRef} handleThis={this} PrescriptionNO={this.state.PrescriptionNO} />
                            </TabPane>
                            <TabPane tab="处方预览" key="2">
                                <Preview onRef={this.onPreviewRef} handleThis={this} />
                            </TabPane>
                            <TabPane tab="过敏记录" key="3">
                                <AllergyRecords onRef={this.onAllergyRef} handleThis={this} />
                            </TabPane>
                            <TabPane tab="检查记录" key="4">
                              <InspectionRecords onRef={this.onInspectionRef} handleThis={this} />
                            </TabPane>
                            <TabPane tab="检验记录" key="5">
                               <TestRecord onRef={this.onTabPaneRef} handleThis={this}  />
                            </TabPane>
                            <TabPane tab="病历预览" key="6">
                                Content of Tab Pane 3666666
                            </TabPane>
                            <TabPane tab="本次医嘱" key="7">
                               <ThisTimeCharge onRef={this.onChargeRef} handleThis={this}  />
                            </TabPane>
                        </Tabs>

                    </Col>
                    <PrescriptionReviewCheck  onRef={this.onReviewRef} handleThis={this} />
                   <Unreasonable PrescriptionNO={this.state.PrescriptionNO} onRef={this.onUnreasonableRef} handleThis={this} />

                </div>
            )
        }
    }
);
export default PrescriptionReview;