/*
* 科室
* */

import React, { Component } from 'react';
import { Input, Form, Select, Modal, Button, Row, Col, Table, Card, Tabs, Checkbox, message } from 'antd';
import { $http } from 'containers/config/https';
import { Util } from 'tools/index';
import '../style/index.less';

const Option = Select.Option;
const FormItem = Form.Item;
const TabPane = Tabs.TabPane;

class ShowRoleModal extends Component {

    constructor(props) {
        var userData = JSON.parse(sessionStorage.getItem('userData'));
        super(props);
        this.state = {
            dataSource: [],
            data: [{}],
            saveData: [],
            userData: userData,
            showKey: '1',
            newData: [],
            docList: []
        }
    }

    componentWillReceiveProps(nextProp) {
        if (nextProp.isShow === true) {
            this.setState({
                modalId: nextProp.modalId,
                resourceType: nextProp.resourceType,
                resourceTypeID: nextProp.resourceTypeID,
            }, () => {
                this.loadSelectData(nextProp.modalId)
            })
        } else if (this.props.isShow === false) {
            this.cleanInput();
        }
    }

    componentDidMount() {
        this.initData()
    }

    initData() {
        this.columns = [
            {
                title: '医生',
                dataIndex: 'docName',
                width: 120,
                align: 'center'
            }, {
                title: '更新时间',
                dataIndex: 'date',
                width: 120,
                align: 'center',
            }, {
                title: '操作',
                dataIndex: 'appointNum',
                width: 120,
                align: 'center',
                render: (text, data) => {
                    return <span style={{ cursor: 'pointer', color: '#1890ff' }} onClick={this.delLocMessage.bind(this, data)}>删除</span>
                }
            }
        ]
    }

    delLocMessage(data) {
        var id = []
        id.push(data.id)
        var params = {
            params: [{
                id: data ? id : this.state.ids,
                logonUserID: this.state.userData.userID,
                resourceTypeID: this.state.resourceTypeID
            }]
        }
        $http.post('urlDeault', this, {
            code: "03010016",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    message.success('删除成功！')
                    this.loadSelectData()
                    this.setState({
                        ids: []
                    })
                }
            }
        });
    }

    loadSelectData(id) {
        var params = {
            params: [{
                id: id || this.state.modalId,
                resourceTypeID: this.state.resourceTypeID
            }]
        }
        $http.post('urlDeault', this, {
            code: "03010014",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    this.setState({
                        otherDocList: res.data.otherDocList,
                        docList: res.data.docList,
                    })
                }
            }
        });
    }

    addLocDoctor() {
        var params = {
            params: [{
                docID: this.state.selectID,
                logonUserID: this.state.userData.userID,
                id: this.state.modalId,
                resourceTypeID: this.state.resourceTypeID
            }]
        }
        $http.post('urlDeault', this, {
            code: "03010015",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    message.success('添加成功！')
                    this.loadSelectData()
                }
            }
        });
    }


    closeModal() {
        let { onHide } = this.props;
        onHide && onHide();
        this.cleanInput();
    }

    //清空输入框
    cleanInput() {
        this.props.form.resetFields();
    }

    //提交
    onSubmit() {
    }

    changeInput(e) {
        this.setState({
            selectID: e
        })
    }
    onSelectChange(ids) {
        this.setState({
            ids
        })
    }

    render() {
        var { otherDocList, ids } = this.state;
        const rowSelection = {
            ids,
            onChange: this.onSelectChange.bind(this),
            selectedRowKeys: ids,
        }
        var otherDocListOption = []
        otherDocList && otherDocList.map((item, index) => {
            otherDocListOption.push(<Option key={index} value={item.id} code={item.descriptsSPCode}>{item.descripts}</Option>)
        })

        return <Modal className='departModalStyle'
            title={'科室'}
            maskClosable={false}
            visible={this.props.isShow}
            onCancel={this.closeModal.bind(this)}
            footer={null}
        // onOk={this.onSubmit.bind(this)}
        >
            <div>
                <div className="modal-body">

                    <div>
                        <Table columns={this.columns} style={{ width: '100%' }}
                            dataSource={this.state.docList}
                            loadData={this.loadSelectData.bind(this)}
                            rowSelection={rowSelection}
                            title={() => {
                                return <div style={{ display: 'flex' }}>
                                    <Select style={{ width: 180 }} allowClear showSearch
                                        filterOption={(input, option) =>
                                            option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
                                            || option.props.code.toLowerCase().indexOf(input.toLowerCase()) >= 0
                                        }
                                        onChange={this.changeInput.bind(this)}>{otherDocListOption}</Select>
                                    <Button style={{ marginLeft: 10 }} onClick={this.addLocDoctor.bind(this)}>添加</Button>
                                    <Button style={{ marginLeft: 10 }} onClick={this.delLocMessage.bind(this, '')}>删除</Button>
                                </div>
                            }}
                            bordered
                            rowKey={(data) => {
                                return data.id
                            }}
                            pagination={false}></Table>
                    </div>
                </div>
            </div>
        </Modal>
    }
}

export default Form.create()(ShowRoleModal);