// 病区床位管理
import React from 'react';
import { Select, Row, Col, Button, Divider, Popconfirm, message, Icon, Form, DatePicker, Table } from 'antd';
import { $http } from '../../../containers/config/https';
import moment from 'moment'

// 组件const
const { Option } = Select;




export default class MedGroupBedModal extends React.Component {
    constructor( props ) {
        super(props)
        this.state = {
            dataMembe: [],  // 添加分组数据
            hospId: '',  // 医院下拉id
            locId: '',  // 科室id
            membeSelectData: [],//组员下拉框数据
            GroupBedModalDate: [],//回显数据
            medGrpId: '',//分组主键
            locBedID: '',//床位主键
            tableData:[],
            medGroupID: props.medGroupID,
            locBedData:[]    //科室床位下拉框数据
        };
        // 添加分组table
        this.columnsMedBed = [{
            title: '当前诊疗组',
            dataIndex: 'medGroupDesc',
            key: '1',
            align: "center",
        }, {
            title: '床号',
            dataIndex: 'bedNo',
            key: '2',
            align: "center",
        }, 
        // {
        //     title: '开始日期',
        //     dataIndex: 'medBedStartDate',
        //     key: '3',
        //     align: "center",
        //     render: (text,record)=>{
        //         return (
        //             <DatePicker value={text!==""?moment(text,'YYYY-MM-DD'):null} />
        //         )
        //     }
        // }, 
        // {
        //     title: '截止日期',
        //     dataIndex: 'medBedEndDate',
        //     key: '4',
        //     align: "center",
        // }, 
        {
            title: '操作',
            key: '5',
            align: "center",
            render: (text, record) => (
                <span>
                    <Icon type="delete" style={{ color: 'rgba(240,65,52,1)' }}></Icon>
                    <Popconfirm title="删除不可恢复，你确定要删除吗?" onConfirm={() => this.handleDelete(record)} >
                        <span className='span' style={{ color: 'rgba(240,65,52,1)' }}>删除</span>
                    </Popconfirm>
                    {/* <Divider type="vertical" />
                    {record.medGroupUserLeader === 'Y' ? <span className='span' onClick={() => this.handleEdit(record, 'N')} style={{ color: 'rgba(240,65,52,1)' }} >取消组长</span> : <span className='span' onClick={() => this.handleEdit(record, 'Y')} >设为组长</span>} */}
                </span>
            ),
        }]

    }

    componentDidMount() {
        if (this.props.GroupBedModalDate && JSON.stringify(this.props.GroupBedModalDate) !== "{}") {
            let data = this.props.GroupBedModalDate
            this.setState({
                hospId: data.hospID,
                locId: data.locID, //  回显数据
                medGrpId: data.medGroupID,
            },()=>{
                this.locBedSelect();
                this.medGrpUserAry();
            });
            // this.callbackData(data) //回显数据

        }
    }

    // // 实时更新
    // componentWillReceiveProps(nextProps) {
    //     const { GroupBedModalDate } = this.state
    //     const newdata = nextProps.GroupBedModalDate


    //     if (GroupBedModalDate !== newdata) {
    //         this.callbackData(nextProps.GroupBedModalDate) //回显数据
    //     }

    // }

    //回显数据
    callbackData = (data) => {
        this.setState({
            hospId: data.hospID,
            locId: data.locID, //  回显数据
            medGrpId: data.medGroupID,
        }, () => {
            // 组员下拉框加载
            this.membeSelect()
            this.medGrpUserAry()//列表数据
        })
    }

    //列表数据
    medGrpUserAry = () => {
        let data = {
            "params": [{
                medGroupID: this.state.medGrpId,
            }]
        };
        $http.post('urlADS', this, {
            code: "01040075",
            data: data,
            success: function (res) {
                let errorCode = res.errorCode;
                if (errorCode === '0') {
                    for (var i = 0; i < res.result.Data.length; i++) {
                        res.result.Data[i].key = i;
                    }
                    this.setState({
                        tableData: res.result.Data,
                    })
                }
            }
        });
    }

    startDateChange = (date, dateString) => {
        console.log(date, dateString);
    }

    //删除组员
    handleDelete = (record) => {
        let data = {
            "params": [{
                medGroupBedID : record.medGroupBedID,
                updateUserID: JSON.parse(window.sessionStorage.userData).userID, //修改人
            }]
        };
        $http.post('urlADS', this, {
            code: "01040077",
            data: data,
            success: function (res) {
                let errorCode = res.errorCode;
                if (errorCode === '0') {
                    message.success(res.errorMessage);
                    this.membeSelect()  // 组员下拉框加载
                    this.medGrpUserAry()//列表数据
                }
            }
        });
    }

    //设为取消组长
    handleEdit = (record, str) => {
        let data = {
            "params": [{
                medGroupUserLogID: record.medGroupUserLogID,
                updateUserID: JSON.parse(window.sessionStorage.userData).userID, //修改人
                medGroupID: this.state.medGrpId,
                medGroupUserLeader: str
            }]
        };
        $http.post('urlADS', this, {
            code: "01040048",
            data: data,
            success: function (res) {
                let errorCode = res.errorCode;
                if (errorCode === '0') {
                    message.success(res.errorMessage);
                    this.medGrpUserAry()//列表数据
                }
            }
        });
    }


    // 组员下拉框加载
    membeSelect = () => {
        let data = {
            "params": [{
                hospID: this.state.hospId,
                locID: this.state.locId,
                selectFlag: 'Y'

            }]
        };
        $http.post('urlDeault', this, {
            code: "01040021",
            data: data,
            success: function (res) {
                let errorCode = res.errorCode;
                if (errorCode === '0') {
                    this.setState({
                        membeSelectData: res.result.Data, // 科室集合
                    })
                }
            }
        });
    }

    // 科室床位下拉框加载
    locBedSelect = () => {
        // console.log();
        // this.state.locBedData=[];
        let data = {
            "params": [{
                hospID: this.state.hospId,
                locID: this.state.locId,
                medGroupID:this.state.medGrpId,
                selectFlag: '1',
                statusID:'1'
            }]
        };
        $http.post('urlDeault', this, {
            code: "01050021",
            data: data,
            success: function (res) {
                let errorCode = res.errorCode;
                if (errorCode === '0') {
                    this.setState({
                        locBedData: res.result.Data, // 科室床位集合
                    })
                }
            }
        });
    }


    locBedChange = (event) => {
        this.setState({
            locBedID: event
        })
    }

    // 添加人员分组负责床位
    handleAdd = () => {
        let data = {
            "params": [{
                locBedID: this.state.locBedID,
                medGroupID : this.state.medGrpId,
                updateUserID: JSON.parse(window.sessionStorage.userData).userID,
                medBedStartDate: moment().format('YYYY-MM-DD')
            }]
        };
        $http.post('urlADS', this, {
            code: "01040076",
            data: data,
            success: function (res) {
                let errorCode = res.errorCode;
                if (errorCode === '0') {
                    message.success(res.errorMessage);
                    this.setState({
                        locBedData:[]
                    }
                        ,()=>{this.locBedSelect()})
                    this.medGrpUserAry()//列表数据
                }
            }
        });
    }

    render() {
        return (
            <div>
                <Row className="mB30">
                    <Col span={18} >
                        <Select 
                            style={{ width: '60%' }} 
                            placeholder="选择科室床位"  
                            onChange={(event) => { this.locBedChange(event) }}
                        >
                            {this.state.locBedData.map(item => 
                                <Option key={item.id} value={item.id}>
                                {item.descripts+"床"}
                                </Option>)
                            }
                        </Select>
                    </Col>
                    <Col span={4}  style={{ textAlign: 'right' }}>
                        <Button icon="plus" onClick={this.handleAdd} style={{ background: 'rgba(247,128,93,1)', color: '#fff' }}>加入</Button>
                    </Col>
                </Row>
                <Row >
                    <Col span={24} >
                        <Table bordered dataSource={this.state.tableData} columns={this.columnsMedBed} pagination={false} style={{ maxHeight: '400px', minHeight: '200px', overflow: 'auto' }} />
                    </Col>
                </Row>
            </div>
        )
    }
}
