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

const { Option } = Select;

class EquipmentDeatils extends React.Component {
  constructor() {
    super();
    var userData = JSON.parse(sessionStorage.getItem('userData'));
    this.state = {
      info: {},
      sexArr: [],
      mainTypeArr: [],
      icdArr: [],
      ICD:""
    };
  }

  componentDidMount() {
    this.props.onAttRef(this)
  }
 
  getInfo = (info) => {
    this.setState({
      info,
      ICD:info.ICDID
    })
  }
  clearInfo = () => {
    this.props.form.resetFields();
    this.setState({
      info: {}
    })
  }
  saveAtt = () => {
    const { info, } = this.state
    this.props.form.validateFields((err, values) => {
      if (!err) {
        let data = {
          params: [{
            equipmentID: info && info.equipmentID ? info.equipmentID : "",
            code: values && values.code || "",
            desc: values && values.dec || "",
            note: values && values.note || "",
            startDate: values.startDate ? moment(values.startDate).format('YYYY-MM-DD') : undefined,
            endDate: values.stopDate ? moment(values.stopDate).format('YYYY-MM-DD') : undefined,
            workStartTime: values.workStartTime ? moment(values.workStartTime).format('HH:mm') : undefined,
            workStopTime: values.workStopTime ? moment(values.workStopTime).format('HH:mm') : undefined,
            updateUserID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).userID : "",
            hospID:sessionStorage.userData ? JSON.parse(sessionStorage.userData).hospID : ""
          }]
        }
        $http.post('urlS', this, {
          code: '04150009',
          data: data,
          success: function (res) {
            if (+res.errorCode === 0) {
              if (info && info.equipmentID) {
                message.success("更新成功")
                this.props.handleQuery()
              } else {
                message.success("添加成功")
                this.props.handleQuery()
              }
            }
          }
        })

      }
    })
  }
  del = (info) => {
    let data = {
      params: [{
        equipmentID: info && info.equipmentID ? info.equipmentID : "",
        updateUserID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).userID : ""
      }]
    }
    $http.post('urlS', this, {
      code: '04150010',
      data: data,
      success: function (res) {
        if (+res.errorCode === 0) {
          message.success("删除成功")
          this.props.handleQuery()
          this.clearInfo()
        }
      }
    })
  }
  //swith开关状态处理
  onChangeSwith = (v, name) => {
    const { info } = this.state
    info[name] = v == true ? "Y" : "N"
    this.setState({
      info
    })
  }

  render() {
    const { info, icdArr } = this.state;

    const { getFieldDecorator } = this.props.form;
    const formLayout = {
      labelCol: { span: 10 },
      wrapperCol: { span: 14 },
    }



    return (
      <div>
        <Form layout={'horizontal'}>
          <Row>
            <Col span={6}>
              <Form.Item label="设备编码" {...formLayout}>
                {getFieldDecorator('code', {
                  initialValue: info.code && info.code ? info.code : undefined,
                  rules: [{ required: true, message: '请输入代码' }]
                })(
                  <Input ></Input>
                )}
              </Form.Item>
            </Col>
            <Col span={6}>
              <Form.Item label='设备名称' {...formLayout}>
                {getFieldDecorator('dec', {
                  initialValue: info.descripts && info.descripts ? info.descripts : undefined,
                  rules: [{ required: true, message: '请输入名称' }],
                })(
                  <Input title={info.desc}></Input>
                )}
              </Form.Item>
            </Col>
            <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 >
          </Row>
          <Row>
          <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('workStartTime', {
                    initialValue: info.workStartTime ? moment(info.workStartTime, 'HH:mm') : undefined,
                  })(
                    <TimePicker
                      separator=":"
                      showToday
                      format={'HH:mm'}
                      style={{ width: '100%' }}
                      placeholder="请选择时间"
                    />
                  )}
                </Form.Item>
            </Col>
            <Col span={6}>
            <Form.Item label="工作结束时间" {...formLayout}>
                  {getFieldDecorator('workStopTime', {
                    initialValue: info.workStopTime ? moment(info.workStopTime, 'HH:mm') : undefined,
                  })(
                    <TimePicker
                      separator=":"
                      showToday
                      format={'HH:mm'}
                      style={{ width: '100%' }}
                      placeholder="请选择时间"
                    />
                  )}
                </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.equipmentID} type="danger">删除</Button>
            </Popconfirm>
            <Button type="primary" onClick={this.clearInfo}>重置</Button>
          </Row>
        </Form>
      </div>
    )
  }
};
const TerminologyReDetail = Form.create()(EquipmentDeatils);
export default TerminologyReDetail;