// 新建模板
import React from 'react';
import { Form, Input, Row, Col, Modal, message, Select, Checkbox } from 'antd';
import { $http } from 'containers/config/https';

let { Option } = Select;

class NewBusinessAuthorization extends React.Component {
  constructor() {
    super();
    this.state = {
      userInfo: {},
      treeSelectData: {},
      rowID: '',
      hospSelectData: [],
      pubAccMsgFlag: '',
      minProgramMsgFlag: '',
      SMSMsgFlag: '',
      aliPayMsgFlag: '',
      appMsgFlag: '',
    }
  }

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

  // 关闭弹窗
  handleCancel = () => {
    this.setState({ visible: false })
  }

  onChangeFlag=(e)=>{
    if (e.target["data-__field"].name === 'pubAccMsg'){
      this.setState({
        pubAccMsgFlag: e.target.checked === true ? 'Y' : 'N'
      })
    }
    if(e.target["data-__field"].name === 'minProgramMsg'){
      this.setState({
        minProgramMsgFlag:  e.target.checked === true ? 'Y' : 'N'
      })
    }
    if (e.target["data-__field"].name === 'SMSMsg'){
      this.setState({
        SMSMsgFlag:  e.target.checked === true ? 'Y' : 'N'
      })
    }
    if (e.target["data-__field"].name === 'aliPayMsg'){
      this.setState({
        aliPayMsgFlag:  e.target.checked === true ? 'Y' : 'N'
      })
    }
    if (e.target["data-__field"].name === 'appMsg'){
      this.setState({
        appMsgFlag:  e.target.checked === true ? 'Y' : 'N'
      })
    }
  }
  // 打开弹窗
  showModal = (obj,rowID) => {
    this.setState({
      visible: true,
      userInfo: obj,
      rowID: rowID  ? rowID : '',
    },()=>{
      let obj = this.props.form.getFieldsValue()
      this.setState({
        pubAccMsgFlag: obj.pubAccMsg === true ? 'Y' : 'N',
        minProgramMsgFlag: obj.minProgramMsg === true ? 'Y' : 'N',
        SMSMsgFlag: obj.SMSMsg === true ? 'Y' : 'N',
        aliPayMsgFlag: obj.aliPayMsg === true ? 'Y' : 'N',
        appMsgFlag: obj.appMsg === true ? 'Y' : 'N',
      })
      
    })
  }

  // 保存
  handleSave = (obj) => {
    let {userInfo}=this.state
    let data = {
      params: [{
        hospID: obj.hospID || '',
        id: userInfo.id ? userInfo.id : '',
        pubAccMsg: obj.pubAccMsg === true ? 'Y' : 'N',
        minProgramMsg: obj.minProgramMsg === true ? 'Y' : 'N',
        SMSMsg: obj.SMSMsg === true ? 'Y' : 'N',
        aliPayMsg: obj.aliPayMsg === true ? 'Y' : 'N',
        appMsg: obj.appMsg === true ? 'Y' : 'N',
        PBMsgAppCode:obj.PBMsgAppCode || '',
        PBMsgTemplateCode:obj.PBMsgTemplateCode || '',
        MPMsgAppCode:obj.MPMsgAppCode || '',
        MPMsgTemplateCode:obj.MPMsgTemplateCode || '',
        SMSMsgAppCode:obj.SMSMsgAppCode || '',
        SMSMsgTemplateCode:obj.SMSMsgTemplateCode || '',
        ALMsgAppCode:obj.ALMsgAppCode || '',
        ALMsgTemplateCode:obj.ALMsgTemplateCode || '',
        AppMsgAppCode:obj.AppMsgAppCode || '',
        AppMsgTemplateCode:obj.AppMsgTemplateCode || '',
        busMessageID: this.state.rowID
      }]
    };
    $http.post('urlS', this, {
      code: '03100171',
      data: data,
      success: function (res) {
        if (+res.errorCode === 0 ) {
          this.resetFields();
          this.setState({
            visible: false,
          },()=>{
            this.props.getBusinessAuthorizationList();
          })
        } else {
         
        }
      }
    })
  }

  handelInterface = (code) => {
    const { form: { validateFields } } = this.props;
    validateFields((errors, values) => {
      // 验证成功
      if (errors === null) {
        console.log(values)
        this.handleSave(values)
      }
    });
  }

  getHospSelectData = () => {
    let data = {
      params: [{}]
    };
    $http.post('urlS', this, {
      code: '03100168',
      data: data,
      success: function (res) {
        if (+res.errorCode === 0 && res.hospital) {
          this.setState({ hospSelectData: res.hospital })
        } else {
          this.setState({ hospSelectData: [] })
        }
      }
    })
  }
  // 重置表单
  resetFields() {
    this.props.form.resetFields();
  }

  render() {
    const { getFieldDecorator } = this.props.form;
    let { visible, flag, userInfo, hospSelectData,pubAccMsgFlag,minProgramMsgFlag,SMSMsgFlag,aliPayMsgFlag,appMsgFlag } = this.state;
    let {obj}= this.props.form.getFieldsValue()
    const formItemInput = {
      labelCol: { span: 12 },
      wrapperCol: { span: 12 }
    };
    let hospOption = hospSelectData && hospSelectData.map((item, index) => {
      return (<Option key={index} value={item.id} title={item.descripts} seatchprop={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>)
    })
    return (
      <Modal
        visible={visible}
        title={flag === 'add' ? '新增模板目录树' : '编辑模板目录树'}
        okText="保存"
        onCancel={this.handleCancel}
        onOk={this.handelInterface}
        width={1000}
      >
        <Form>
          
          <Row>
            <Col span={6}>
              <Form.Item labelAlign='left' label="医院:" {...formItemInput}>
                {getFieldDecorator('hospID', {
                  initialValue: userInfo && userInfo.hospID ? userInfo.hospID : undefined,
                  rules: [{ required: true, message: '医院不能为空' }]
                })(
                  <Select  placeholder="请选择" showSearch optionFilterProp="seatchprop" allowClear>
                    {hospOption}
                  </Select>
                )}
              </Form.Item>
            </Col>
          </Row>
          <Row>
          <Col span={6}>
              <Form.Item labelAlign='left' label="微信公众号消息:" {...formItemInput}>
                {getFieldDecorator('pubAccMsg', {
                  valuePropName: 'checked',
                  initialValue: userInfo && userInfo.pubAccMsg ? (userInfo.pubAccMsg === 'Y' ? true : false) : false,
                  rules: []
                })(
                  <Checkbox checkedChildren="是" unCheckedChildren="否" onChange={this.onChangeFlag}/>
                )}
              </Form.Item>
            </Col>
            <Col span={9}>
              <Form.Item labelAlign='right' label="微信公众号主体代码：" {...formItemInput}>
                {getFieldDecorator('PBMsgAppCode', {
                  initialValue: userInfo && userInfo.PBMsgAppCode ? userInfo.PBMsgAppCode : undefined,
                  rules: [{ required: pubAccMsgFlag === 'Y' ? true : false, message: '微信公众号主体代码不能为空' }]
                })(
                  <Input placeholder="请输入" />
                )}
              </Form.Item>
            </Col>
            <Col span={9}>
              <Form.Item labelAlign='right' label="微信公众号模板代码：" {...formItemInput}>
                {getFieldDecorator('PBMsgTemplateCode', {
                  initialValue: userInfo && userInfo.PBMsgTemplateCode ? userInfo.PBMsgTemplateCode : undefined,
                  rules: [{ required: pubAccMsgFlag === 'Y' ? true : false, message: '微信公众号模板代码不能为空' }]
                })(
                  <Input placeholder="请输入" />
                )}
              </Form.Item>
            </Col>
          </Row>
          <Row>
          <Col span={6}>
              <Form.Item labelAlign='left' label="微信小程序消息:" {...formItemInput}>
                {getFieldDecorator('minProgramMsg', {
                  valuePropName: 'checked',
                  initialValue: userInfo && userInfo.minProgramMsg ? (userInfo.minProgramMsg === 'Y' ? true : false) : false,
                  rules: []
                })(
                  <Checkbox checkedChildren="是" unCheckedChildren="否"  onChange={this.onChangeFlag}/>
                )}
              </Form.Item>
            </Col>
            <Col span={9}>
              <Form.Item  labelAlign='right' label="微信小程序主体代码：" {...formItemInput}>
                {getFieldDecorator('MPMsgAppCode', {
                  initialValue: userInfo && userInfo.MPMsgAppCode ? userInfo.MPMsgAppCode : undefined,
                  rules: [{ required: minProgramMsgFlag === 'Y' ? true : false, message: '微信小程序主体代码不能为空' }]
                })(
                  <Input placeholder="请输入" />
                )}
              </Form.Item>
            </Col>
            <Col span={9}>
              <Form.Item labelAlign='right'  label="微信小程序模板代码：" {...formItemInput}>
                {getFieldDecorator('MPMsgTemplateCode', {
                  initialValue: userInfo && userInfo.MPMsgTemplateCode ? userInfo.MPMsgTemplateCode : undefined,
                  rules: [{ required: minProgramMsgFlag === 'Y' ? true : false, message: '微信小程序模板代码不能为空' }]
                })(
                  <Input placeholder="请输入" />
                )}
              </Form.Item>
            </Col>
          </Row>
          <Row>
          <Col span={6}>
              <Form.Item labelAlign='left' label="短信消息" {...formItemInput}>
                {getFieldDecorator('SMSMsg', {
                  valuePropName: 'checked',
                  initialValue: userInfo && userInfo.SMSMsg ? (userInfo.SMSMsg === 'Y' ? true : false) : false,
                  rules: []
                })(
                  <Checkbox checkedChildren="是" unCheckedChildren="否"  onChange={this.onChangeFlag}/>
                )}
              </Form.Item>
            </Col>
            <Col span={9}>
              <Form.Item labelAlign='right' label="短信主体代码：" {...formItemInput}>
                {getFieldDecorator('SMSMsgAppCode', {
                  initialValue: userInfo && userInfo.SMSMsgAppCode ? userInfo.SMSMsgAppCode : undefined,
                  rules: [{ required: SMSMsgFlag  === 'Y' ? true : false, message: '短信主体代码不能为空' }]
                })(
                  <Input placeholder="请输入" />
                )}
              </Form.Item>
            </Col>
            <Col span={9}>
              <Form.Item labelAlign='right'  label="短信主体模板代码：" {...formItemInput}>
                {getFieldDecorator('SMSMsgTemplateCode', {
                  initialValue: userInfo && userInfo.SMSMsgTemplateCode ? userInfo.SMSMsgTemplateCode : undefined,
                  rules: [{ required: SMSMsgFlag  === 'Y' ? true : false, message: '短信主体模板代码不能为空' }]
                })(
                  <Input placeholder="请输入" />
                )}
              </Form.Item>
            </Col>
          </Row>
          <Row>
          <Col span={6}>
              <Form.Item labelAlign='left' label="支付宝消息:" {...formItemInput}>
                {getFieldDecorator('aliPayMsg', {
                  valuePropName: 'checked',
                  initialValue: userInfo && userInfo.aliPayMsg ? (userInfo.aliPayMsg === 'Y' ? true : false) : false,
                  rules: []
                })(
                  <Checkbox checkedChildren="是" unCheckedChildren="否"  onChange={this.onChangeFlag}/>
                )}
              </Form.Item>
            </Col>
            <Col span={9}>
              <Form.Item labelAlign='right' label="支付宝主体代码：" {...formItemInput}>
                {getFieldDecorator('ALMsgAppCode', {
                  initialValue: userInfo && userInfo.ALMsgAppCode ? userInfo.ALMsgAppCode : undefined,
                  rules: [{ required: aliPayMsgFlag  === 'Y' ? true : false, message: '支付宝主体代码不能为空' }]
                })(
                  <Input placeholder="请输入" />
                )}
              </Form.Item>
            </Col>
            <Col span={9}>
              <Form.Item labelAlign='right' label="支付宝主体模板代码：" {...formItemInput}>
                {getFieldDecorator('ALMsgTemplateCode', {
                  initialValue: userInfo && userInfo.ALMsgTemplateCode ? userInfo.ALMsgTemplateCode : undefined,
                  rules: [{ required: aliPayMsgFlag  === 'Y' ? true : false, message: '支付宝主体模板代码不能为空' }]
                })(
                  <Input placeholder="请输入" />
                )}
              </Form.Item>
            </Col>
          </Row>
          <Row>
          <Col span={6}>
              <Form.Item labelAlign='left' label="APP消息:" {...formItemInput}>
                {getFieldDecorator('appMsg', {
                  valuePropName: 'checked',
                  initialValue: userInfo && userInfo.appMsg ? (userInfo.appMsg === 'Y' ? true : false) : false,
                  rules: []
                })(
                  <Checkbox checkedChildren="是" unCheckedChildren="否"  onChange={this.onChangeFlag}/>
                )}
              </Form.Item>
            </Col>
            <Col span={9}>
              <Form.Item labelAlign='right' label="APP主体代码：" {...formItemInput}>
                {getFieldDecorator('AppMsgAppCode', {
                  initialValue: userInfo && userInfo.AppMsgAppCode ? userInfo.AppMsgAppCode : undefined,
                  rules: [{ required: appMsgFlag  === 'Y' ? true : false, message: 'APP主体代码不能为空' }]
                })(
                  <Input placeholder="请输入" />
                )}
              </Form.Item>
            </Col>
            <Col span={9}>
              <Form.Item labelAlign='right' label="APP主体模板代码：" {...formItemInput}>
                {getFieldDecorator('AppMsgTemplateCode', {
                  initialValue: userInfo && userInfo.AppMsgTemplateCode ? userInfo.AppMsgTemplateCode : undefined,
                  rules: [{ required: appMsgFlag  === 'Y' ? true : false, message: 'APP主体模板代码不能为空' }]
                })(
                  <Input placeholder="请输入" />
                )}
              </Form.Item>
            </Col>
          </Row>
        </Form>
      </Modal>
    )
  }
};
const NewBusinessAuthorizationForm = Form.create()(NewBusinessAuthorization);
export default NewBusinessAuthorizationForm;