/*
 * Creator:     ML
 * CreatDate:   2019-10-28
 * descripts：  科室设备维护表 明细
 * */
import React from 'react';
import { Form, Input, Select, Button, message, Row, Icon, Divider, Popconfirm, DatePicker, Table, Col } from 'antd';
import { $http } from 'containers/config/https';
import moment from 'moment';
import store from 'store';
import DeparEquAssModal from './DeparEquAssModal'


const { Option } = Select;

class DepartEquMainDeatils extends React.Component {
  constructor() {
    super();
    var userData = JSON.parse(sessionStorage.getItem('userData'));
    this.state = {
      info:{},
      medID:"",
    };
  }

  componentDidMount() {
    this.props.onAttRef(this)
    this.getFindEquDate()
  }
  onRef=(ref)=>{
    this.onChildRef=ref
  }
  searchmedID=(e)=>{
    this.onChildRef.showModal(e.target.value)
  }
  changemedID=(e)=>{
    if(e&&e.target.value.length==0){
      this.setState({
        medID:""
      })
    }
  }
  handleParms=(data)=>{
    this.setState({
      medID:data.id,
    })
    this.props.form.setFieldsValue({medItemID:data.descripts})
}
handleClear=()=>{
  if(this.state.medID&&this.state.medID){
    this.setState({
      // medID,
    })
  }else{
    this.setState({medID:""})
    this.props.form.setFieldsValue({medItemID:undefined})
  }
}
  getInfo=(info)=>{
    this.setState({
      info,
      medID:info.medItemID
    })
  }
  clearInfo=()=>{
    this.props.form.resetFields();
    this.setState({
      info:{},
      medID:""
    })
  }
  saveAtt=()=>{
    const {info,medID}=this.state
    this.props.form.validateFields((err, values) => {
      if(!err){
        if(medID==""){
          this.props.from.setFieldsValue({medItemID:undefined})
          message.warning("请输入检索医嘱项目")
          return
        }
        let data = {
          params:[{
            locEquipLinkItemID:info&&info.locEquipLinkItemID?info.locEquipLinkItemID:"",
            medItemID: medID&&medID || "",
            locEquipmentID: values && values.equipmentID || "",
            intervalTime: values && values.intervalTime || "",
            note: values && values.note || "",
            attention: values && values.attention || "",
            startDate:values.startDate ? moment(values.startDate).format('YYYY-MM-DD') : undefined,
            endDate:values.stopDate ? moment(values.stopDate).format('YYYY-MM-DD') : undefined,
            updateUserID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).userID : ""
          }]
        } 
         $http.post('urlS', this, {
            code: '04150016',
            data: data,
            success: function (res) {
              if (+res.errorCode === 0) {
                    if(info&&info.locEquipLinkItemID){
                      message.success("更新成功")
                      this.props.handleQuery()
                    }else{
                      message.success("添加成功")
                      this.props.handleQuery()
                    }
              }
            }
          })
      }
    })
  }
del=(info)=>{
  let data = {
    params:[{
      locEquipLinkItemID:info&&info.locEquipLinkItemID ?info.locEquipLinkItemID:"",
      updateUserID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).userID : ""
    }]
  }
  $http.post('urlS', this, {
    code: '04150015',
    data: data,
    success: function (res) {
      if (+res.errorCode === 0) {
            message.success("删除成功")
            this.props.handleQuery()
            this.clearInfo()
      }
    }
  })
}
 //查询科室设备
 getFindEquDate = (values,e) => {
  let { page, pageSize, } = this.state;
  let data = {
    params: [{
      locID: "",
      status:"Y",
    }],
    pagination: [{
      pageSize: 999,
      currentPage: 1,
    }]
  }
  $http.post('urlS', this, {
    code: '04150011',
    data: data,
    success: function (res) {
      if (+res.errorCode === 0) {
        this.setState({
          EquData: res.result.Data,
        })
      }
    }
  })
}

  render() {
    const { info,EquData } = this.state;
    const { getFieldDecorator } = this.props.form;
    const formLayout = {
      labelCol: { span: 10 },
      wrapperCol: { span: 14 },
    }
    const formLayoutWarning = {
      labelCol: { span:5 },
      wrapperCol: { span: 19 },
    }
     //科室设备
     let equOpt = []
     EquData && EquData.map(item => {
       equOpt.push(<Option key={item.locEquipmentID} value={item.locEquipmentID}>{item.equipmentDesc}</Option>)
     })

    return (
      <div>
        <Form layout={'horizontal'}>
          <Row>
            <Col span={6}>
              <Form.Item label="科室设备" {...formLayout}>
                {getFieldDecorator('equipmentID', {
                  initialValue: info.locEquipmentID && info.locEquipmentID ? info.locEquipmentID : undefined,
                  rules: [{ required: true, message: '请选择设备' }]
                })(
                  <Select disabled={info&&info.locEquipLinkItemID}>
                      {equOpt}
                  </Select>
                )}
              </Form.Item>
            </Col>
            <Col span={6}>
            <Form.Item label="耗时(分钟)" {...formLayout}>
                  {getFieldDecorator('intervalTime', {
                    initialValue: info.intervalTime || info.intervalTime==0 ?info.intervalTime: undefined,
                    getValueFromEvent: (event) => {
                      return event.target.value.replace(/\D/g, '')
                  },
                  })(
                   <Input></Input>
                  )}
                </Form.Item>
            </Col>
            <Col span={12}>
              <Form.Item label='医嘱项目' {...formLayoutWarning}>
                {getFieldDecorator('medItemID', {
                  initialValue: info.medItemDesc && info.medItemDesc ? info.medItemDesc : undefined,
                  rules: [{ required: true, message: '请选择医嘱项目' }],
                })(
                  <Input title={info.medItemDesc} placeholder="首拼/汉字回车进行检索" onPressEnter={(e)=>this.searchmedID(e)} onChange={(e)=>this.changemedID(e)}></Input>
                )}
              </Form.Item>
            </Col>
          </Row>
          <Row>
          <Col span={6} className="starDataDom">
              <Form.Item label='生效日期' {...formLayout}>
                {getFieldDecorator('startDate', {
                  initialValue: info && info.startDate ? moment(info.startDate, "YYYY-MM-DD") : moment(),
                  rules: [{ required: true, message: '请选择开始日期' }],
                })(
                  <DatePicker style={{ width: '100%' }} placeholder={'开始日期'} disabledDate={(curDate) => {
                    return curDate && curDate > moment().endOf('day');
                  }} />
                )}
              </Form.Item>
            </Col>
            <Col span={6} >
              <Form.Item label='失效日期' {...formLayout}>
                {getFieldDecorator('stopDate', {
                  initialValue: info && info.stopDate ? moment(info.stopDate, "YYYY-MM-DD") : undefined,
                  rules: [{ required: false, message: '' }],
                })(
                  <DatePicker style={{ width: '100%' }} placeholder={'请选择失效日期'}  />
                )}
              </Form.Item>
            </Col >
            <Col span={6}>
              <Form.Item label='备注' {...formLayout}>
                {getFieldDecorator('note', {
                  initialValue: info.note && info.note ? info.note : undefined,
                  rules: [{ required: false, message: '' }],
                })(
                  <Input title={info.note}></Input>
                )}
              </Form.Item>
            </Col>
            <Col span={6}>
            <Form.Item label="注意事项" {...formLayout}>
                  {getFieldDecorator('attention', {
                    initialValue: info&&info.attention?info.attention: undefined,
                  })(
                   <Input></Input>
                  )}
                </Form.Item>
            </Col>
          </Row>
          <Row style={{display:"flex",justifyContent:"space-between",justifyItems:"center",alignItems:"center",height:"48px"}}>
            <Button type="primary" onClick={this.saveAtt}>保存</Button>
            <Popconfirm title="是否要删除此事项" onConfirm={() => this.del(info)}>
              <Button disabled={info&&!info.locEquipLinkItemID} type="danger">删除</Button>
            </Popconfirm>
            <Button type="primary" onClick={this.clearInfo}>重置</Button>
          </Row>
        </Form>
        <DeparEquAssModal onRef={this.onRef} handleParms={this.handleParms} handleClear={this.handleClear}></DeparEquAssModal>
      </div>
    )
  }
};
const DepartEquMainDeatil = Form.create()(DepartEquMainDeatils);
export default DepartEquMainDeatil;