/*
 * Creator:     ML
 * CreatDate:   2020-04-16
 * descripts：  左侧体检记录
 * */
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 { RangePicker } = DatePicker;
const Option = Select.Option;
const Search = Input.Search;
const TabPane = Tabs.TabPane;
const confirm = Modal.confirm;
class AssessmentLeftList extends React.Component {
    constructor(props) {
        super(props)
        var patID = JSON.parse(sessionStorage.getItem('patID'))
        this.state = {
            recordingData: [],//体检记录数据
            patID: patID,
            activeKey: '1',
            detailsId: '', //行id
        };
        // 体检记录数据表头
        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[1].style.height = store.getState().tableHeight.y + 'px';
            // listData&&listData.map((i,s)=>{
            // })
        } if (listData.length > 2) {
            listData[2].style.height = store.getState().tableHeight.y + 'px';
            // listData&&listData.map((i,s)=>{
            // })
        }
        this.initialData();
    }
    //初始数据
    initialData = () => {
        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) {
                    this.setState({ recordingData: res.result.data });
                    if (res.result.data && res.result.data.length > 0) {
                        this.setState({ detailsId: JSON.parse(sessionStorage.getItem('healthRecordID')) || res.result.data[0].ID }, () => {
                            if (!JSON.parse(sessionStorage.getItem('healthRecordID'))) {
                                window.sessionStorage.setItem("healthRecordID", JSON.stringify(res.result.data[0].ID));
                            }
                            this.props.tabThis.getDocID()
                        });
                    }
                }
            }
        });
    };
    //点击行，渲染当前行样式
    setRowClassName = (record) => {
        if (record) {
            if (this.state.detailsId == record.ID) return 'clickRowStyle';
            else return '';
        } else {
            return '';
        }
    };
    render() {
        return <div className='AssessmentLeftList'>
            <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.state.detailsId === '') {
                                        // 保存行数据以及ID
                                        this.setState({ detailsId: record.ID }, () => {
                                            this.props.tabThis.getDocID(record.ID)
                                            window.sessionStorage.setItem("healthRecordID", JSON.stringify(record.ID));
                                            let that = this
                                            setTimeout(() => {
                                                that.props.tabThis.getPress()
                                            }, 500);
                                        });
                                    } else {
                                        if (String(this.state.detailsId) !== record.ID) {9
                                            var that = this;
                                            if (sessionStorage.getItem("noSave") && JSON.parse(sessionStorage.getItem("noSave")) === true) {
                                                confirm({
                                                    title: '提示',
                                                    content: '当前页面数据还没保存，确定要切换页面吗？',
                                                    okText:  '确定',
                                                    cancelText: '取消',
                                                    onOk() {
                                                        window.sessionStorage.setItem("noSave",false);
                                                        that.setState({ detailsId: record.ID }, () => {
                                                            that.props.tabThis.getDocID(record.ID)
                                                            window.sessionStorage.setItem("healthRecordID", JSON.stringify(record.ID));
                                                            setTimeout(() => {
                                                                that.props.tabThis.getPress()
                                                            }, 500);
                                                        });
                                                    },
                                                    onCancel() {
                                                        return
                                                    }
                                                })
                                            } else {
                                                that.setState({ detailsId: record.ID }, () => {
                                                    that.props.tabThis.getDocID(record.ID)
                                                    window.sessionStorage.setItem("healthRecordID", JSON.stringify(record.ID));
                                                   
                                                    setTimeout(() => {
                                                        that.props.tabThis.getPress()
                                                    }, 500);
                                                });
                                            }
                                           
                                        } else {
                                            
                                        }
                                    }
                                },

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

}

export default AssessmentLeftList;
