/*
 * Creator:     ML
 * CreatDate:   2019-09-30
 * descripts：医嘱子类设置--右上基本信息
 * */
import React from 'react';
import moment from 'moment';
import { Form, Row, Col, Input, Select, DatePicker, Button, message, Switch } from 'antd';
import { $http } from '../../../containers/config/https';
import '../style/index.less';

const paddRightCol = {
    paddingRight: "0px"
}
const formItemLayoutType = {
    labelCol: { span: 9 },
    wrapperCol: { span: 15 }
}
const formItemLayoutCheck = {
    labelCol: { span: 15 },
    wrapperCol: { span: 25 },
}
const formItemLayoutCheckNoRepeat = {
    labelCol: { span: 16 },
    wrapperCol: { span: 25 },
}
const FormItem = Form.Item;
const Option = Select.Option;
const SubBaseInformation = Form.create()(
    class SubBaseInformation extends React.Component {
        cacheOriginData = {};
        constructor() {
            super()
            this.state = {
                topInfo: {},
                stockOrders: false,
                adviceTypeArr: []
            }
        }
        componentDidMount() {
            this.props.OnChildBaseInfo(this);
            this.getBigClass()
            this.getAdviceType()
        }
        //获取大类下拉框
        getBigClass = () => {
            let data = {}
            data = {
                params: [{
                }],
            }
            $http.post('urlS', this, {
                code: '05100228',
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0 && res.result.Data) {
                        this.setState({
                            bigTypeArr: res.result.Data
                        })
                    }
                }
            })
        }
        //获取医嘱类别下拉框
        getAdviceType = () => {
            let data = {}
            data = {
                params: [{
                }],
            }
            $http.post('urlS', this, {
                code: '05100229',
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0 && res.result.Data) {
                        this.setState({
                            adviceTypeArr: res.result.Data
                        })
                    }
                }
            })
        }
        //提交保存基本信息
        handleTopOk = () => {
            const { topInfo } = this.state
            this.props.form.validateFields((err, values) => {
                if (!err) {
                    let data = {}
                    //修改
                    if (topInfo && topInfo.orderSubCatID) {
                        data = {
                            params: [{
                                orderSubCatID: topInfo.orderSubCatID ||"",
                                orderSubCatCode: values.bigCode ||"",
                                orderSubCatDesc: values.bigDec ||"",
                                orderCatID: values.bigMajorType ||"",
                                orderTypeID: values.majorType ||"",
                                templetID: values.applicTem ||"",
                                noOrdSheet: this.state.noDisplayAdvice == true ? "Y" : "N",
                                noOrdExec: this.state.noDisplayExecution == true ? "Y" : "N",
                                ordExecType: values.ExecutionType ||"",
                                ordType: values.bigType ||"",
                                noStock: this.state.stockOrders == true ? "Y" : "N",
                                ownRecLoc: this.state.selfLoc == true ? "Y" : "N",
                                privateFlag: this.state.privateLogo == true ? "Y" : "N",
                                noChangeFlag:this.state.noBill == true ? "Y" : "N",
                                noAlertRepeat:this.state.noRepeatTip == true ? "Y" : "N",
                                customPrice:this.state.selfPricingMarkup == true ? "Y" : "N",
                                editFreq:this.state.editableFrequency == true ? "Y" : "N",
                                editUsage:this.state.editableUse == true ? "Y" : "N",
                                editDose:this.state.editableML == true ? "Y" : "N",
                                startDate: values.dateFrom ? moment(values.dateFrom).format('YYYY-MM-DD') : "",
                                stopDate: values.dateTo ? moment(values.dateTo).format('YYYY-MM-DD') : "",
                                updateUserID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).userID : "",
                            }],

                        }
                    } else {
                        data = {
                            params: [{
                                orderSubCatCode: values.bigCode,
                                orderSubCatDesc: values.bigDec,
                                orderCatID: values.bigMajorType,
                                orderTypeID: values.majorType,
                                templetID: values.applicTem,
                                noOrdSheet: this.state.noDisplayAdvice == true ? "Y" : "N",
                                noOrdExec: this.state.noDisplayExecution == true ? "Y" : "N",
                                ordExecType: values.ExecutionType,
                                ordType: values.bigType,
                                noStock: this.state.stockOrders == true ? "Y" : "N",
                                ownRecLoc: this.state.selfLoc == true ? "Y" : "N",
                                privateFlag: this.state.privateLogo == true ? "Y" : "N",
                                noChangeFlag:this.state.noBill == true ? "Y" : "N",
                                noAlertRepeat:this.state.noRepeatTip == true ? "Y" : "N",
                                customPrice:this.state.selfPricingMarkup == true ? "Y" : "N",
                                editFreq:this.state.editableFrequency == true ? "Y" : "N",
                                editUsage:this.state.editableUse == true ? "Y" : "N",
                                editDose:this.state.editableML == true ? "Y" : "N",
                                startDate: values.dateFrom ? moment(values.dateFrom).format('YYYY-MM-DD') : "",
                                stopDate: values.dateTo ? moment(values.dateTo).format('YYYY-MM-DD') : "",
                                updateUserID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).userID : "",
                            }],

                        }
                    }

                    $http.post('urlS', this, {
                        code: '05100196',
                        data: data,
                        success: function (res) {
                            if (+res.errorCode === 0) {
                                if (topInfo && topInfo.orderSubCatID) {
                                    message.success("修改成功")
                                    //刷新医嘱大类表格数据 若设为失效 左侧默显示生效状态更改为默认显示全部
                                    if ((values.dateFrom && values.dateFrom) && (values.dateTo && values.dateTo)) {
                                        if (moment(values.dateFrom).format('YYYYMMDD') < moment(values.dateTo).format('YYYYMMDD')) {
                                            this.props.updataTable("N")
                                            return
                                        }
                                        this.props.updataTable()
                                    }
                                } else {
                                    message.success("添加成功")
                                    this.setState({
                                        canSave: true
                                    })
                                    this.props.canSave(res.orderSubCatID)
                                    if ((values.dateFrom && values.dateFrom) && (values.dateTo && values.dateTo)) {
                                        if (moment(values.dateFrom).format('YYYYMMDD') < moment().format('YYYYMMDD')) {
                                            this.props.getLast("N")
                                            return
                                        }
                                    }
                                    this.props.getLast("Y", res.orderSubCatID)
                                    return
                                }
                                this.props.updataTable()
                            }
                        }
                    })
                }
            })
        }
        //取消保存基本信息--回到刚进入市状态信息
        handletTopCancel = () => {
            // this.props.form.resetFields()
            // this.setState({
            //     topInfo,
            // })
            this.setState({
                canSave: false
            })
            this.initOrig()
        }
        //切换时清空内容
        initData = (topInfo) => {
            this.props.form.resetFields()
            this.setState({
                topInfo: topInfo,
                canSave: false
            })
            // this.props.form.setFieldsValue({ dateFrom: topInfo.startDate, dataTo: topInfo.stopDate })

        }
        //取消时 返回到添加状态
        initOrig = () => {
            this.props.form.resetFields()
            this.setState({
                topInfo: {},

            })
            this.props.initOrigin();
            this.initDefalut();
        }
        init=()=>{
            this.props.form.resetFields()
            this.setState({
                topInfo: {},

            })
            this.initDefalut();
        }
        //switch事件
        changeChecked = (v, name) => {
            this.setState({
                [name]: v
            })
        }
        //设置是否被选中 接收父组件
        setDefalut = (record) => {
            this.setState({
                stockOrders: record.noStock == "Y" ? true : false,
                selfLoc: record.ownRecLoc == "Y" ? true : false,
                privateLogo: record.privateFlag == "Y" ? true : false,
                noRepeatTip: record.noAlertRepeat == "Y" ? true : false,
                noBill: record.noChangeFlag == "Y" ? true : false,
                selfPricingMarkup: record.customPrice == "Y" ? true : false,
                editableFrequency: record.editFreq == "Y" ? true : false,
                editableUse: record.editUsage == "Y" ? true : false,
                editableML: record.editDose == "Y" ? true : false,
                noDisplayAdvice: record.noOrdSheet == "Y" ? true : false,
                noDisplayExecution: record.noOrdExec == "Y" ? true : false,
            })
        }
        initDefalut = () => {
            this.setState({
                stockOrders: false,
                selfLoc: false,
                privateLogo: false,
                noRepeatTip: false,
                noBill: false,
                selfPricingMarkup: false,
                editableFrequency: false,
                editableUse: false,
                editableML: false,
                noDisplayAdvice: false,
                noDisplayExecution: false,
            })
        }
        render() {
            const { getFieldDecorator } = this.props.form;
            let { topInfo, bigTypeArr, adviceTypeArr } = this.state
            let typeArr = [{
                code: "01",
                descripts: "医生开嘱",
                descriptsSPCode: "YSKZ",
                enDescripts: "",
                id: "D"
            }, {
                code: "02",
                descripts: "护士开嘱",
                descriptsSPCode: "HSKZ",
                enDescripts: "",
                id: "N"
            },
            //  {
            //     code: "03",
            //     descripts: "补录开嘱(费用)",
            //     descriptsSPCode: "BLKZ",
            //     enDescripts: "",
            //     id: "A"
            // },
         ]
            //开医嘱类型
            let typeOp = []
            typeArr && typeArr.map(item => {
                typeOp.push(<Option key={item.id} value={String(item.id)}>{item.descripts}</Option>)
            })
            //执行类型
            let extionOp = []
            typeArr && typeArr.map(item => {
                extionOp.push(<Option key={item.id} value={String(item.id)}>{item.descripts}</Option>)
            })
            //医嘱大类
            let bigTypeOp = []
            bigTypeArr && bigTypeArr.map(item => {
                bigTypeOp.push(<Option key={item.id} role={item.descripts + '^' + item.descriptsSPCode} title={item.descripts} value={String(item.id)}>{item.descripts}</Option>)
            })
            //医嘱类别
            let adviceTypeOp = []
            adviceTypeArr && adviceTypeArr.map(item => {
                adviceTypeOp.push(<Option key={item.id} role={item.descripts + '^' + item.descriptsSPCode} title={item.descripts} value={String(item.id)}>{item.descripts}</Option>)
            })
            return (
                <div>
                    <Form>
                        <Row gutter={60} style={{margin:"0"}}>
                            <Col span={5} style={paddRightCol}>
                                <FormItem {...formItemLayoutType} label='代码'>
                                    {getFieldDecorator('bigCode', {
                                        initialValue: topInfo.orderSubCatcode && topInfo.orderSubCatcode ? topInfo.orderSubCatcode : undefined,
                                        rules: [{ required: true, message: '请输入代码' }],
                                    })(
                                        <Input />
                                    )}
                                </FormItem>
                            </Col>
                            <Col span={5} style={paddRightCol}>
                                <FormItem {...formItemLayoutType} label='描述'>
                                    {getFieldDecorator('bigDec', {
                                        initialValue: topInfo.orderSubCatDesc && topInfo.orderSubCatDesc ? topInfo.orderSubCatDesc : undefined,
                                        rules: [{ required: true, message: '请输入描述' }],
                                    })(
                                        <Input />
                                    )}
                                </FormItem>
                            </Col>
                            <Col span={5} style={paddRightCol}>
                                <FormItem {...formItemLayoutType} label='开医嘱类型'>
                                    {getFieldDecorator('bigType', {
                                        initialValue: topInfo.ordType && topInfo.ordType ? topInfo.ordType : undefined,
                                        rules: [{ required: false, message: '' }],
                                    })(
                                        <Select
                                        allowClear
                                        >
                                            {typeOp}
                                        </Select>
                                    )}
                                </FormItem>
                            </Col>
                            <Col span={5} style={paddRightCol}>
                                <FormItem {...formItemLayoutType} label='医嘱大类'>
                                    {getFieldDecorator('bigMajorType', {
                                        initialValue: topInfo.orderCatID && topInfo.orderCatID ? topInfo.orderCatID : undefined,
                                        rules: [{ required: true, message: '请选择医嘱大类' }],
                                    })(
                                        <Select
                                            allowClear={true}
                                            showSearch={true}
                                            optionFilterProp="role"
                                        >
                                            {bigTypeOp}
                                        </Select>
                                    )}
                                </FormItem>
                            </Col>
                        </Row>
                        <Row gutter={60} style={{margin:"0"}}>
                            <Col span={5} style={paddRightCol}>
                                <FormItem {...formItemLayoutType} label='医嘱类别'>
                                    {getFieldDecorator('majorType', {
                                        initialValue: topInfo.orderTypeID && topInfo.orderTypeID!=="F" ? topInfo.orderTypeID : undefined,
                                        rules: [{ required: true, message: '请选择医嘱类别' }],
                                    })(
                                        <Select
                                            allowClear={true}
                                            showSearch={true}
                                            optionFilterProp="role"
                                        >
                                            {adviceTypeOp}
                                        </Select>
                                    )}
                                </FormItem>
                            </Col>
                            <Col span={5} style={paddRightCol}>
                                <FormItem {...formItemLayoutType} label='申请单模板'>
                                    {getFieldDecorator('applicTem', {
                                        initialValue: topInfo.templetID && topInfo.templetID ? topInfo.templetID : undefined,
                                        rules: [{ required: false, message: '' }],
                                    })(
                                        <Input></Input>
                                    )}
                                </FormItem>
                            </Col>
                            <Col span={5} style={paddRightCol}>
                                <FormItem {...formItemLayoutType} label='执行类型'>
                                    {getFieldDecorator('ExecutionType', {
                                        initialValue: topInfo.ordExecType && topInfo.ordExecType ? topInfo.ordExecType : undefined,
                                        rules: [{ required: false, message: '' }],
                                    })(
                                        <Select
                                        allowClear
                                        >
                                            {extionOp}
                                        </Select>
                                    )}
                                </FormItem>
                            </Col>
                            <Col span={5} style={paddRightCol}>
                                <FormItem label='生效日期' {...formItemLayoutType}>
                                    {getFieldDecorator('dateFrom', {
                                        initialValue: topInfo.startDate ? moment(topInfo.startDate, "YYYY-MM-DD") : moment(),
                                        rules: [{ required: true, message: '请选择生效日期' }],
                                    })(
                                        <DatePicker style={{ width: '100%' }} placeholder={'生效日期'} disabledDate={(curDate) => {
                                            return curDate && curDate > moment().endOf('day');
                                        }} />
                                    )}
                                </FormItem>
                            </Col>
                            <Col span={5} style={paddRightCol}>
                                <FormItem label='失效日期' {...formItemLayoutType}>
                                    {getFieldDecorator('dateTo', {
                                        initialValue: topInfo.stopDate ? moment(topInfo.stopDate, "YYYY-MM-DD") : undefined,
                                        rules: [{ required: false, message: '' }],
                                    })(
                                        <DatePicker style={{ width: '100%' }} placeholder={'失效日期'} disabledDate={(curDate) => {
                                            return curDate < moment(this.props.form.getFieldValue('dateTo')).format('YYYY-MM-DD');
                                        }} />
                                    )}
                                </FormItem>
                            </Col >
                        </Row>
                        <Row gutter={60} style={{margin:"0"}}>
                            <Col span={5} style={{ paddingLeft: "31px", boxSizing: "border-box" }}>
                                <FormItem label="无库存医嘱" {...formItemLayoutCheck} >
                                    {getFieldDecorator('stockOrders', {
                                        initialValue: undefined,
                                        rules: [{ required: false, message: ' ' }],
                                    })(
                                        <Switch checkedChildren="是" unCheckedChildren="否" checked={this.state.stockOrders} onChange={(v) => this.changeChecked(v, "stockOrders")}></Switch>
                                    )}
                                </FormItem>
                            </Col>
                            <Col span={5} style={{ paddingLeft: "31px", boxSizing: "border-box" }}>
                                <FormItem label="本科到本科"  {...formItemLayoutCheck} >
                                    {getFieldDecorator('selfLoc', {
                                        initialValue: undefined,
                                        rules: [{ required: false, message: ' ' }],
                                    })(
                                        <Switch checkedChildren="是" unCheckedChildren="否" checked={this.state.selfLoc} onChange={(v) => this.changeChecked(v, "selfLoc")}></Switch>
                                    )}
                                </FormItem>
                            </Col>
                            <Col span={5} style={{ paddingLeft: "31px", boxSizing: "border-box" }}>
                                <FormItem label="私有标志" {...formItemLayoutCheck} >
                                    {getFieldDecorator('privateLogo', {
                                        initialValue: undefined,
                                        rules: [{ required: false, message: ' ' }],
                                    })(
                                        <Switch checkedChildren="是" unCheckedChildren="否" checked={this.state.privateLogo} onChange={(v) => this.changeChecked(v, "privateLogo")}></Switch>
                                    )}
                                </FormItem>
                            </Col>
                            <Col span={6} style={{ paddingLeft: "31px", boxSizing: "border-box" }}>
                                <FormItem label="不提示重复医嘱" {...formItemLayoutCheckNoRepeat} >
                                    {getFieldDecorator('noRepeatTip', {
                                        initialValue: undefined,
                                        rules: [{ required: false, message: ' ' }],
                                    })(
                                        <Switch checkedChildren="是" unCheckedChildren="否" checked={this.state.noRepeatTip} onChange={(v) => this.changeChecked(v, "noRepeatTip")}></Switch>
                                    )}
                                </FormItem>
                            </Col>
                            <Col span={2} style={{ padding:"0", }}>
                                <Button key="submitc" type="primary" onClick={this.handletTopCancel}>取消</Button>
                            </Col>
                            <Col span={5} style={{ paddingLeft: "31px", boxSizing: "border-box" }}>
                                <FormItem label="不计费" {...formItemLayoutCheck} >
                                    {getFieldDecorator('noBill', {
                                        initialValue: undefined,
                                        rules: [{ required: false, message: ' ' }],
                                    })(
                                        <Switch checkedChildren="是" unCheckedChildren="否" checked={this.state.noBill} onChange={(v) => this.changeChecked(v, "noBill")}></Switch>
                                    )}
                                </FormItem>
                            </Col>
                            <Col span={5} style={{ paddingLeft: "31px", boxSizing: "border-box" }}>
                                <FormItem label="自定价标记" {...formItemLayoutCheck} >
                                    {getFieldDecorator('selfPricingMarkup', {
                                        initialValue: undefined,
                                        rules: [{ required: false, message: ' ' }],
                                    })(
                                        <Switch checkedChildren="是" unCheckedChildren="否" checked={this.state.selfPricingMarkup} onChange={(v) => this.changeChecked(v, "selfPricingMarkup")}></Switch>
                                    )}
                                </FormItem>
                            </Col>
                            {/* <Col span={4} style={{ paddingLeft: "31px", boxSizing: "border-box" }}>
                                <FormItem label="可编辑频次" {...formItemLayoutCheck} >
                                    {getFieldDecorator('editableFrequency', {
                                        initialValue: undefined,
                                        rules: [{ required: false, message: ' ' }],
                                    })(
                                        <Switch checkedChildren="是" unCheckedChildren="否" checked={this.state.editableFrequency} onChange={(v) => this.changeChecked(v, "editableFrequency")}></Switch>
                                    )}
                                </FormItem>
                            </Col> */}
                            {/* <Col span={4} style={{ paddingLeft: "31px", boxSizing: "border-box" }}>
                                <FormItem label="可编辑用法" {...formItemLayoutCheck} >
                                    {getFieldDecorator('editableUse', {
                                        initialValue: undefined,
                                        rules: [{ required: false, message: ' ' }],
                                    })(
                                        <Switch checkedChildren="是" unCheckedChildren="否" checked={this.state.editableUse} onChange={(v) => this.changeChecked(v, "editableUse")}></Switch>
                                    )}
                                </FormItem>
                            </Col> */}
                            {/* <Col span={4} style={{ paddingLeft: "31px", boxSizing: "border-box" }}>
                                <FormItem label="可编辑剂量" {...formItemLayoutCheck} >
                                    {getFieldDecorator('editableML', {
                                        initialValue: undefined,
                                        rules: [{ required: false, message: ' ' }],
                                    })(
                                        <Switch checkedChildren="是" unCheckedChildren="否" checked={this.state.editableML} onChange={(v) => this.changeChecked(v, "editableML")}></Switch>
                                    )}
                                </FormItem>
                            </Col> */}
                            <Col span={5} style={{ paddingLeft: "31px", boxSizing: "border-box" }}>
                                <FormItem label="医嘱单不显示" {...formItemLayoutCheck} >
                                    {getFieldDecorator('noDisplayAdvice', {
                                        initialValue: undefined,
                                        rules: [{ required: false, message: ' ' }],
                                    })(
                                        <Switch checkedChildren="是" unCheckedChildren="否" checked={this.state.noDisplayAdvice} onChange={(v) => this.changeChecked(v, "noDisplayAdvice")}></Switch>
                                    )}
                                </FormItem>
                            </Col>
                            <Col span={6} style={{ paddingLeft: "31px", boxSizing: "border-box" }}>
                                <FormItem label="执行单不显示" {...formItemLayoutCheck} >
                                    {getFieldDecorator('noDisplayExecution', {
                                        initialValue: undefined,
                                        rules: [{ required: false, message: ' ' }],
                                    })(
                                        <Switch checkedChildren="是" unCheckedChildren="否" checked={this.state.noDisplayExecution} onChange={(v) => this.changeChecked(v, "noDisplayExecution")}></Switch>
                                    )}
                                </FormItem>
                            </Col>
                            <Col span={2} style={{ padding:"0", }}>
                            <Button key="submits" type="primary" disabled={this.state.canSave} onClick={this.handleTopOk}>保存</Button>
                            </Col>
                        </Row>
                        <Row gutter={60} style={{margin:"0"}}> 
                            <Col style={{ color: "#faad14" }}>
                                <span>{topInfo && topInfo.orderCatID ? "点击”取消“，返回到添加医嘱子类" : "请保存基本信息后，添加相关信息"}</span>
                            </Col>
                        </Row>
                    </Form>
                </div>
            )
        }
    })
export default SubBaseInformation