/*
 * 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 ProcessMethodnewDet extends React.Component {
  constructor() {
    super();
    var userData = JSON.parse(sessionStorage.getItem('userData'));
    this.state = {
      info:{}
    };
  }

  componentDidMount() {
    this.props.onPrmRef(this)
  }
  getInfo=(info)=>{
    this.setState({
      info,
    })
  }
  clearInfo=()=>{
    this.props.form.resetFields();
    this.setState({
      info:{}
    })
    // this.props.handleQuery()
  }
  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:[{
            methodId:info&&info.methodId?info.methodId:"",
            methodCode: values&&values.code || "",
            methodDesc:values&&values.dec || "",
            note:values&&values.note || "",
            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.methodId){
          $http.post('urlS', this, {
            code: '03010085',
            data: data,
            success: function (res) {
              if (+res.errorCode === 0) {
                    message.success("更新成功")
                    this.props.handleQuery()
              }
            }
          })
        }else{
          $http.post('urlS', this, {
            code: '03010086',
            data: data,
            success: function (res) {
              if (+res.errorCode === 0) {
                    message.success("添加成功")
                    this.props.handleQuery()
              }
            }
          })
        }
      }
    })
  }
del=(info)=>{
  let data = {
    params:[{
      methodId:info&&info.methodId?info.methodId:"",
      updateUserID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).userID : ""
    }]
  }
  $http.post('urlS', this, {
    code: '03010087',
    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="代码" {...formLayout}>
                {getFieldDecorator('code', {
                  initialValue: info.methodCode && info.methodCode ? info.methodCode : undefined,
                  rules: [{ required: true, message: '请输入代码' }]
                })(
                  <Input ></Input>
                )}
              </Form.Item>
            </Col>
            <Col span={6}>
              <Form.Item label='描述' {...formLayout}>
                {getFieldDecorator('dec', {
                  initialValue: info.methodDesc && info.methodDesc ? info.methodDesc : 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 allowClear={false} style={{ width: '100%' }} 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='备注' {...formLayout}>
                {getFieldDecorator('note', {
                  initialValue: info.note && info.note ? info.note : undefined,
                  rules: [{ required: true, message: '请输入备注' }],
                })(
                  <Input ></Input>
                )}
              </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.methodId} type="danger">删除</Button>
            </Popconfirm>
            <Button type="primary" onClick={this.clearInfo}>重置</Button>
          </Row>
        </Form>
      </div>
    )
  }
};
const ProcessMethodnewDetail = Form.create()(ProcessMethodnewDet);
export default ProcessMethodnewDetail;