/*
 * Creator:     ML
 * CreatDate:   2020-02-18
 * descripts：  黑名单配置 明细
 * */
import React from 'react';
import { Form, Input, Select, Button, message, Row, Icon, Divider, Popconfirm, DatePicker, TimePicker,Table, Col } from 'antd';
import { $http } from 'containers/config/https';
import moment from 'moment';
import store from 'store';


const { Option } = Select;

class BlackListConfigDetails extends React.Component {
  constructor() {
    super();
    var userData = JSON.parse(sessionStorage.getItem('userData'))
    this.state = {
      userData: userData,
      info:{},
      ICD:"",
      diaID:""
    };
  }

  componentDidMount() {
    this.props.onAttRef(this)
    this.getHopSelect()
    this.getBlackListType()
  }
  //获取医院下拉框
  getHopSelect = () => {
    let data = {
        params: [{
            hospID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).hospID : "",
            logonGroupID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).groupID : "",
            logonHospID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).hospID : "",
        }],
    }
    $http.post('urlS', this, {
        code: '01040001',
        data: data,
        success: function (res) {
            if (+res.errorCode === 0) {
                this.setState({
                    hospArr: res.result
                })
            }
        }
    })
}
 //获取黑名单类型下拉框
 getBlackListType = () => {
  let data = {
      params: [{
          language: this.state.userData.language,
      }],
  }
  $http.post('urlS', this, {
      code: '03050187',
      data: data,
      success: function (res) {
          if (+res.errorCode === 0) {
              this.setState({
                  blackListTypeArr: res.result.Data
              })
          }
      }
  })
}
  getInfo=(info)=>{
    this.setState({
      info,
      ICD:info.nationDiaID,
      diaID:info.diaID
    })
  }
  clearInfo=(n)=>{
    this.props.form.resetFields();
    this.setState({
      info:{}
    },()=>{
        if(n) this.props.newAdd()
    })
  }
  saveAtt=()=>{
    const {info,ICD,diaID}=this.state
    this.props.form.validateFields((err, values) => {
      if(!err){
        let data = {
          params:[{
            ID:info&&info.ID?info.ID:"",
            hospID:values&&values.hospID || "",
            period:values&&values.period || "",
            times:values&&values.times || "",
            limitDays:values&&values.limitDays || "",
            blackListTypeID:this.props.form.getFieldValue('blackListTypeID'),
            startDate:values.startDate ? moment(values.startDate).format('YYYY-MM-DD') : undefined,
            stopDate:values.stopDate ? moment(values.stopDate).format('YYYY-MM-DD') : undefined,
            createDate:moment(moment()).format('YYYY-MM-DD'),
            createTime:moment(moment()).format('HH:mm'),
            createUserID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).userID : "",
            updateUserID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).userID : "",
          }]
        } 
         $http.post('urlS', this, {
            code: '03050183',
            data: data,
            success: function (res) {
              if (+res.errorCode === 0) {
                    if(info&&info.ID){
                      message.success(this.props.localeProvider && this.props.localeProvider.BlacklistConfigurationTableIntUpdateCompleted? this.setMessage('BlacklistConfigurationTableIntUpdateCompleted', 'descripts') : '更新成功')
                      this.props.handleQuery()
                    }else{
                      message.success(this.props.localeProvider && this.props.localeProvider.BlacklistConfigurationTableIntAddedSuccessfully? this.setMessage('BlacklistConfigurationTableIntAddedSuccessfully', 'descripts') : '添加成功')
                      this.props.handleQuery()
                    }
              }
            }
          })
        
      }
    })
  }
del=(info)=>{
  let data = {
    params:[{
      ID:info&&info.ID?info.ID:"",
      updateUserID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).userID : ""
    }]
  }
  $http.post('urlS', this, {
    code: '03050182',
    data: data,
    success: function (res) {
      if (+res.errorCode === 0) {
            message.success(this.props.localeProvider && this.props.localeProvider.BlacklistConfigurationTableIntSuccessfullyDeleted? this.setMessage('BlacklistConfigurationTableIntSuccessfullyDeleted', 'descripts') : '删除成功')
            this.props.handleQuery()
            this.clearInfo("1")
      }
    }
  })
}
  //国际化
  setMessage(name, message) {
    return this.props.localeProvider ? this.props.localeProvider[name][message] : ''
}
  render() {
    const { info } = this.state;

    const { getFieldDecorator } = this.props.form;
//医院
let hosOp = []
this.state.hospArr && this.state.hospArr.map((item) => {
    hosOp.push(<Option key={item.id}  value={String(item.id)}>{item.descripts}</Option>)
})
 //黑名单类型
 let blackListTypeOp = []
 this.state.blackListTypeArr && this.state.blackListTypeArr.map((item) => {
   blackListTypeOp.push(<Option key={item.id} value={String(item.id)}>{item.descripts}</Option>)
 })
    const formLayout = {
      labelCol: { span: 10 },
      wrapperCol: { span: 14 },
    }
    const formLayoutType = {
      labelCol: { span: 12 },
      wrapperCol: { span: 12 },
    }

    return (
      <div>
        <Form layout={'horizontal'}>
          <Row>
            <Col span={6}>
              <Form.Item label={this.props.localeProvider && this.props.localeProvider.BlacklistConfigurationTableIntHospital ? this.setMessage('BlacklistConfigurationTableIntHospital', 'descripts') : '医院'} {...formLayoutType} >
                {getFieldDecorator('hospID', {
                  initialValue: info.hospID && info.hospID ? String(info.hospID) : undefined,
                  rules: [{ required: true, message: this.props.localeProvider && this.props.localeProvider.BlacklistConfigurationTableIntHospital ? this.setMessage('BlacklistConfigurationTableIntHospital', 'message') : '请选择医院'}],
                })(
                    <Select
                >
                    {hosOp}
                </Select>
                )}
              </Form.Item>
            </Col>
            <Col span={6}>
              <Form.Item label={this.props.localeProvider && this.props.localeProvider.BlacklistConfigurationTableIntCycle ? this.setMessage('BlacklistConfigurationTableIntCycle', 'descripts') : '周期(天)'} {...formLayout}>
                {getFieldDecorator('period', {
                  initialValue: info.period && info.period ? info.period : undefined,
                  rules: [{ required: true, message: this.props.localeProvider && this.props.localeProvider.BlacklistConfigurationTableIntCycle ? this.setMessage('BlacklistConfigurationTableIntCycle', 'message') : '请填写周期(天)' }],
                })(
                  <Input type="number"></Input>
                )}
              </Form.Item>
            </Col>
            <Col span={6}>
              <Form.Item label={this.props.localeProvider && this.props.localeProvider.BlacklistConfigurationTableIntNumberOfAppointments ? this.setMessage('BlacklistConfigurationTableIntNumberOfAppointments', 'descripts') : '爽约次数'} {...formLayout}>
                {getFieldDecorator('times', {
                  initialValue: info.times && info.times ? info.times : undefined,
                  rules: [{ required: true, message:this.props.localeProvider && this.props.localeProvider.BlacklistConfigurationTableIntNumberOfAppointments ? this.setMessage('BlacklistConfigurationTableIntNumberOfAppointments', 'message') : '请填写爽约次数' }],
                })(
                  <Input type="number"></Input>
                )}
              </Form.Item>
            </Col>
            <Col span={6}>
              <Form.Item label={this.props.localeProvider && this.props.localeProvider.BlacklistConfigurationTableIntLimitAppointmentDays ? this.setMessage('BlacklistConfigurationTableIntLimitAppointmentDays', 'descripts') : '限制预约天数'} {...formLayout}>
                {getFieldDecorator('limitDays', {
                  initialValue: info.limitDays && info.limitDays ? info.limitDays : undefined,
                  rules: [{ required: true, message: this.props.localeProvider && this.props.localeProvider.BlacklistConfigurationTableIntLimitAppointmentDays ? this.setMessage('BlacklistConfigurationTableIntLimitAppointmentDays', 'message') : '请填写限制预约天数' }],
                })(
                  <Input type="number"></Input>
                )}
              </Form.Item>
            </Col>
          </Row>
          <Row>
          <Col span={6}>
              <Form.Item label={this.props.localeProvider && this.props.localeProvider.BlacklistInformationInterfacBlacklistType ? this.setMessage('BlacklistInformationInterfacBlacklistType', 'descripts') : '黑名单类型'}  {...formLayoutType}>
                {getFieldDecorator('blackListTypeID', {
                  initialValue: info.blackListTypeID && info.blackListTypeID ? String(info.blackListTypeID) : undefined,
                  rules: [{ required: true, message: this.props.localeProvider && this.props.localeProvider.BlacklistInformationInterfacBlacklistType ? this.setMessage('BlacklistInformationInterfacBlacklistType', 'message') : '请选择黑名单类型' }],
                })(
                  <Select
                  >
                    {blackListTypeOp}
                  </Select>
                )}
              </Form.Item>
            </Col>
          <Col span={6} className="starDataDom">
              <Form.Item label={this.props.localeProvider && this.props.localeProvider.BlacklistConfigurationTableIntEffectiveDate ? this.setMessage('BlacklistConfigurationTableIntEffectiveDate', 'descripts') : '生效日期'} {...formLayout}>
                {getFieldDecorator('startDate', {
                  initialValue: info && info.startDate ? moment(info.startDate, "YYYY-MM-DD") : moment(),
                  rules: [{ required: true, message: this.props.localeProvider && this.props.localeProvider.BlacklistConfigurationTableIntEffectiveDate ? this.setMessage('BlacklistConfigurationTableIntEffectiveDate', 'message') : '请选择开始日期' }],
                })(
                  <DatePicker style={{ width: '100%' }} disabledDate={(curDate) => {
                    return curDate && curDate > moment().endOf('day');
                  }} />
                )}
              </Form.Item>
            </Col>
            <Col span={6} >
              <Form.Item label={this.props.localeProvider && this.props.localeProvider.BlacklistConfigurationTableIntExpirationDate ? this.setMessage('BlacklistConfigurationTableIntExpirationDate', 'descripts') : '失效日期'} {...formLayout}>
                {getFieldDecorator('stopDate', {
                  initialValue: info && info.stopDate ? moment(info.stopDate, "YYYY-MM-DD") : undefined,
                  rules: [{ required: false, message: '' }],
                })(
                  <DatePicker style={{ width: '100%' }} />
                )}
              </Form.Item>
            </Col >
            <Col span={6}>
              <Form.Item label={this.props.localeProvider && this.props.localeProvider.BlacklistConfigurationTableIntCreationDate ? this.setMessage('BlacklistConfigurationTableIntCreationDate', 'descripts') : '创建日期'} {...formLayout}>
                {getFieldDecorator('createDate', {
                  initialValue: info && info.createDate ? moment(info.createDate, "YYYY-MM-DD")  : moment(moment(), "YYYY-MM-DD"),
                  rules: [{ required: false, message: '' }],
                })(
                    <DatePicker style={{ width: '100%' }} disabled  />
                )}
              </Form.Item>
            </Col>
          </Row>
          <Row style={{display:"flex",justifyContent:"space-between",justifyItems:"center",alignItems:"center",height:"48px"}}>
                <Button type="primary" onClick={this.saveAtt}>{info&&info.ID?this.props.localeProvider && this.props.localeProvider.BlacklistConfigurationTableIntUpdate? this.setMessage('BlacklistConfigurationTableIntUpdate', 'descripts') : '更新':this.props.localeProvider && this.props.localeProvider.BlacklistConfigurationTableIntAdd? this.setMessage('BlacklistConfigurationTableIntAdd', 'descripts') : '新增'}</Button>
            <Popconfirm title={this.props.localeProvider && this.props.localeProvider.BlacklistConfigurationTableIntWhether ? this.setMessage('BlacklistConfigurationTableIntWhether', 'descripts') : '是否要删除该配置'} onConfirm={() => this.del(info)}>
              <Button disabled={info&&!info.ID} type="danger">{this.props.localeProvider && this.props.localeProvider.BlacklistConfigurationTableIntDelete ? this.setMessage('BlacklistConfigurationTableIntDelete', 'descripts') : '删除'}</Button>
            </Popconfirm>
            <Button type="primary" onClick={()=>this.clearInfo("1")}>{this.props.localeProvider && this.props.localeProvider.BlacklistConfigurationTableIntReset? this.setMessage('BlacklistConfigurationTableIntReset', 'descripts') : '重置'}</Button>
          </Row>
        </Form>
      </div>
    )
  }
};
const BlackListConfigDetail = Form.create()(BlackListConfigDetails);
export default BlackListConfigDetail;