/*
 * Creator:     ML
 * CreatDate:   2019-10-24
 * 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 TerminologyNaModal from './TerminologyNaModal'

const { Option } = Select;

class TerminNaDetail extends React.Component {
  constructor() {
    super();
    this.state = {
      info: {},
      mainTypeArr: [],
      ICD: ""
    };
  }

  componentDidMount() {
    this.props.onAttRef(this)
    this.getMainType()
  }
  onRef = (ref) => {
    this.onChildRef = ref
  }
  searchICD = (e) => {
    this.onChildRef.showModal(e.target.value)
  }
  changeICD=(e)=>{
    if(e&&e.target.value.length==0){
      this.setState({
        ICD:""
      })
    }
  }
  handleParms = (data) => {
    this.setState({
      ICD: data.id,
    })
    this.props.form.setFieldsValue({ ICDID: data.descripts })
  }
  handleClear = () => {
    if (this.state.ICD && this.state.ICD) {
      this.setState({
        // ICD,
      })
    } else {
      this.setState({ ICD: "" })
      this.props.form.setFieldsValue({ ICDID: undefined })
    }
  }
  getMainType = () => {
    let data = {}
    data = {
      params: [{
      }]
    }
    $http.post('urlS', this, {
      code: '05100259',
      data: data,
      success: function (res) {
        if (+res.errorCode === 0) {
          this.setState({
            mainTypeArr: res.result.Data
          })
        }
      }
    })
  }
  getInfo = (info) => {
    this.setState({
      info,
    })
  }
  clearInfo = () => {
    this.props.form.resetFields();
    this.setState({
      info: {}
    })
  }
  saveAtt = () => {
    const { info, ICD } = this.state
    this.props.form.validateFields((err, values) => {
      if (!err) {
        let data = {
          params: [{
            nationDiaID: info && info.nationDiaID ? info.nationDiaID : "",
            nationDiaCode: values && values.code || "",
            nationDiaDesc: values && values.dec || "",
            mainTypeID: values && values.mainTypeID || "",
            ICDID: ICD && ICD || "",
            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,
            updateUserID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).userID : ""
          }]
        }
        $http.post('urlS', this, {
          code: '05100257',
          data: data,
          success: function (res) {
            if (+res.errorCode === 0) {
              if (info && info.nationDiaID) {
                message.success("更新成功")
                this.props.handleQuery()
              } else {
                message.success("添加成功")
                this.props.handleQuery()
              }

            }
          }
        })
      }
    })
  }
  del = (info) => {
    let data = {
      params: [{
        nationDiaID: info && info.nationDiaID ? info.nationDiaID : "",
        updateUserID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).userID : ""
      }]
    }
    $http.post('urlS', this, {
      code: '05100258',
      data: data,
      success: function (res) {
        if (+res.errorCode === 0) {
          message.success("删除成功")
          this.props.handleQuery()
          this.clearInfo()
        }
      }
    })
  }

  render() {
    const { info, mainTypeArr } = this.state;
    const { getFieldDecorator } = this.props.form;
    const formLayout = {
      labelCol: { span: 10 },
      wrapperCol: { span: 14 },
    }
    let mainTypeOpt = []
    mainTypeArr && mainTypeArr.map(item => {
      mainTypeOpt.push(<Option key={item.id} value={String(item.id)}>{item.descripts}</Option>)
    })

    return (
      <div>
        <Form layout={'horizontal'}>
          <Row>
            <Col span={6}>
              <Form.Item label="代码" {...formLayout}>
                {getFieldDecorator('code', {
                  initialValue: info.nationDiaCode && info.nationDiaCode ? info.nationDiaCode : undefined,
                  rules: [{ required: true, message: '请输入代码' }]
                })(
                  <Input ></Input>
                )}
              </Form.Item>
            </Col>
            <Col span={6}>
              <Form.Item label='描述' {...formLayout}>
                {getFieldDecorator('dec', {
                  initialValue: info.nationDiaDesc && info.nationDiaDesc ? info.nationDiaDesc : undefined,
                  rules: [{ required: true, message: '请输入描述' }],
                })(
                  <Input ></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('mainTypeID', {
                  initialValue: info.mainTypeID && info.mainTypeID ? String(info.mainTypeID) : undefined,
                  rules: [{ required: false, message: '' }],
                })(
                  <Select>
                    {mainTypeOpt}
                  </Select>
                )}
              </Form.Item>
            </Col>
            <Col span={6}>
              <Form.Item label='ICD' {...formLayout}>
                {getFieldDecorator('ICDID', {
                  initialValue: info.ICDDesc && info.ICDDesc ? info.ICDDesc : undefined,
                  rules: [{ required: false, message: '' }],
                })(
                  <Input title={info.ICDDesc} placeholder="首拼/汉字回车进行检索" onPressEnter={(e) => this.searchICD(e)} onChange={(e)=>this.changeICD(e)}></Input>
                )}
              </Form.Item>
            </Col>
            <Col span={6}>
              <Form.Item label='Note' {...formLayout}>
                {getFieldDecorator('note', {
                  initialValue: info.note && info.note ? info.note : undefined,
                  rules: [{ required: false, message: '' }],
                })(
                  <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.nationDiaID} type="danger">删除</Button>
            </Popconfirm>
            <Button type="primary" onClick={this.clearInfo}>重置</Button>
          </Row>
        </Form>
        <TerminologyNaModal onRef={this.onRef} handleParms={this.handleParms} handleClear={this.handleClear}></TerminologyNaModal>
      </div>
    )
  }
};
const TerminologyNaDetail = Form.create()(TerminNaDetail);
export default TerminologyNaDetail;