import React, { Component } from 'react';
import { Spin, Button, Col, Row, message } from 'antd';
import { $http } from '../../containers/config/https';
import { Util } from '../../tools';
import CBConfigTable from './CBConfigTable'

class CBModuleConfigMul extends Component {
    constructor(props) {
        super(props);
        this.state = {
            configData: [],
            spinLoading: false
        };
    }

    componentDidMount() {
        let moduleCode = ''
        if (this.props.location) {
            if (this.props.location.state) {
                if (this.props.location.state.moduleCode) {
                    moduleCode = this.props.location.state.moduleCode ? this.props.location.state.moduleCode : '';
                }
            }
        } else {
            if (this.props.paras) {
                moduleCode = this.props.paras.moduleCode ? this.props.paras.moduleCode : '';
            } else {
                moduleCode = this.props.moduleCode ? this.props.moduleCode : '';
            }
        }
        if (moduleCode === '') return
        this.GetConfig(moduleCode)
    }

    GetConfig = (moduleCode) => {
        this.setState({ spinLoading: true });
        const userData = sessionStorage.userData ? JSON.parse(sessionStorage.userData) : {}
        let data = {
            params: [{
                hospID: userData.hospID,
                moduleCode: moduleCode,
                mulFlag: 'Y'
            }]
        }
        $http.post('urlS', this, {
            code: '01040071',
            data: data,
            success: function (res) {
                if (+res.errorCode === 0) {
                    this.setState({
                        spinLoading: false,
                        configData: res.result
                    })
                } else {
                    message.error('失败：' + res.errorMessage);
                    this.setState({ spinLoading: false });
                }
            },
            error: function (error) {
                console.log('error', error)
                this.setState({ spinLoading: false });
            }
        })
    }

    SaveConfig = () => {
        const userData = sessionStorage.userData ? JSON.parse(sessionStorage.userData) : {}
        let data = {
            params: []
        }
        //转换values  里的Radio类型的字段的值,true转换成Y,false转换成N

        var configList = []
        var configObj = this.refs
        for (var i in configObj) {
            var item = {}
            item.configCode = i
            item.configVal = configObj[i].state.selectedRowKeys.join('^')
            configList.push(item)

        }
        var dataObj = {}
        dataObj.hospID = userData.hospID
        dataObj.userID = userData.userID
        var configList = JSON.parse(JSON.stringify(configList));

        dataObj.configList = configList
        // 处理需要传递的obj
        data.params.push(Util.disposeData(dataObj));
        $http.post('urlADS', this, {
            code: '01040072',
            data: data,
            success: function (res) {
                if (+res.errorCode === 0) {
                    // 提示
                    message.success('保存成功');
                } else {
                    message.error('保存失败:' + res.errorMessage);
                }
            }
        })
    }

    render() {
        const { spinLoading } = this.state;
        return (
            <div style={{ width: '100%', height: '100%', padding: '12px', boxSizing: 'border-box' }}>
                <div style={{ height: 'calc(100% - 46px)', paddingBottom: '6px' }}>
                    <div style={{ width: '100%', height: '100%', overflow: 'auto' }}>
                        <Spin tip="数据加载中..." spinning={spinLoading}>
                            <Row gutter={24} style={{ width: '100%', minHeight: '460px' }}>
                                {this.state.configData.map((item, key) => {
                                    var selectRows = []
                                    var configVal = item.configVal
                                    if (configVal !== "") {
                                        selectRows = configVal.split("^")
                                    }
                                    return (
                                        <Col span={6} key={key} style={{ background: '#fff', height: '320px', padding: '20px' }}>
                                            <CBConfigTable configData={item.configData} selectedRowKeys={selectRows} configName={item.configName} ref={item.configCode} />
                                        </Col>
                                    )
                                })}
                            </Row>
                        </Spin>
                    </div>
                </div>
                <div style={{ paddingTop: '12px', textAlign: 'center', borderTop: '1px solid #e8e8e8' }}>
                    <Button type="primary" onClick={this.SaveConfig}>保存</Button>
                </div>
            </div>
        );
    }
}

export default CBModuleConfigMul;