import React, { Component } from 'react';
import { Collapse, Table, message, Button, Icon, Row, Col } from 'antd';
import DoctorOrderForm from './DoctorOrderForm'
import { $http } from '../../containers/config/https';

const Panel = Collapse.Panel;
let user;
class DoctorOrder extends Component {

    constructor(props) {
        super(props);
        this.state = {
            ordCatData: [],
            ordCatColumn: [
                {
                    title: "医嘱分类",
                    dataIndex: "descripts",
                    key: "descripts",
                    align: 'left',
                }
            ],
            recLocColumn: [
                {
                    title: "接受科室",
                    dataIndex: "descripts",
                    key: "descripts",
                    align: 'left',
                }
            ],
            priority: [],
            recLoc: [],
            ordCatSelectedRowKeys: [],
            recLocSelectedRowKeys: [],
            fromData: {},
            MedItem: [],
            showHeader: false,
        }
      user= window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
    }

    saveData = () => {
        let ordCat = this.state.ordCatSelectedRowKeys;
        if (ordCat.length !== 0) { ordCat = ordCat.join("^") }
        else { ordCat = "" }
        let recLoc = this.state.recLocSelectedRowKeys;
        if (recLoc.length !== 0) { recLoc = recLoc.join("^") }
        else { recLoc = "" }
        let data = {
            params: [{
                hospID: this.props.hospID,
                type: this.props.ConfigType,
                userID: user.userID,
                configData: {
                    ordCat: ordCat,
                    recLoc: recLoc,
                    baseData: this.child.props.form.getFieldsValue(),

                },
            }]
        }

        $http.post('urlDeault', this, {
            code: "04010001",
            data: data,
            success: (res) => {
                if (+res.errorCode === 0) {
                    message.success("保存成功!");
                } else {
                    message.error('保存失败' + res.errorMessage);
                }
            }
        })

    }
    getData = (hospID) => {

        let data = {
            params: [{
                hospID: hospID,
                type: this.props.ConfigType,
            }]
        }

        $http.post('urlDeault', this, {
            code: "04010002",
            data: data,
            success: (res) => {
                if (+res.errorCode === 0) {
                    this.child.resetFields();
                    this.setState({
                        ordCatData: res.result.orderCatData,
                        recLoc: res.result.locList,
                        fromData: res.result.docConfig,
                        ordCatSelectedRowKeys: res.result.ordCatKeys,
                        recLocSelectedRowKeys: res.result.recLocKeys,
                        MedItem: res.result.medItem,
                    });
                } else {
                    message.error('查询失败：' + res.errorMessage);
                }
            }
        })
    }
    componentWillUnmount() {
        this.setState = (state, callback) => {
            return
        }
    }
    componentDidMount() {
        this.getData(this.props.hospID);
    }
    componentWillReceiveProps(nextProps) {
        if (this.props.hospID !== nextProps.hospID) {
            if (nextProps.hospID !== "") {
                this.getData(nextProps.hospID);
            }
        }
    }
    // 调用公共组件的连接事件
    onRef = (ref) => {
        this.child = ref
    }
    render() {
        const ordCatSelection = {
            onChange: (selectedRowKeys, selectedRows) => {

                this.setState({ ordCatSelectedRowKeys: selectedRowKeys });
            },
            onSelect: (record, selected, selectedRows) => {
                if (record.children) {
                    //选择父节点时自动勾选子节点数据
                    let id = record.id;
                    let selectedRowKeys = this.state.ordCatSelectedRowKeys;
                    if (selected) {
                        for (var i = 0; i < record.children.length; i++) {
                            let id = "^" + record.children[i].id + "^";
                            if (("^" + selectedRowKeys.join("^") + "^").indexOf(id) < 0) {
                                selectedRowKeys.push(record.children[i].id);
                            }
                        }
                        if (("^" + selectedRowKeys.join("^") + "^").indexOf("^" + id + "^") < 0) {
                            selectedRowKeys.push(id);
                        }
                        this.setState({ ordCatSelectedRowKeys: selectedRowKeys });
                    }
                    else {
                        let newkey = [];
                        for (var i = 0; i < selectedRowKeys.length; i++) {

                            let key = selectedRowKeys[i];
                            if (((("^" + key).indexOf("^" + id + "||")) < 0) && ((("^" + key + "^").indexOf("^" + id + "^")) < 0)) {
                                newkey.push(key);
                            }
                        }
                        this.setState({ ordCatSelectedRowKeys: newkey });

                    }
                }

            },
            selectedRowKeys: this.state.ordCatSelectedRowKeys,
        };
        const recLocSelection = {
            onChange: (selectedRowKeys, selectedRows) => {
                this.setState({ recLocSelectedRowKeys: selectedRowKeys });
            },
            selectedRowKeys: this.state.recLocSelectedRowKeys,
        }
        const saveConfig = () => (
            <Button
                type="primary"
                onClick={(event) => {
                    event.stopPropagation();
                    this.saveData();
                }} >
                <Icon type="save" />
                保存
            </Button>
        );

        return (
            <Row>
                <Col span={24}>
                    <Button
                        type="primary"
                        onClick={(event) => {
                            event.stopPropagation();
                            this.saveData();
                        }} >
                        <Icon type="save" />
                        保存
            </Button>
                </Col>
                <Col span={24}>
                    <Collapse
                        accordion
                        defaultActiveKey="1"
                        style={{ width: "80%" }}>
                        <Panel header="基础配置" key="1" extra={saveConfig()}>
                            <DoctorOrderForm onRef={this.onRef} FromData={this.state.fromData} MedItem={this.state.MedItem} />
                        </Panel>

                        <Panel header="屏蔽医嘱分类" key="2" extra={saveConfig()}>
                            <Table
                                columns={this.state.ordCatColumn}
                                rowSelection={ordCatSelection}
                                showHeader={this.state.showHeader}
                                dataSource={this.state.ordCatData}
                                rowKey={(row) => {
                                    return row.id;
                                }}
                                indentSize={20}
                                scroll={{ y: 600 }}
                                pagination={false}
                            />
                        </Panel>
                        <Panel header="屏蔽科室" key="3" extra={saveConfig()}>
                            <Table
                                showHeader={this.state.showHeader}
                                columns={this.state.recLocColumn}
                                rowSelection={recLocSelection}
                                dataSource={this.state.recLoc}
                                rowKey={(row) => {
                                    return row.id;
                                }}
                                indentSize={20}
                                scroll={{ y: 630 }}
                                pagination={false}
                            />
                        </Panel>

                    </Collapse>
                </Col>
            </Row>
        )
    };
}
export default DoctorOrder