/**
 * 医嘱录入列设置
 */

import React from 'react';
import {
    Card, Table, Input, Form, Select, Row, Col, Checkbox, Tabs,
    Radio, Button, Popconfirm, Icon, Divider, message, Modal,
} from 'antd';
import { Util, dayFormat } from 'tools/index';
import { $http } from 'containers/config/https';
import ShowMaintainModal from './component/ShowMaintainModal';
import store from 'store';
import moment from 'moment';
import './style/index.less';

const Search = Input.Search;
const Option = Select.Option;
const confirm = Modal.confirm;

class AdviceColumsMaintainPage extends React.Component {
    constructor(props) {
        super(props)
        var userData = JSON.parse(sessionStorage.getItem('userData'))
        this.state = {
            dataSource: [],
            total: 0,
            page: 1,
            pageSize: 10,
            userData: userData,
            btnStatus: 'HOSP'
        }
    }

    componentDidMount() {
        this.loadData()
        this.initData()
        document.querySelector('.ant-table-body').style.height = store.getState().tableHeight.y + 'px';
    }

    initData() {
        this.columns = [
            {
                title: '代码',
                dataIndex: 'code',
                width: 180,
                align: 'center',
            },
            {
                title: '描述',
                dataIndex: 'descripts',
                width: 180,
                align: 'center',
            },
            {
                title: '英文描述',
                dataIndex: 'enDescripts',
                width: 180,
                align: 'center',
            },
            {
                title: '列宽',
                dataIndex: 'width',
                width: 100,
                align: 'center',
            },
            {
                title: '隐藏',
                dataIndex: 'hide',
                width: 100,
                align: 'center',
            },
            {
                title: '其他样式',
                dataIndex: 'style',
                width: 140,
                align: 'center',
            },
            {
                title: '字段类型',
                dataIndex: 'fieldType',
                width: 180,
                align: 'center',
            },
            {
                title: '获取字段方法',
                dataIndex: 'dataMethod',
                width: 180,
                align: 'center',
            },
            {
                title: '数据属姓名',
                dataIndex: 'attribute',
                width: 180,
                align: 'center',
            },
            {
                title: '数据校验规则',
                dataIndex: 'validateRules',
                width: 180,
                align: 'center',
            },
            {
                title: '创建日期',
                dataIndex: 'createDate',
                width: 180,
                align: 'center',
            },
            {
                title: '创建时间',
                dataIndex: 'createTime',
                width: 180,
                align: 'center',
            },
            {
                title: '创建用户',
                dataIndex: 'createUser',
                width: 180,
                align: 'center',
            },
            {
                title: '更新日期',
                dataIndex: 'updateDate',
                width: 180,
                align: 'center',
            },
            {
                title: '更新时间',
                dataIndex: 'updateTime',
                width: 180,
                align: 'center',
            },
            {
                title: '更新用户',
                dataIndex: 'updateUser',
                width: 180,
                align: 'center',
            },
            {
                title: '操作',
                dataIndex: '1',
                width: 180,
                align: 'center',
                fixed: 'right',
                render: (text, data) => {
                    return (
                        <span>
                            <Icon type="edit" style={{ color: '#108EE9' }}></Icon>
                            <span className='span' onClick={this.updateTableSource.bind(this, data)}>编辑</span>
                            <Divider type="vertical" />
                            <Icon type="delete" style={{ color: 'rgba(240,65,52,1)' }}></Icon>
                            <Popconfirm title="删除不可恢复，你确定要删除吗?" onConfirm={this.handleDel.bind(this, data)} >
                                <span className='span' style={{ color: 'rgba(240,65,52,1)' }}>删除</span>
                            </Popconfirm>
                        </span >
                    )
                }
            },
        ]
    }

    handleDel(data) {
        var params = {
            params: [{
                userID: this.state.userData.userID,
                logonGroupID: this.state.userData.groupID,
                logonHospID: this.state.userData.hospID,
                id: [data.id]
            }]
        }
        $http.post('urlDeault', this, {
            code: "03010019",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    message.success('删除成功！', this.state.userData && this.state.userData.messageTime)
                    this.loadData()
                }
            }
        });
    }

    loadData() {
        var params = {
            params: [{
                userID: this.state.userData.userID,
                hospID: this.state.userData.hospID,
                groupID: this.state.userData.groupID,
                logonHospID: this.state.userData.hospID,
                logonGroupID: this.state.userData.groupID,
            }]
        }
        $http.post('urlDeault', this, {
            code: "03010020",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    this.setState({
                        dataSource: res.data
                    })
                }
            }
        });
    }

    updateTableSource(data) {
        this.setState({
            isShowMaintainModal: true,
            editData: data,
            updateID: data.id
        })
    }

    //隐藏弹出框
    hideModal(name) {
        this.setState({
            [name]: false,//将所有参数置为false
        });
    }

    onSubmit(data) {
        data['userID'] = this.state.userData.userID,
            data['hospID'] = this.state.userData.hospID,
            data['groupID'] = this.state.userData.groupID,
            data['logonGroupID'] = this.state.userData.groupID,
            data['logonHospID'] = this.state.userData.hospID,
            data['id'] = this.state.updateID
        var params = {
            params: [data]
        }
        $http.post('urlDeault', this, {
            code: "03010018",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    message.success('操作成功', this.state.userData && this.state.userData.messageTime)
                    this.loadData()
                    this.hideModal('isShowMaintainModal')
                }
            }
        });
    }

    render() {
        return <div className='columns-station-body' style={{ height: '100%' }}>
            {/**数据维护弹框 */}
            {this.state.isShowMaintainModal?<ShowMaintainModal isShow={this.state.isShowMaintainModal}
                editData={this.state.editData}
                onSubmit={this.onSubmit.bind(this)}
                onHide={this.hideModal.bind(this, 'isShowMaintainModal')}>
            </ShowMaintainModal>:null}
            <Row style={{ margin: '0 20px' }}>
                <Table columns={this.columns} dataSource={this.state.dataSource}
                    pagination={false} bordered
                    scroll={{ y: store.getState().tableHeight.y + 'px', x: 1400 }}
                    title={() => {
                        return <Button type='primary' onClick={this.updateTableSource.bind(this)}>添加</Button>
                    }}
                    rowKey={(data) => {
                        return data.id
                    }}
                ></Table>
            </Row>
        </div>
    }

}
export default AdviceColumsMaintainPage;