// 体检满意度调查
import React from 'react';
import { List, Radio, Button, Toast } from 'antd-mobile';
import './style/index.less';
import { $http } from "containers/config/https";
import {Util} from 'tools';

const RadioItem = Radio.RadioItem;

export default class SatisfactionSurvey extends React.Component {
  constructor({history}) {
    super();
    this.state = {
      healthTream: undefined,
      service: undefined,
      messageData: {}
    };
    this.history = history;
  }

  componentDidMount() {
    let messageData = this.props.location&&this.props.location.state ? this.props.location.state : {};
    if (messageData&&messageData.messageID) {
      this.setState({messageData})
    } else {
      let url = window.location.href.indexOf("?") !== -1 ? String(window.location.href).split("?")[1] : "";
      let messageValue = Util.interceptTheValueOfTheURL(Util.interceptTheValueOfTheURL(url, "params", "&", "="), "messageValue", "^", ":");
      let messageID = Util.interceptTheValueOfTheURL(Util.interceptTheValueOfTheURL(url, "params", "&", "="), "messageID", "^", ":");
      if (messageValue&&messageID) {
        let obj = {};
        obj.messageValue = messageValue;  
        obj.messageID = messageID;
        this.setState({messageData: obj})
      }
    }
  }

  // 提交
  handleSubmit = () => {
    let {healthTream, service, messageData} = this.state;
    if (healthTream&&service) {
      let data = {
        params: [{
          healthReocrdID: messageData.messageValue || "",
          healthTream,
          service,
          messageID: messageData.messageID || "",
        }]
      }
      $http.post("urlS", this, {
        code: "03070084",
        data: data,
        namespace: "HIS",
        success: function(res) {
          Toast.info(res.errorMessage, 3, () => {}, false)
          if (+res.errorCode === 0) {
            if (messageData.messageTypeCode) {
              this.history.goBack()
            } else {
              this.history.push("/0001/00000001")
            }
          }
        }
      });
    } else {
      Toast.info("请完善调查信息。", 3, () => {}, false)
    }
  }

  handleRadioChangeOne = val => {
    this.setState({healthTream: val})
  }

  handleRadioChangeTwo = val => {
    this.setState({service: val})
  }
  
  render() {
    let {healthTream, service} = this.state;
    const data = [
      { value: "Y", label: '是' },
      { value: "N", label: '否' },
    ];
    const data2 = [
      { value: "Y", label: '满意' },
      { value: "N", label: '不满意' },
    ];
    return (
      <div className="satisfaction-survey">
        <div className="ss-desc">
          <i className="iconfont icon-erduo"></i>
          <div>
            感谢您对海裕社康中心的支持！现邀请您针对本次体检的服务情况进行评价，我们将认真聆听您的声音，为您提供更好的服务。
          </div>
        </div>
        <List renderHeader={() => '1、您是否希望拥有个性化健康管理团队？'}>
          {data.map(i => (
            <RadioItem key={i.value} checked={healthTream === i.value} onChange={() => this.handleRadioChangeOne(i.value)}>
              {i.label}
            </RadioItem>
          ))}
        </List>
        <List renderHeader={() => '2、 您对我们的服务满意吗？'}>
          {data2.map(i => (
            <RadioItem key={i.value} checked={service === i.value} onChange={() => this.handleRadioChangeTwo(i.value)}>
              {i.label}
            </RadioItem>
          ))}
        </List>
        <div className="ss-btn">
          <Button type="primary" onClick={this.handleSubmit}>提交</Button>
        </div>
      </div>
    )
  }
}