/*
 * Creator:     ML
 * CreatDate:   2019-12-25
 * descripts：  服务组列表添加/编辑
 * */
import React from 'react';
import { Form, Row, Col, Input, Select, Button, DatePicker, Modal, Table, Icon, Pagination, message, Divider, Tooltip, Checkbox } from 'antd';
import moment from 'moment';
import { $http } from 'containers/config/https';
import store from 'store';

const FormItem = Form.Item;
const paddRightCol = {
    paddingRight: "0px"
}
const formItemLayout = { // label 和input宽度
    labelCol: { span: 9 },
    wrapperCol: { span: 15 },
}
const formLayoutItenm = {
    labelCol: { span: 4 },
    wrapperCol: { span: 20 },
}
const ServiceGroupListModal = Form.create()(
    class ServiceGroupListModal extends React.Component {
        constructor(props) {
            super(props)
            this.state = {
                visible: false,
                baseParms: {},
                edit: -1,
                params: {},
                id: "",
            }
        }
        componentDidMount() {
            this.props.OnChildrefs(this);
            this.getHopSelect()
        }
        showModal = (record, num) => {
            this.setState({
                visible: true,
                publicBaseLocArr: [],
                LocDr: ""
            })

            if (record && num && num == "1") {
                // console.log("record",record)
                this.handleBaseHosp(record.hospId)
                this.setState({
                    edit: num,
                    params: record,
                    id: record.id,
                    medItemID: record.medItemId
                }, () => {
                    this.props.form.setFieldsValue({ LocDr: record.locId })
                })
            }
        }
        //获取医院下拉框
        getHopSelect = () => {
            let data = {
                params: [{
                    hospID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).hospID : "",
                    logonGroupID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).groupID : "",
                    logonHospID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).hospID : "",
                }],
            }
            $http.post('urlS', this, {
                code: '01040001',
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        this.setState({
                            hospArr: res.result
                        })
                    }
                }
            })
        }
        //医院选择带出科室
        handleBaseHosp = (id) => {
            this.props.form.setFieldsValue({ LocDr: undefined })
            if (id) {
                this.setState({
                    hosID: id
                }, () => {
                    this.getPublicBaseLocSelect()
                })
            } else {
                this.setState({
                    hosID: "",
                    publicBaseLocArr: []
                }, () => {
                    this.getPublicBaseLocSelect()
                })
            }
        }
        //获取科室下拉框
        getPublicBaseLocSelect = () => {
            let data = {}
            data = {
                params: [{
                    hospID: this.state.hosID || "",
                    locTypeID: this.state.LocDr || ""
                }],
            }
            $http.post('urlS', this, {
                code: '01050029',
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0 && res.result.Data) {
                        let arr = res.result.Data && res.result.Data.map((item, index) => {
                            res.result.Data[index].key = Number(index)
                        })
                        this.setState({
                            publicBaseLocArr: res.result.Data,
                        })
                    } else {

                    }
                }
            })
        }
        handleCancel = () => {
            this.props.form.resetFields();
            this.setState({ visible: false, params: {}, edit: -1, id: "" });
        }
        //添加/编辑卡
        handleCreat = () => {
            this.props.form.validateFields((err, values) => {
                if (!err) {
                    let data = {}
                    data = {
                        params: [{
                            updateUserID: JSON.parse(sessionStorage.userData).userID || '',
                            createUserID: JSON.parse(sessionStorage.userData).userID || '',
                            ID: this.state.id && this.state.id ? this.state.id : "",//修改
                            hospId: values && values.getHop || '',
                            locId: values.LocDr || "",
                            code: values.code || "",
                            descripts: values.descripts || "",
                            eNDesc: values.eNDesc || "",
                            createDate: moment().format('YYYY-MM-DD'),//创建日期
                            startDate: values.dateFrom ? moment(values.dateFrom).format('YYYY-MM-DD') : undefined,//生效日期
                            stopDate: values.dateTo ? moment(values.dateTo).format('YYYY-MM-DD') : undefined,//失效日期
                        }]
                    }
                    $http.post('urlS', this, {
                        code: '01040155',
                        data: data,
                        success: function (res) {
                            if (+res.errorCode === 0) {
                                if (this.state.edit == "1") {
                                    message.success('修改成功');
                                    this.setState({ visible: false, })
                                    this.props.getNewData();
                                } else {
                                    message.success('添加成功');
                                    this.props.getNewData(res.result.ID);
                                }
                                this.setState({
                                    visible: false,
                                    edit: -1,
                                    params: {},
                                    id: ""
                                })
                                this.props.form.resetFields();
                            }
                        }
                    })
                }
            })
        }
        render() {
            const { getFieldDecorator, } = this.props.form;
            const { params } = this.state
            //医院
            let hosOp = []
            this.state.hospArr && this.state.hospArr.map((item) => {
                hosOp.push(<Option  role={item.descripts + '^' + item.descriptsSPCode} key={item.id} onClick={(e, ) => this.handleBaseHosp(item.id)} value={String(item.id)}>{item.descripts}</Option>)
            })
            //科室类型
            let locOp = []
            this.state.publicBaseLocArr && this.state.publicBaseLocArr.map(item => {
                locOp.push(<Option key={item.id} role={item.descripts + '^' + item.descriptsSPCode} title={item.descripts} value={String(item.id)}>{item.descripts}</Option>)
            })
            return (
                <div className="bedDom">
                    <Modal
                        className='serviceDom'
                        width='1000px'
                        visible={this.state.visible}
                        title={this.state.edit == "1" ? "编辑服务组" : "添加服务组"}
                        onOk={this.handleOk}
                        onCancel={this.handleCancel}
                        footer={[
                            <Button key="submit" type="primary" loading={this.setState.loading} disabled={this.state.rowId == ""} onClick={this.handleCreat}>确定</Button>,
                            <Button key="cancel" type="primary" loading={this.setState.loading} onClick={this.handleCancel}>取消</Button>,
                        ]}>
                        <div className="department-header">
                            <Form>
                                <Row gutter={60} style={{ margin: '0' }}>
                                    <Col span={6} style={paddRightCol}>
                                        <FormItem {...formItemLayout} label='医院'>
                                            {getFieldDecorator('getHop', {
                                                initialValue: params && params.hospId ? params.hospId : undefined,
                                                rules: [{ required: true, message: '请选择医院' }],
                                            })(
                                                <Select
                                                    onChange={this.handleBaseHosp}
                                                    showSearch
                                                    optionFilterProp="role"
                                                >
                                                    {hosOp}
                                                </Select>
                                            )}
                                        </FormItem>
                                    </Col>
                                    <Col span={6} style={paddRightCol}>
                                        <FormItem {...formItemLayout} label='科室'>
                                            {getFieldDecorator('LocDr', {
                                                initialValue: params && params.locId ? params.locId : undefined,
                                                rules: [{ required: true, message: '请选择科室' }],
                                            })(
                                                <Select
                                                    placeholder={"请先选择医院"}
                                                    showSearch
                                                    optionFilterProp="role"
                                                >
                                                    {locOp}
                                                </Select>
                                            )}
                                        </FormItem>
                                    </Col>
                                    <Col span={6} style={paddRightCol}>
                                        <FormItem label='代码' {...formItemLayout}>
                                            {getFieldDecorator('code', {
                                                initialValue: params && params.code ? params.code : undefined,
                                                rules: [{ required: true, message: '请输入代码' }],
                                            })(
                                                <Input allowClear />
                                            )}
                                        </FormItem>
                                    </Col>
                                    <Col span={6} style={paddRightCol}>
                                        <FormItem label='描述' {...formItemLayout}>
                                            {getFieldDecorator('descripts', {
                                                initialValue: params && params.descripts ? params.descripts : undefined,
                                                rules: [{ required: true, message: '请输入描述' }],
                                            })(
                                                <Input allowClear />
                                            )}
                                        </FormItem>
                                    </Col>
                                </Row>
                                <Row gutter={60} style={{ margin: '0' }}>
                                    <Col span={6} style={paddRightCol}>
                                        <FormItem label='英文描述' {...formItemLayout}>
                                            {getFieldDecorator('eNDesc', {
                                                initialValue: params && params.eNDesc ? params.eNDesc : undefined,
                                                rules: [{ required: false, message: '' }],
                                            })(
                                                <Input allowClear />
                                            )}
                                        </FormItem>
                                    </Col>
                                    <Col span={6} style={paddRightCol}>
                                        <FormItem label='生效日期' {...formItemLayout}>
                                            {getFieldDecorator('dateFrom', {
                                                initialValue: params.startDate ? moment(params.startDate, "YYYY-MM-DD") : moment(),
                                                rules: [{ required: true, message: '请选择生效日期' }],
                                            })(
                                                <DatePicker style={{ width: '100%' }} disabledDate={(curDate) => {
                                                    return curDate && curDate > moment().endOf('day');
                                                }} />
                                            )}
                                        </FormItem>
                                    </Col>
                                    <Col span={6} style={paddRightCol}>
                                        <FormItem label='失效日期' {...formItemLayout}>
                                            {getFieldDecorator('dateTo', {
                                                initialValue: params.StopDate ? moment(params.StopDated, "YYYY-MM-DD") : undefined,
                                                rules: [{ required: false, message: '' }],
                                            })(
                                                <DatePicker style={{ width: '100%' }} />
                                            )}
                                        </FormItem>
                                    </Col>
                                </Row>
                            </Form>
                        </div>
                    </Modal>
                </div>
            )
        }
    })
export default ServiceGroupListModal    