/*
 * Creator:     ML
 * CreatDate:   2019-10-10
 * descripts 科室评估单配置 -- 新增 编辑科室评估单
 * */
import React from 'react';
import { Modal, Input, Form, Row, Col, Select, DatePicker, message } from 'antd';
import moment from 'moment';
import { $http } from 'containers/config/https';

let { Option } = Select;

class DepAssModal extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      visible: false,
      selectData: [], // 下拉框数据
      flag: 'add',
      userInfo: {}, // 编辑数据
    }
  }

  componentDidMount() {
    this.props.onRef(this);
  }

  componentWillReceiveProps(props) {
    if (this.state.selectData !== props.selectData) {
      this.setState({ selectData: props.selectData })
    }
  }

  // 新增 编辑 评估单科室
  handleOk = () => {
    const {userInfo} = this.state
    this.props.form.validateFields((err, values) => {
      if (!err) {
        let data = {
              params: [{
                userID:sessionStorage.userData ? JSON.parse(sessionStorage.userData).userID : "",
                hospID:sessionStorage.userData ? JSON.parse(sessionStorage.userData).hospID : "",
                groupID:sessionStorage.userData ? JSON.parse(sessionStorage.userData).groupID : "",
                locID: values.locID,
                id:userInfo&&userInfo.id?userInfo.id:"",
                descripts:values.descripts,
                filedNum:values.filedNum,
                startDate: values.startDate ? moment(values.startDate).format('YYYY-MM-DD') : "", // 生效日期
                stopDate: values.stopDate ? moment(values.stopDate).format('YYYY-MM-DD') : "",// 失效日期
              }]
            }
        $http.post('urlADS', this, {
          code: '03040021',
          data: data,
          success: function (res) {
            if (+res.errorCode === 0) {
              if(userInfo&&userInfo.id){
                message.success("编辑成功")
                this.setState({ visible: false }, () => {
                 this.resetFields();
               })
               if ((values.startDate && values.startDate) && (values.stopDate && values.stopDate)) {
                if (moment(values.stopDate).format('YYYYMMDD') < moment().format('YYYYMMDD')) {
                  // this.props.handleQuery(false,{},res.result)
                    // return
                }
            }
               this.props.handleQuery()
              }else{
                message.success("添加成功")
                this.setState({ visible: false }, () => {
                 this.resetFields();
               })
               if (moment(values.stopDate).format('YYYYMMDD') < moment().format('YYYYMMDD')) {
               this.props.getLast(false,res.result)
                 return
             }
               this.props.handleQuery()
               this.props.sarchInit()
              }
            }
          }
        })
      }
    })
  }

  // 关闭模态框
  handleCancel = () => {
    this.setState({ visible: false }, () => {
        // 重置数据
        this.setState({ userInfo: {} });
        this.resetFields();
    })
  }

  // 打开模态框
  showModal = (userInfo) => {
    this.setState({ visible: true, })
    if(userInfo){
      this.setState({
        userInfo
      })
    }
  }

  // 重置表单
  resetFields() {
    this.props.form.resetFields();
    this.setState({
      userInfo:{}
    })
  }
  getLocArr=(locArr)=>{
    this.setState({
      locArr
    })
  }


  render() {
    let { selectData, userInfo, visible } = this.state;
    const { getFieldDecorator } = this.props.form;
    const formItemInput = {
      labelCol: {
        span: 8
      },
      wrapperCol: {
        span: 16
      },
    };
    //科室类型
    let locOp = []
    this.state.locArr && this.state.locArr.map(item => {
        locOp.push(<Option key={item.id} role={item.descripts + '^' + item.descriptsSPCode} title={item.descripts} value={String(item.id)}>{item.descripts}</Option>)
    })

// console.log(userInfo,"userInfo")
    return (

      <Modal
        title={userInfo&&userInfo.id?"编辑评估单科室":"添加评估单科室"}
        visible={visible}
        onOk={this.handleOk}
        onCancel={this.handleCancel}
        width={600}
      >
        <div style={{ width: '100%', height: '200px' }}>
          <Form className="depart-form" style={{ minHeight: '150px', maxHeight: '170px' }}>
            <Row>
              <Col span={12}>
                <Form.Item label="名称" {...formItemInput}>
                  {getFieldDecorator('descripts', {
                    initialValue: userInfo.descripts ? userInfo.descripts : undefined,
                    rules: [{ required: true, message: '请输入挂号评估单名称' }]
                  })(
                    <Input placeholder="请输入挂号评估单名称" />
                  )}
                </Form.Item>
              </Col>
              <Col span={12} >
                <Form.Item {...formItemInput}  label='科室'>
                  {getFieldDecorator('locID', {
                    initialValue: userInfo.locID ? userInfo.locID : undefined,
                    rules: [{ required: true, message: '请选择科室' }],
                  })(
                    <Select
                      disabled={userInfo&&userInfo.id}
                      style={{ width: "100%" }}
                      allowClear={true}
                      showSearch={true}
                      optionFilterProp="role"
                    >
                      {locOp}
                    </Select>
                  )}
                </Form.Item>
              </Col>
              <Col span={12}>
                <Form.Item label="单行字段数" {...formItemInput}>
                  {getFieldDecorator('filedNum', {
                    initialValue: userInfo.filedNum ? userInfo.filedNum : undefined,
                    rules: [{ required: true, message: '请输入单行字段数' }],
                  })(
                    <Input placeholder="请输入单行字段数" />
                  )}
                </Form.Item>
              </Col>
              <Col span={12}>
                <Form.Item label="生效日期：" {...formItemInput}>
                  {getFieldDecorator('startDate', {
                    initialValue: userInfo.startDate ? moment(userInfo.startDate) : moment(),
                    rules: [{ required: true, message: '生效日期不能为空' }]
                  })(
                    <DatePicker style={{ width: '100%' }} format="YYYY-MM-DD" allowClear />
                  )}
                </Form.Item>
              </Col>
            </Row>
            <Row>
              <Col span={12}>
                <Form.Item label="失效日期：" {...formItemInput}>
                  {getFieldDecorator('stopDate', {
                    initialValue: userInfo.stopDate ? moment(userInfo.stopDate) : undefined,
                  })(
                    <DatePicker style={{ width: '100%' }} format="YYYY-MM-DD" allowClear />
                  )}
                </Form.Item>
              </Col>
            </Row>
          </Form>
        </div>
      </Modal>
    )
  }
};
const OutRegItem = Form.create()(DepAssModal);
export default OutRegItem;