import React from 'react';
import '../../assets/css/system.css';
import store from '../../store';
import {
    Table, Popconfirm, Row, Col, Button, Pagination, message, Divider, Spin,Select
  } from 'antd';
import { $http } from '../../containers/config/https';
import MedGroupUserModal from './MedGroupUserModal.jsx'
// 主菜单弹框
const Option = Select.Option;

export default  class EditableTable extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            userInfo: {},
            // 主菜单列表数据
            data: [],
            // 抽屉显示
            visible: false,
            //医疗分组ID eg:"92||2"
            id : '',
            // 主菜单分页数据
            total: 0,
            page: 1,
            pageSize: 10,
            flag: '',
            title: '',
            // 是否显示loading
            showLoading: true,
            // 表格loading
            loading: true,
            selectGroupId: '',
            buttonDisplay:'none'
        };
        this.BasicClassData=[]

        this.columns = [
            {
                title: '医务人员',
                dataIndex: 'medStaffDesc',
                width: 200,
                align: "center"
            },
            {
                title: '门诊标志',
                dataIndex: 'oPFlag',
                width: 100,
                align: "center"
            },
            {
                title: '住院标志',
                dataIndex: 'iPFlag',
                width: 100,
                align: "center"
            }, 
            {
                title: '组长标志',
                dataIndex: 'leaderFlag',
                width: 100,
                align: "center"
            },
            {
                title: '开始日期',
                dataIndex: 'startDate',
                width: 100,
                align: "center"
            },
            {
                title: '失效日期',
                dataIndex: 'stopDate',
                width: 100,
                align: "center"
            },
            {
                title: '更新日期',
                dataIndex: 'updateDate',
                width: 100,
                align: "center"
            },
            {
                title: '更新人',
                dataIndex: 'updateUserDrDesc',
                width: 100,
                align: "center"
            },
            {
                title: '操作',
                dataIndex: 'operation',
                align: "center",
                width: 150,
                render: (text,record) => (
                    <span>
                        <span className='span' onClick={this.handleAdd}>新增</span>
                        <Divider type="vertical" />
                        <span className='span' onClick={this.handleCompile.bind(this, record)}>编辑</span>
                        {/* onConfirm-- 点击确认的回调*/}
                        {/* 分割线 */}
                        <Divider type="vertical" />
                        <Popconfirm title="删除不可恢复，你确定要删除吗?" onConfirm={this.handleDetele.bind(this, record)} >
                            <span className='span'>删除</span>
                        </Popconfirm>
                    </span>
                )
            },
        ];
    }
    

    // 查询
    handleQuery = () => {
        let {page, pageSize} = this.state;
        let groupId = this.state.selectGroupId;
        let data = {}
        if (groupId) {
            data = {
                params: [{ 
                    Id: groupId,
                }],
                pagination:[{
                    pageSize: pageSize,
                    currentPage: page,
                    sortColumn: "Code",
                    sortOrder: "asc"
                }]
            }
        } else {
            data = {
                params: [{
                    Id: "92||2",
                }],
                pagination:[{
                    pageSize: pageSize,
                    currentPage: page,
                    sortColumn: "Code",
                    sortOrder: "asc"
                }]
            }
        }
        $http.post('urlS', this, {
        	code: '10003nzs',
            data: data,
            success: function (res) {
                if (+res.errorCode === 0 && res.result) {
                for(var i = 0; i < res.result.Data.length; i++) {
                    res.result.Data[i].key = i
                }
                this.setState({data: res.result.Data, total: res.result.TotalCount, loading: false,buttonDisplay:'none'})
                } else {
                    setTimeout(() => {
                        this.setState({data: [], total: 0, loading: false,buttonDisplay:'block'})
                    }, 500)
                }
            }
        })
    }
  
    // 新增
    handleAdd = () => {
        // 显示弹框
        //this.child.showModal()
        this.child.showDrawer();
        // 初始化数据
        this.setState({flag: 'add', title: '新增', selectGroupId : this.state.selectGroupId})
    }


    // 编辑
    handleCompile(record) {
        // 重置form表单
        this.child.resetFields()
        // 打开弹框
        this.child.showDrawer();
        // 初始化数据
        this.setState({flag: 'compile', userInfo: record, title: '编辑', selectGroupId : this.state.selectGroupId});
    }
    
    // 删除
    handleDetele(record) {
        let data = {
            params: [{
                ID: record.ID,
                updateUserDr: '1'
            }]
        }
        $http.post('urlADS', this, {
            code: '10005nzs',
            data: data,
            success: function (res) { 
                if (+res.errorCode === 0) {
                    message.success('删除成功');
                    // 刷新列表数据
                    this.handleQuery()
                } else {
                    message.error('删除失败：' + res.errorMessage);
                }
            },
            error:function (error) {
                message.error('删除失败：' + error);
            }
        })
    }
    // 页码改变的回调，参数是改变后的页码及每页条数
    onChange = (page, pageSize)=> {
        this.setState({page: page, pageSize: pageSize}, () => {
            // 重新调用查询方法
            this.handleQuery();
        });
    }
    // 调用子组件的连接事件
    onRef = (ref) => {
        this.child = ref
    }
    // 重置userInfo
    resetUserInfo() {
        this.setState({userInfo: {}})
    }

    componentWillReceiveProps(){
        
        if ((this.props.handelThis.state.rowId)&&(this.props.handelThis.state.rowId != this.state.selectGroupId)&&(this.props.handelThis.state.key === '2')){
            this.setState({
                
                selectGroupId:this.props.handelThis.state.rowId
            },()=>{
               
                this.handleQuery();
            });
        }
    }


    componentDidMount() {
        // 初始化列表数据
        this.handleQuery();
        // 初始化设置table的高度以及表体的高度
        document.querySelector('.ant-table-body').style.height = store.getState().tableHeight.y + 'px';
        // 数据异步请求，请求成功之后setState
		this.setState({
			showLoading: false
		})
    }

    componentWillMount() {
        this.GetBasicClassData()
    }
    GetBasicClassData=()=>{
        let data = {
            params: [{}]
        }
        $http.post("urlS",this, {
            code: "nzs001",
            data: data,
            success: function (res) { 
                if (+res.errorCode === 0) {
                    this.BasicClassData=res.result
                } else {
                    message.error('失败：' + res.errorMessage);
                }
            }
        }) 
    }

    SelectChange = (value,optionObj) => {
        this.setState({
            selectGroupId : value,
        });
    }


    
    render() {
        
        return (
            <Spin tip="加载中..." spinning={this.state.showLoading}>
                <div>
                    <div style={{ padding: 24, marginBottom: 24 }}>
                        <div className="table-operations" style={{marginBottom: '30px',display:this.state.buttonDisplay}} >
                            <Row>
                                <Col span={24} >
                                <Button type="primary"  onClick={this.handleAdd}>新增</Button>
                                </Col>
                            </Row>
                        </div>
                        <div>
                            <Table
                                style={{height: store.getState().tableHeight.y - 10 + 'px'}}
                                dataSource={this.state.data}
                                columns={this.columns}
                                rowClassName="editable-row"
                                pagination={false}
                                scroll={{ x: 1000, y: store.getState().tableHeight.y }}
                                bordered='true'
                                loading={this.state.loading}
                            />
                            {/* 分页: total--总条数  defaultCurrent--默认的当前页 hideOnSinglePage--只有一页时隐藏分页器 */}
                            <Pagination
                                size="small"
                                showQuickJumper
                                showSizeChanger
                                defaultCurrent={1}
                                total={this.state.total}
                                onChange={this.onChange}
                                onShowSizeChange={this.onShowSizeChange}
                                style={{textAlign: "right", marginTop: '13px'}}
                            />
                        </div>
                    </div>

                    {/*新增弹框*/}
                    <MedGroupUserModal
                        onRef={this.onRef}
                        userInfo={this.state.userInfo}
                        flag={this.state.flag}
                        handelThis={this}
                        title={this.state.title}
                    />
                </div>
            </Spin>
        );
    }
  }
  