/*
 * 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 TerminologyRAndNnaModals from './TerminologyRAndNnaModals';
import TerminologyRAndNreModals from './TerminologyRAndNreModals'


const { Option } = Select;

class TerminNaAndReDetail extends React.Component {
  constructor() {
    super();
    this.state = {
      info:{},
      ICD:"",
      diaID:""
    };
  }

  componentDidMount() {
    this.props.onAttRef(this)
  }
  onRef = (ref) => {
    this.onChildRef = ref
  }
  ondiaRef = (ref) => {
    this.onChilddiaRef = ref
  }
  searchICD = (e) => {
    this.onChildRef.showModal(e.target.value)
  }
  searchdiaICD= (e) => {
    this.onChilddiaRef.showModal(e.target.value)
  }
  changeICD=(e)=>{
    if(e&&e.target.value.length==0){
      this.setState({
        ICD:""
      })
    }
  }
  changediaICD=(e)=>{
    if(e&&e.target.value.length==0){
      this.setState({
        diaID:""
      })
    }
  }
  handleParms = (data) => {
    this.setState({
      ICD: data.id,
    })
    this.props.form.setFieldsValue({ ICDID: data.descripts, })
  }
  handlediaParms = (data) => {
    this.setState({
      diaID:data.id
    })
    this.props.form.setFieldsValue({ diaICDID:data.descripts })
  }
  handleClear = () => {
    if (this.state.ICD && this.state.ICD) {
      this.setState({
        // ICD,
      })
    } else {
      this.setState({ ICD: "" })
      this.props.form.setFieldsValue({ ICDID: undefined})
    }
  }
  handlediaClear = () => {
    if (this.state.diaID && this.state.diaID) {
      this.setState({
        // diaID,
      })
    } else {
      this.setState({ diaID: "" })
      this.props.form.setFieldsValue({diaICDID:undefined})
    }
  }
  getInfo=(info)=>{
    this.setState({
      info,
      ICD:info.nationDiaID,
      diaID:info.diaID
    })
  }
  clearInfo=()=>{
    this.props.form.resetFields();
    this.setState({
      info:{}
    })
  }
  saveAtt=()=>{
    const {info,ICD,diaID}=this.state
    this.props.form.validateFields((err, values) => {
      if(!err){
        let data = {
          params:[{
            diaLinkID:info&&info.diaLinkID?info.diaLinkID:"",
            diaID: diaID&&diaID || "",
            nationDiaID:ICD&&ICD || "",
            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: '05100264',
            data: data,
            success: function (res) {
              if (+res.errorCode === 0) {
                    if(info&&info.diaLinkID){
                      message.success("更新成功")
                      this.props.handleQuery()
                    }else{
                      message.success("添加成功")
                      this.props.handleQuery()
                    }
              }
            }
          })
        
      }
    })
  }
del=(info)=>{
  let data = {
    params:[{
      diaLinkID:info&&info.diaLinkID?info.diaLinkID:"",
      updateUserID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).userID : ""
    }]
  }
  $http.post('urlS', this, {
    code: '05100265',
    data: data,
    success: function (res) {
      if (+res.errorCode === 0) {
            message.success("删除成功")
            this.props.handleQuery()
            this.clearInfo()
      }
    }
  })
}

  render() {
    const { info } = 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='国家ICD' {...formLayout}>
                {getFieldDecorator('ICDID', {
                  initialValue: info.nationDiaDesc && info.nationDiaDesc ? info.nationDiaDesc : undefined,
                  rules: [{ required: true, message: '请回车检索信息' }],
                })(
                  <Input title={info.nationDiaDesc} placeholder="首拼/汉字回车进行检索" onPressEnter={(e)=>this.searchICD(e)} onChange={(e)=>this.changeICD(e)}></Input>
                )}
              </Form.Item>
            </Col>
            <Col span={6}>
              <Form.Item label='地区ICD' {...formLayout}>
                {getFieldDecorator('diaICDID', {
                  initialValue: info.diaDesc && info.diaDesc ? info.diaDesc : undefined,
                  rules: [{ required: true, message: '请回车检索信息' }],
                })(
                  <Input title={info.diaDesc} placeholder="首拼/汉字回车进行检索" onPressEnter={(e)=>this.searchdiaICD(e)} onChange={(e)=>this.changediaICD(e)}></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 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.diaLinkID} type="danger">删除</Button>
            </Popconfirm>
            <Button type="primary" onClick={this.clearInfo}>重置</Button>
          </Row>
        </Form>
        <TerminologyRAndNnaModals onRef={this.onRef} handleParms={this.handleParms} handleClear={this.handleClear}></TerminologyRAndNnaModals>
        <TerminologyRAndNreModals ondiaRef={this.ondiaRef} handlediaParms={this.handlediaParms} handlediaClear={this.handlediaClear}></TerminologyRAndNreModals>
      </div>
    )
  }
};
const TerminologyNaAndReDetail = Form.create()(TerminNaAndReDetail);
export default TerminologyNaAndReDetail;