/**
 *健康体检记录添加弹框

 */
import React from 'react';
import {
    Card, Table, Input, Form, Select, Row, Col, Checkbox, Tabs,
    DatePicker, Button, Popconfirm, Icon, Divider, message, Modal,
} from 'antd';
import { Util, dayFormat } from 'tools/index';
import { $http } from 'containers/config/https';
import store from 'store';
import moment from 'moment';
import '../style/Secondary.less';
const confirm = Modal.confirm;
const { RangePicker } = DatePicker;
const Option = Select.Option;
const Search = Input.Search;
const TabPane = Tabs.TabPane;

class RecordingLeft extends React.Component {
    constructor(props) {
        super(props)
        var patID = JSON.parse(sessionStorage.getItem('patID'));
        this.state = {
            recordingData: [],//体检记录数据
            patID: patID,
            activeKey:'1',
            detailsId: '', //行id
            healthList:{},//行内容
        };
        // 体检记录数据表头
        this.columns = [
            {
                key: 1,
                title: '体检日期',
                dataIndex: 'healthDate',
                align: "center",
                width: '50%'
            },
            {
                key: 2,
                title: '责任医生',
                dataIndex: 'doctorDesc',
                align: "center",
                width: '50%'
            }
        ];
    }

    componentDidMount() {
        this.props.onRef(this);
        let listData = document.querySelectorAll('.recordingHeight  .ant-table-body');
        if (listData.length > 0) {
            listData[0].style.height = store.getState().tableHeight.y + 'px';
        }
        this.initialData();

    }
    componentWillUnmount(){
        window.sessionStorage.setItem("changeValue",JSON.stringify(false));
    }
    //初始数据
    initialData=(val)=>{
        let data = {
            params: [{
                patientID:this.state.patID ? this.state.patID : '',
            }]
        };
        $http.post('urlS', this, {
            code: "03070022",
            data: data,
            success: function (res) {
                if (+res.errorCode === 0) {
                    // if(res.result.data.length==0){
                    //     window.sessionStorage.setItem("changeValue",JSON.stringify(true));
                    // }
                    this.setState({recordingData:res.result.data});
                    if(res.result.data&&res.result.data.length>0){
                        window.sessionStorage.setItem("healthRecordID",JSON.stringify(val? val : res.result.data[0].ID));
                        this.setState({detailsId: val? val:res.result.data[0].ID ,healthList: res.result.data[0]},()=>{
                            this.props.handleThis.getList(val? val:'');
                                this.props.handleThis.detailsData(val? val:res.result.data[0].ID);
                                this.props.handleThis.getSchedule();
                                this.props.handleThis.getPromptData();
                        });
                    }else {
                        this.setState({healthList:{}})
                        this.props.handleThis.getList();
                        sessionStorage.removeItem("healthRecordID");
                        // sessionStorage.removeItem("changeValue");
                        // this.props.handleThis.clearFileList();
                    }
                }
            }
        });
    };
    //点击行，渲染当前行样式
    setRowClassName = (record) => {
        if(record){
            if(this.state.detailsId == record.ID) return 'clickRowStyle';
            else return '';
        }else {
            return '';
        }
    };
    changeExamination=()=>{

        if(JSON.parse(sessionStorage.getItem('changeValue'))){
            return true
        }else {
            return false
        }
    }
    setChangeValue=()=>{
        window.sessionStorage.setItem("changeValue",JSON.stringify(false));
    }
    render() {
        return <div className='RecordingLeft'>
            <Card
                size="small"
                title={<div className="card-title-left-icon"><div></div>健康体检记录</div>}
            >
                <div>
                    <Table
                        columns={this.columns}
                        dataSource={this.state.recordingData}
                        pagination={false}
                        bordered
                        scroll={{x:'100%', y: store.getState().tableHeight.y-27 }}
                        rowKey={record => record.key}
                        className="recordingHeight"
                        rowClassName={this.setRowClassName}
                        onRow={(record) => {
                            return {
                                onClick: () => {
                                    if(this.changeExamination()){
                                        let that = this;
                                        confirm({
                                            title: '提示',
                                            content: '当前页面数据还没保存，确定要切换页面吗？',
                                            okText:  '确定',
                                            cancelText: '取消',
                                            onOk() {
                                                that.setChangeValue();
                                                // that.props.handleThis.clearForm()
                                                that.props.handleThis.setState({disabled: ''});
                                                if (that.state.detailsId === '') {
                                                    // 保存行数据以及ID
                                                    that.setState({detailsId:record.ID,healthList:record}, () => {
                                                        that.props.handleThis.getList();
                                                        that.props.handleThis.detailsData(record.ID);
                                                        that.props.handleThis.getSchedule();
                                                        that.props.handleThis.getPromptData();
                                                        that.props.handleThis.noClickChildTabs()
                                                    });
                                                    window.sessionStorage.setItem("healthRecordID", JSON.stringify(record.ID));

                                                } else {
                                                    if (that.state.detailsId !== record.ID) {
                                                        that.props.handleThis.clearForm()
                                                        that.setState({detailsId:record.ID,healthList:record}, () => {
                                                            that.props.handleThis.getList();
                                                            that.props.handleThis.detailsData(record.ID);
                                                            that.props.handleThis.getSchedule();
                                                            that.props.handleThis.getPromptData();
                                                            that.props.handleThis.noClickChildTabs()
                                                        });
                                                        window.sessionStorage.setItem("healthRecordID", JSON.stringify(record.ID));
                                                    }
                                                    // else {
                                                    //     sessionStorage.removeItem('healthRecordID');
                                                    //     sessionStorage.removeItem("changeValue");
                                                    // }
                                                }
                                            },
                                            onCancel() {
                                                return
                                            }
                                        })
                                    }else {
                                        this.props.handleThis.setState({disabled: ''});
                                        if (this.state.detailsId === '') {
                                            // 保存行数据以及ID
                                            this.setState({detailsId:record.ID,healthList:record}, () => {
                                                this.props.handleThis.getList();
                                                this.props.handleThis.detailsData(record.ID);
                                                this.props.handleThis.getSchedule();
                                                this.props.handleThis.getPromptData();
                                                this.props.handleThis.noClickChildTabs()
                                            });
                                            window.sessionStorage.setItem("healthRecordID", JSON.stringify(record.ID));

                                        } else {
                                            if (this.state.detailsId !== record.ID) {
                                                this.props.handleThis.clearForm()
                                                this.setChangeValue();
                                                this.setState({detailsId:record.ID,healthList:record}, () => {
                                                    this.props.handleThis.getList();
                                                    this.props.handleThis.detailsData(record.ID);
                                                    this.props.handleThis.getSchedule();
                                                    this.props.handleThis.getPromptData();
                                                    this.props.handleThis.noClickChildTabs()
                                                });
                                                window.sessionStorage.setItem("healthRecordID", JSON.stringify(record.ID));
                                            }
                                            // else {
                                            //     sessionStorage.removeItem('healthRecordID');
                                            //     sessionStorage.removeItem("changeValue");
                                            // }
                                        }
                                    }

                                },

                            };
                        }}
                    />
                </div>
            </Card>
        </div>
    }

}

export default RecordingLeft;
