// 一键呼叫
import React from 'react';
import { Card } from 'antd-mobile';
import './style/index.less';
import store from "store";

export default class OneTouchCall extends React.Component {
  constructor({history}) {
    super();
    let userData = localStorage.getItem("userData") ? JSON.parse(localStorage.getItem("userData")) : {};
    this.state = {
      userData,
      userInfo: {},
      inputValue: undefined,
      cardData: {}
    };
   
    this.history = history;
  }
  componentDidMount() {
    let userInfo = this.props.location&&this.props.location.state ? this.props.location.state : {};
    if (userInfo&&userInfo.signDoctorTypeCode) {
      this.getCardData(userInfo);
      store.dispatch({
        type: "OTCItem",
        value: {...userInfo}
      });
    } else {
      let OTCItem = store.getState().appointRegRedux&&store.getState().appointRegRedux.OTCItem ? store.getState().appointRegRedux.OTCItem : {};
      if (OTCItem&&OTCItem.signDoctorTypeCode) {
        this.getCardData(OTCItem, "goback");
      } else {
        this.history.goBack()
      }
    }
  }

  // 获取未读数量
  // getUnreadQuantity = (userInfo) => {
  //   let p = new Promise((resolve, reject) => {
  //     let {userData} = this.state;
  //     let data = {
  //       params: [{
  //         signRecordID: userInfo.signRecordID || '',
  //         frUser: userInfo.imUserID || '',
  //         toUser: userData.imUserID || '',
  //       }]
  //     }
  //     $http.post("urlS", this, {
  //       code: "03070087",
  //       data: data,
  //       namespace: "HIS",
  //       success: function(res) {
  //         if (+res.errorCode === 0) {
  //           resolve(res.result)
  //         } else {
  //           Toast.info(res.errorMessage)
  //         }
  //       }
  //     });
  //   });
  //   p.then((result) => {
  //     return result.count
  //   })
  // }
  
  // 获取card数据
  getCardData = (userInfo, type) => {
    if (userInfo&&userInfo.descripts) {
      document.title =  "一键呼叫" + userInfo.descripts;
    }
    let cardData = {}
    if (userInfo&&userInfo.signDoctorTypeCode === "01") {
      cardData.headTitle = "全科医生";
      cardData.data = [
        {
          ...userInfo,
          icon: "icon-consult",
          menuTitle: "咨询",
          locDesc: "",
          msgCount: type === "goback" ? 0 : userInfo.msgCount
        },
        {
          icon: "icon-order",
          menuTitle: "预约挂号",
          locDesc: ""
        }
      ]
    } else if (userInfo&&userInfo.signDoctorTypeCode === "02") {
      cardData.headTitle = "专科医生";
      cardData.data = []
      if (userInfo&&userInfo.data instanceof Array) {
        cardData.data = userInfo.data;
        for(var i = 0; i < cardData.data.length; i++) {
          cardData.data[i].msgCount = type === "goback" ? 0 : cardData.data[i].msgCount;
        }
      } else {
        cardData.data.push(userInfo);
        cardData.data[0].msgCount = type === "goback" ? 0 : userInfo.msgCount;
        cardData.data[0].menuTitle = userInfo.doctorName
      }
    } else if (userInfo&&userInfo.signDoctorTypeCode === "03") {
      cardData.headTitle = "护理";
      cardData.data = [
        {
          ...userInfo,
          icon: "icon-consult",
          menuTitle: "咨询",
          locDesc: "",
          msgCount: type === "goback" ? 0 : userInfo.msgCount
        },
        {
          icon: "icon-huli",
          menuTitle: "护理平台",
          locDesc: ""
        }
      ]
    } else if (userInfo&&userInfo.signDoctorTypeCode === "04") {
      cardData.headTitle = "社工";
      cardData.data = [
        {
          ...userInfo,
          icon: "icon-consult",
          menuTitle: "咨询",
          locDesc: "",
          msgCount: type === "goback" ? 0 : userInfo.msgCount
        },
        {
          icon: "icon-contact",
          menuTitle: userInfo&&userInfo.mobile ? userInfo.mobile : "",
          locDesc: "",
          iphone: true
        }
      ]
    }
    this.setState({cardData})
  }

  // 跳转到聊天室界面
  handleGotoChatRoom = (item) => {
    console.log(item)
    if (item&&item.signDoctorTypeCode) {
      this.history.push({
        pathname: "/0003/00030003",
        state: {...item}
      })
    }
  }

  handleContact = e => {
    e.stopPropagation();
  }

  render() {
    let {cardData} = this.state;
    return (
      <div className="one-touch-call">
        <Card>
          <Card.Header
            title={cardData&&cardData.headTitle ? cardData.headTitle : ""}
          />
          <Card.Body>
            <ul className="c-body-list">
              {cardData.data&&cardData.data.length > 0&&cardData.data.map((item, index) => {
                return (
                  <li key={index} onClick={() => this.handleGotoChatRoom(item)} className={!item.icon&&index%2 !== 1 ? "c-body-icon-list border-right-before" : (!item.icon ? "c-body-icon-list" : (index%2 !== 1 ? "border-right-before" : ""))}>
                    {item.icon ? <i className={"iconfont "+item.icon}></i> : ""}
                    <div>
                      {item.iphone ? <a href={`tel:${item.menuTitle}`} onClick={this.handleContact}>{item.menuTitle}</a> : item.menuTitle}
                      {item.locDesc ? <span>{item.locDesc}</span> : ""}
                    </div>
                    {item.msgCount ? <span className="unread-message">{item.msgCount}</span> : ""}
                  </li>
                )
              })}
            </ul>
          </Card.Body>
        </Card>
      </div>
    )
  }
}