/**
 * 添加过敏记录
 * 2019-06-28
 * JYW
 */
import React from 'react'
import {
    Input, Table, Pagination, message, Icon, Divider, Popconfirm
} from 'antd';
import ColumnAuthoritysModel from '../../columnAuthority/ColumnAuthoritysModel'
import { $http } from 'containers/config/https';
import { Util, dayFormat } from 'tools/index';
import store from 'store';
import '../style/index.less';
var recordData, user;
class AllergyRecordsList extends React.Component {

    constructor(props) {
        recordData = JSON.parse(sessionStorage.getItem('patientData'));
        if (!recordData) {
            recordData = JSON.parse(sessionStorage.getItem('recordData'));
        }
        user = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
        super(props);
        this.state = {
            dataSource: [],
            recordData: recordData,
            findDesc: "",
            clickRow: {},
            column: [],
            ENObj:{},
            CNObj:{},
            columnOperation:[],
        }
        this.columnOperation = [];
        this.Columns = [
            {
                title: '日期',
                dataIndex: 'onsetDate',
                width: 80,
                align: 'center',
            }, {
                title: '过敏分类',
                dataIndex: 'allergyTypeDesc',
                width: 90,
                align: 'center',
            }, {
                title: '过敏源',
                dataIndex: 'allergensDesc',
                width: 90,
                align: 'center',
            }, {
                title: '过敏项目',
                dataIndex: 'medDesc',
                width: 200,
                align: 'center',
            }, {
                title: '备注',
                dataIndex: 'note',
                width: 100,
                align: 'center',
            }, {
                title: '最后更新操作员',
                dataIndex: 'updateUserName',
                width: 80,
                align: 'center',
            }, {
                title: '更新日期',
                dataIndex: 'updateDate',
                width: 80,
                align: 'center',
            }, {
                title: '过敏冲突检测',
                dataIndex: 'checkConflictDesc',
                width: 80,
                align: 'center',
          
            //  }, {
            //     title: '操作',
            //     dataIndex: 'operation',
            //     align: "center",
            //     render: (text, record) => (
            //         <span>
            //             <Icon type="edit" style={{ color: '#108EE9' }}></Icon>
            //             <span className='span' onClick={() => this.onEidtClick(record)}>编辑</span>
            //             {/* 分割线 */}
            //             <Divider type="vertical" />
            //             <Icon type="delete" style={{ color: 'rgba(240,65,52,1)' }}></Icon>
            //             <Popconfirm title="删除不可恢复，你确定要删除吗?" onConfirm={() => this.onDelete(record)} >
            //                 <span className='span' style={{ color: 'rgba(240,65,52,1)' }}>删除</span>
            //             </Popconfirm>

            //         </span>
            //     ),
            }
        ]
    }

    componentDidMount() {
        this.loadColumn()
        message.config({ top: 400, duration: 3 });
        
        this.loadPatAllergy(this.state.recordData.patID);
    }
    componentWillReceiveProps(nextProps) {
        if (nextProps.reload) {
            this.loadPatAllergy(this.state.recordData.patID);
        }
    }
    componentWillUnmount() {
        // 组件销毁前将静止让setState修改state的状态
        this.setState = (state, callback) => { return; }
    }
    loadPatAllergy = (patID) => {
        var params = {
            params: [{
                patID: patID,
                findDesc: this.state.findDesc,
            }]
        }
        $http.post('urlDeault', this, {
            code: "03050042",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    this.setState({
                        dataSource: res.result //过敏记录
                    })
                }
            }
        });
    }
    //查询条件改变事件
    onChange = (e) => {
        this.setState({
            findDesc: e.target.value,
        })
    }
    //查询条件回车事件
    onPressEnter = () => {
        this.loadPatAllergy(this.state.recordData.patID);
    }
    //编辑过敏记录
    onEidtClick = (record) => {
        this.props.hanlderThis.setState({
            editAllergy: record,
        })
    }
    //删除过敏记录
    onDelete = (record) => {
        var params = {
            params: [{
                id: record.id,
            }]
        }
        $http.post('urlDeault', this, {
            code: "03050043",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    message.success(user.language=="EN"?this.state.ENObj.AllergyListDeleteSuccess:this.state.CNObj.AllergyListDeleteSuccess);
                    this.loadPatAllergy(this.state.recordData.patID);
                    this.props.hanlderThis.setState({
                        editAllergy: {},
                    })
                }
            }
        });
    }
    //点击行
    onRowClick = (record, index, e) => {
        if (record.id === this.state.clickRow.id) {
            this.setState({
                clickRow: {},
            })
        } else {
            this.setState({
                clickRow: record,
            })
        }
    }
    ///行背景色
    setRowClassName = (row) => {
        return row.id === this.state.clickRow.id ? 'clickRowStyle' : '';
    }
    //查询列头数据
    loadColumn = () => {
        let data = {
            params: [{
                reactCode: ['AllergyList'],
                compontName: "AllergyList",
                type: "C",
            }]
        }
        $http.post('urlDeault', this, {
            code: "01040073",
            data: data,
            success: (res) => {
                if (+res.errorCode === 0) {
                    this.props.hanlderThis.setState({
                        reload: false,
                    })
                    console.log(res)
                    let column = res.result.C;
                    if (user.language == 'EN') {
                        for (let i = 0; i < column.length; i++) {
                            column[i].title = column[i].enDescripts
                        }
                    }
                    this.setState({
                        column: column,
                        ENObj : res.result.language.EN,
                        CNObj : res.result.language.CN
                    },()=>{
                        var language = user.language
                        var languageObj = {}
                        if(language == "EN" ){
                            languageObj = this.state.ENObj
                        }else{
                            languageObj = this.state.CNObj
                        }
                        var culm =  [
                            {
                                title: languageObj.AllergyListOperation!=null?languageObj.AllergyListOperation.descripts:"" , 
                                dataIndex: 'operation',
                                align: "center",
                                render: (text, record) => (
                                    <span>
                                        <Icon type="edit" style={{ color: '#108EE9' }}></Icon>
                                        <span className='span' onClick={() => this.onEidtClick(record)}>{languageObj.AllergyListEdit!=null?languageObj.AllergyListEdit.descripts:""}</span>
                                        {/* 分割线 */}
                                        <Divider type="vertical" />
                                        <Icon type="delete" style={{ color: 'rgba(240,65,52,1)' }}></Icon>
                                        <Popconfirm title={languageObj.AllergyListDeleteNotRecoverable!=null?languageObj.AllergyListDeleteNotRecoverable.descripts:""} onConfirm={() => this.onDelete(record)} >
                                            <span className='span' style={{ color: 'rgba(240,65,52,1)' }}>{languageObj.AllergyListDelete!=null?languageObj.AllergyListDelete.descripts:""}</span>
                                        </Popconfirm>
                    
                                    </span>
                                ),
                            }
                        ];
                        this.setState({
                            columnOperation:culm,
                        })
                    })
                }
            }
        })
    }
    //组件权限编辑器
    onColumnRef = (ref) => {
        this.columnRef = ref;
    }
    render() {

        let columns = [];
        if (this.state.column.length > 0) {
            columns = this.state.column.concat(this.state.columnOperation);
        }

        var language = user.language
        var languageObj = {}
        if(language == "EN" ){
            languageObj = this.state.ENObj
        }else{
            languageObj = this.state.CNObj
        }
        return (
            <div className="doctor-allergy">
                <ColumnAuthoritysModel
                    clientWidth='800px'
                    compontName="AllergyList"
                    configType="C"
                    onRef={this.onColumnRef}
                    reloadData={this.loadColumn}
                />
                <div>
                    <Input value={this.state.findDesc} onChange={this.onChange} onPressEnter={this.onPressEnter} style={{ width: 280, height: '32px', margin: '8px 14px 8px 8px' }}
                        placeholder={languageObj.AllergyListEnterProject!=null?languageObj.AllergyListEnterProject.descripts:""} />
                </div>
                <div style={{ height: 'calc(100% - 48px)' }}>
                    <Table
                        dataSource={this.state.dataSource}
                        columns={columns}
                        bordered
                        pagination={false}
                        onRow={(record, index) => ({ onClick: (e) => { this.onRowClick(record, index, e) } })}
                        rowClassName={this.setRowClassName}
                        rowKey={(data) => {
                            return data.id
                        }}
                        onHeaderRow={column => {
                            return {
                                onDoubleClick: () => {
                                    this.columnRef.onOpenModel();
                                }
                            };
                        }
                        }
                        scroll={{ y: this.props.modal == 1 ? "calc(70vh - 48px - 32px - 50px)" : 'calc(100% - 50px)' }}
                        style={{ height: this.props.modal == 1 ? "calc(70vh - 48px - 32px)" : 'calc(100% - 2px)' }}
                    >

                    </Table>
                </div>
            </div>
        )
    }
}
export default AllergyRecordsList;