// 医生站配置*
import React, { Component } from 'react';
import { Switch, Button, Col, Row, Input, Select, message, Form, Tooltip } from 'antd';
import { $http } from 'containers/config/https';
import { Util } from 'tools';

const Option = Select.Option;

const CBModuleConfig = Form.create()(
    class extends Component {
        constructor(props) {
            super(props);
            this.state = {
                configData: [],
                moduleCode: ""
            }
        }

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

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

        GetCom = (item) => {
            const { getFieldDecorator } = this.props.form;
            var configCode = item.configCode;
            var configVal = item.configVal;
            var type = item.type;
            if (type === "Text") {
                return (
                    {
                        ...getFieldDecorator(configCode, {
                            initialValue: configVal
                        })(<Input onKeyDown={this.FieldOnKeyDown} style={{ width: '100%' }} />)
                    }
                )
            } else if (type === "CheckBox") {
                return (
                    {
                        ...getFieldDecorator(configCode, {
                            initialValue: configVal === "Y" ? true : false,
                            valuePropName: 'checked',
                            normalize: (value, prevValue) => {
                                return value
                            }
                        })(<Switch checkedChildren="是" unCheckedChildren="否" />)
                    }
                )
            } else if (type === "Select") {
                return (
                    {
                        ...getFieldDecorator(configCode, {
                            initialValue: configVal,
                            normalize: (value, prevValue) => {
                                if (value === 0) {
                                    value = ""
                                }
                                return value
                            }
                        })(<Select
                            allowClear={true}
                            placeholder=""
                            showSearch
                            style={{ width: '100%' }}
                            optionFilterProp="title"
                        >
                            {
                                item.configData.map((item, key) => {
                                    return (
                                        <Option key={item.ID} alias={item.Alias} title={item.Alias + "^" + item.Desc}>{item.Desc}</Option>
                                    )
                                })
                            }
                        </Select>
                        )
                    }
                )
            }
        }

        FieldOnKeyDown = () => {

        }

        SaveConfig = () => {
            const userData = sessionStorage.userData ? JSON.parse(sessionStorage.userData) : {}
            const { form: { validateFields } } = this.props;
            validateFields((errors, values) => {
                if (errors === null) {
                    let data = {
                        params: []
                    }
                    //转换values  里的Radio类型的字段的值,true转换成Y,false转换成N
                    let configData = this.state.configData
                    var configList = []
                    for (var i = 0; i < configData.length; i++) {
                        var itemConfig = configData[i]
                        var val = values[itemConfig.configCode]
                        if (itemConfig.type === "CheckBox") {
                            if (val === true) {
                                val = "Y"
                            } else {
                                val = "N"
                            }
                        } else if (itemConfig.type === "Date") {
                            if (val) {
                                val = val.format('YYYY-MM-DD')
                                //console.log(values[itemField.Code].format('YYYY-MM-DD')) 
                            }
                        }
                        var item = {}
                        item.configCode = itemConfig.configCode
                        item.configVal = val
                        configList.push(item)
                    }
                    var dataObj = {}
                    dataObj.hospID = userData.hospID
                    dataObj.userID = userData.userID
                    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('保存成功');
                                // 新增或编辑成功重置form表单

                                //this.props.handelThis.getAllData()
                                // this.resetFields();

                            } else {
                                message.error("保存失败:" + res.errorMessage);
                            }
                        }
                    })
                } else {
                    return;
                }
            })
        }

        render() {
            if (this.state.configData.length <= 0) {
                return (<div></div>)
            }
            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' }}>
                            <Form onSubmit={this.handleSubmit} layout="vertical">
                                <Row gutter={24} style={{ margin: 0 }}>
                                    {this.state.configData.map((item, key) => {
                                        return (
                                            <Col span={4} key={key}>
                                                <Form.Item
                                                    key={key}
                                                    label={(
                                                        <Tooltip title={item.configName}>
                                                            <span className='ellipsis' style={{ width: '100%', display: 'inline-block' }}>{item.configName}</span>
                                                        </Tooltip>
                                                    )}
                                                    style={{ height: '60px' }}
                                                >
                                                    {this.GetCom(item)}
                                                </Form.Item>
                                            </Col>
                                        )
                                    })}
                                </Row>
                            </Form>
                        </div>
                    </div>
                    <div style={{ paddingTop: '12px', textAlign: 'center', borderTop: '1px solid #e8e8e8' }}>
                        <Button type="primary" onClick={this.SaveConfig}>保存</Button>
                    </div>
                </div>
            );
        }
    }
);

export default CBModuleConfig;