/*
 * Creator:     ML
 * CreatDate:   2019-10-22
 * 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 '../style/childStyle.less';
import CommonPagination from 'components/pagination/index';

// import PubilcTablePagination from 'pages/common/PubilcTablePagination.jsx';

const { Option } = Select;

class OtherItemnewDet extends React.Component {
  constructor() {
    super();
    var userData = JSON.parse(sessionStorage.getItem('userData'));
    this.state = {
      info:{},
      typeData:[],
      linkData:[]
    };
  }

  componentDidMount() {
    this.props.onOthRef(this)
    this.getType()
    this.getLink()
  }
  getInfo=(info)=>{
    this.setState({
      info,
    })
  }
  clearInfo=()=>{
    this.props.form.resetFields();
    this.setState({
      info:{}
    })
    this.props.clearRow()
  }
  getLink=()=>{
    let data = {
      params: [{
      }],
     
    }
    $http.post('urlS', this, {
      code: '03010092',
      data: data,
      success: function (res) {
        if (+res.errorCode === 0) {
          this.setState({
            linkData:res.result.Data,
          })
        }
      }
    })
  }
  getType=()=>{
    let data = {
      params: [{
      }],
     
    }
    $http.post('urlS', this, {
      code: '03010093',
      data: data,
      success: function (res) {
        if (+res.errorCode === 0) {
          this.setState({
            typeData:res.result.Data,
          })
        }
      }
    })
  }
  onPanelChange=(mode,num)=>{
    //策略
    let obj = {
     "1": () =>this.props.form.setFieldsValue({ startDate:  undefined }),
     "2": () =>this.props.form.setFieldsValue({ stopDate: undefined }),
 }
   if(!mode){
     if (obj[num]) {
       obj[num]()
   }
   }
 }
  saveAtt=()=>{
    const {info}=this.state
    this.props.form.validateFields((err, values) => {
      if(!err){
        let data = {
          params:[{
            otherItemId:info&&info.otherItemId?info.otherItemId:"",
            otherItemCode: values&&values.code || "",
            otherItemDesc:values&&values.dec || "",
            tableData:values&&values.tableData || "",
            fieldTypeId:values&&values.fieldTypeId || "",
            linkFieldId:values&&values.linkFieldId || "",
            fieldUnit:values&&values.fieldUnit ||  "",
            dataType:values&&values.dataType || "",
            normalRangeTo:values&&values.normalRangeTo || "",
            normalRangeFrom:values&&values.normalRangeFrom || "",
            startDate:values.startDate ? moment(values.startDate).format('YYYY-MM-DD') : "",
            stopDate:values.stopDate ? moment(values.stopDate).format('YYYY-MM-DD') : "",
            updateUserID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).userID : ""
          }]
        }
        if(info&&info.otherItemId){
          $http.post('urlS', this, {
            code: '03010089',
            data: data,
            success: function (res) {
              if (+res.errorCode === 0) {
                    message.success("更新成功")
                    this.props.handleQuery()
              }
            }
          })
        }else{
          $http.post('urlS', this, {
            code: '03010090',
            data: data,
            success: function (res) {
              if (+res.errorCode === 0) {
                    message.success("添加成功")
                    this.props.handleQuery()
              }
            }
          })
        }
      }
    })
  }
del=(info)=>{
  let data = {
    params:[{
      otherItemId:info&&info.otherItemId?info.otherItemId:"",
      updateUserID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).userID : ""
    }]
  }
  $http.post('urlS', this, {
    code: '03010091',
    data: data,
    success: function (res) {
      if (+res.errorCode === 0) {
            message.success("删除成功")
            this.props.handleQuery()
            this.clearInfo()
      }
    }
  })
}
  render() {
    const { info,typeData,linkData } = this.state;

    const { getFieldDecorator } = this.props.form;
    const dataTypeData = [{
      id:"N",
      descripts:"Number"
    },{  
      id:"T",
    descripts:"Text"
    }]
    //字段类型
    let fieldTypeOpt = []
    typeData&&typeData.map((item,i)=>{
     return(
      fieldTypeOpt.push(<Option key={i} value={item.id} >{item.descripts}</Option>)
     )
    })
    //LinkFieldDr
    let linkFieldOpt = []
    linkData&&linkData.length>0&&linkData.map((item,i)=>{
     return(
      linkFieldOpt.push(<Option key={i} value={item.id} >{item.descripts}</Option>)
     )
    })
    //dataTypeOpt
    let dataTypeOpt = []
    dataTypeData&&dataTypeData.map((item,i)=>{
     return(
      dataTypeOpt.push(<Option  key={i} value={item.id} >{item.descripts}</Option>)
     )
    })

    const formLayout = {
      labelCol: { span: 10 },
      wrapperCol: { span: 14 },
    }

    return (
      <div>
        <Form layout={'horizontal'}>
          <Row>
            <Col span={6}>
              <Form.Item label="代码" {...formLayout}>
                {getFieldDecorator('code', {
                  initialValue: info.fieldTypeCode && info.fieldTypeCode ? info.fieldTypeCode : undefined,
                  rules: [{ required: true, message: '请输入代码' }]
                })(
                  <Input ></Input>
                )}
              </Form.Item>
            </Col>
            <Col span={6}>
              <Form.Item label='描述' {...formLayout}>
                {getFieldDecorator('dec', {
                  initialValue: info.fieldTypeDesc && info.fieldTypeDesc ? info.fieldTypeDesc : 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%' }}  allowClear={false} placeholder={'开始日期'} onChange={()=>this.onPanelChange("1")}  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={'请选择失效日期'} onChange={()=>this.onPanelChange("2")}  />
                )}
              </Form.Item>
            </Col >
          </Row>
          <Row>
          <Col span={6}>
              <Form.Item label='tableData' {...formLayout}>
                {getFieldDecorator('tableData', {
                  initialValue: info.tableData && info.tableData ? info.tableData : undefined,
                  rules: [{ required: false, message: '' }],
                })(
                  <Input ></Input>
                )}
              </Form.Item>
            </Col>
            <Col span={6}>
              <Form.Item label='字段单位' {...formLayout}>
                {getFieldDecorator('fieldUnit', {
                  initialValue: info.fieldUnit && info.fieldUnit ? info.fieldUnit : undefined,
                  rules: [{ required: false, message: '' }],
                })(
                  <Input ></Input>
                )}
              </Form.Item>
            </Col>
            <Col span={6}>
              <Form.Item label='字段类型' {...formLayout}>
                {getFieldDecorator('fieldTypeId', {
                  initialValue: info.fieldTypeId && info.fieldTypeId ? info.fieldTypeId : undefined,
                  rules: [{ required: true, message: '请选择字段ID' }],
                })(
                  <Select>
                    {fieldTypeOpt}
                  </Select>
                )}
              </Form.Item>
            </Col>
            <Col span={6}>
              <Form.Item label='数据类型' {...formLayout}>
                {getFieldDecorator('dataType', {
                  initialValue: info.dataType && info.dataType?info.dataType:undefined,
                  rules: [{ required: false, message: '请选择字段ID' }],
                })(
                  <Select allowClear>
                    {dataTypeOpt}
                  </Select>
                )}
              </Form.Item>
            </Col>
          </Row>
          <Row>
          <Col span={6}>
              <Form.Item label='正常低值' {...formLayout}>
                {getFieldDecorator('normalRangeFrom', {
                  initialValue: info.normalRangeFrom && info.normalRangeFrom ? info.normalRangeFrom : undefined,
                  rules: [{ required: false, message: '' }],
                })(
                  <Input></Input>
                )}
              </Form.Item>
            </Col>
          <Col span={6}>
              <Form.Item label='正常高值' {...formLayout}>
                {getFieldDecorator('normalRangeTo', {
                  initialValue: info.normalRangeTo && info.normalRangeTo ? info.normalRangeTo : undefined,
                  rules: [{ required: false, message: '' }],
                })(
                  <Input></Input>
                )}
              </Form.Item>
            </Col>
          <Col span={6}>
              <Form.Item label='相关联字段' {...formLayout}>
                {getFieldDecorator('linkFieldId', {
                  initialValue: info.linkFieldId && info.linkFieldId ? info.linkFieldId : undefined,
                  rules: [{ required: false, message: ' ' }],
                })(
                  <Select allowClear>
                    {linkFieldOpt}
                  </Select>
                )}
              </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.otherItemId} type="danger">删除</Button>
            </Popconfirm>
            <Button type="primary" onClick={this.clearInfo}>重置</Button>
          </Row>
        </Form>
      </div>
    )
  }
};
const OtherItemnewDetail = Form.create()(OtherItemnewDet);
export default OtherItemnewDetail;