/*
 * Creator:     ML
 * CreatDate:   2019-10-24
 * descripts：  地区术语集 明细
 * */
import React from 'react';
import { Form, Input, Select, Button, message, Row, Icon, Divider, Popconfirm, DatePicker, Table, Col, Switch } from 'antd';
import { $http } from 'containers/config/https';
import moment from 'moment';
import store from 'store';
import TerminologyReModal from './TerminologyReModal'

const { Option } = Select;

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

  componentDidMount() {
    this.props.onAttRef(this)
    this.getSelectSex()
    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})
  }
}
  getSelectSex = () => {
    let data = {}
    data = {
      params: [{
        sexFlag: 1,//性别
      }]
    }
    $http.post('urlS', this, {
      code: '03030005',
      data: data,
      success: function (res) {
        if (+res.errorCode === 0) {
          this.setState({
            sexArr: res.sex
          })
        }
      }
    })
  }
  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,
      ICD:info.ICDID
    })
  }
  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: [{
            diagnosisID: info && info.diagnosisID ? info.diagnosisID : "",
            diagnosisCode: values && values.code || "",
            diagnosisDesc: values && values.dec || "",
            stAge: values && values.stAge || "",
            endAge: values && values.endAge || "",
            sexID: values && values.sexID || "",
            mainTypeID: values && values.mainTypeID || "",
            ICDID: ICD && ICD|| "",
            injuryMark: values && values.injuryMark == true || info.injuryMark=="Y" ? "Y":"N",
            pathologicalMark: values && values.pathologicalMark == true || info.pathologicalMark=="Y" ? "Y" : "N",
            diagnosticMark: values && values.diagnosticMark == true || info.diagnosticMark=="Y"  ? "Y" : "N",
            outPatFlag: values && values.outPatFlag == true || info.outPatFlag=="Y" ? "Y" : "N",
            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: '05100215',
          data: data,
          success: function (res) {
            if (+res.errorCode === 0) {
              if (info && info.diagnosisID) {
                message.success("更新成功")
                this.props.handleQuery()
              } else {
                message.success("添加成功")
                this.props.handleQuery()
              }
            }
          }
        })

      }
    })
  }
  del = (info) => {
    let data = {
      params: [{
        diagnosisID: info && info.diagnosisID ? info.diagnosisID : "",
        updateUserID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).userID : ""
      }]
    }
    $http.post('urlS', this, {
      code: '05100216',
      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, sexArr, mainTypeArr, icdArr } = this.state;

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

    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.diagnosisCode && info.diagnosisCode ? info.diagnosisCode : undefined,
                  rules: [{ required: true, message: '请输入代码' }]
                })(
                  <Input ></Input>
                )}
              </Form.Item>
            </Col>
            <Col span={6}>
              <Form.Item label='描述' {...formLayout}>
                {getFieldDecorator('dec', {
                  initialValue: info.diagnosisDesc && info.diagnosisDesc ? info.diagnosisDesc : undefined,
                  rules: [{ required: true, message: '请输入描述' }],
                })(
                  <Input title={info.diagnosisDesc}></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('stAge', {
                  initialValue: info.stAge && info.stAge ? info.stAge : undefined,
                  rules: [{ required: false, message: '' }],
                })(
                  <Input ></Input>
                )}
              </Form.Item>
            </Col>
            <Col span={6}>
              <Form.Item label='年龄结束' {...formLayout}>
                {getFieldDecorator('endAge', {
                  initialValue: info.endAge && info.endAge ? info.endAge : undefined,
                  rules: [{ required: false, message: '' }],
                })(
                  <Input ></Input>
                )}
              </Form.Item>
            </Col>
            <Col span={6}>
              <Form.Item label='性别' {...formLayout}>
                {getFieldDecorator('sexID', {
                  initialValue: info.sexID && info.sexID ? info.sexID : undefined,
                  rules: [{ required: false, message: '' }],
                })(
                  <Select>
                    {sexOpt}
                  </Select>
                )}
              </Form.Item>
            </Col>
            <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>
          </Row>
          <Row>
            <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>
          </Row>
          <Row>
            <Col span={6}>
              <Form.Item label='损伤中毒' {...formLayout}>
                {getFieldDecorator('injuryMark', {
                  rules: [{ required: false, message: '' }],
                })(
                  <Switch checkedChildren="是" checked={info.injuryMark == "Y" ? true : false} unCheckedChildren="否" onChange={(v) => { this.onChangeSwith(v, "injuryMark") }} />
                )}
              </Form.Item>
            </Col>
            <Col span={6}>
              <Form.Item label='病理学' {...formLayout}>
                {getFieldDecorator('pathologicalMark', {
                  rules: [{ required: false, message: '' }],
                })(
                  <Switch checkedChildren="是" checked={info.pathologicalMark == "Y" ? true : false} unCheckedChildren="否" onChange={(v) => { this.onChangeSwith(v, "pathologicalMark") }} />
                )}
              </Form.Item>
            </Col>
            <Col span={6}>
              <Form.Item label='中医诊断' {...formLayout}>
                {getFieldDecorator('diagnosticMark', {
                  rules: [{ required: false, message: '' }],
                })(
                  <Switch checkedChildren="是" checked={info.diagnosticMark == "Y" ? true : false} unCheckedChildren="否" onChange={(v) => { this.onChangeSwith(v, "diagnosticMark") }} />
                )}
              </Form.Item>
            </Col>
            <Col span={6}>
              <Form.Item label='门诊诊断' {...formLayout}>
                {getFieldDecorator('outPatFlag', {
                  rules: [{ required: false, message: '' }],
                })(
                  <Switch checkedChildren="是" checked={info.outPatFlag == "Y" ? true : false} unCheckedChildren="否" onChange={(v) => { this.onChangeSwith(v, "outPatFlag") }} />
                )}
              </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.diagnosisID} type="danger">删除</Button>
            </Popconfirm>
            <Button type="primary" onClick={this.clearInfo}>重置</Button>
          </Row>
        </Form>
        <TerminologyReModal onRef={this.onRef} handleParms={this.handleParms} handleClear={this.handleClear}></TerminologyReModal>
      </div>
    )
  }
};
const TerminologyReDetail = Form.create()(TerminReDetail);
export default TerminologyReDetail;