import constant from '../../../../../utils/constant';
import {
  evaluation
} from '../../../../../../../api/consult'
import {
  throttle
} from "../../../../../../../utils/util"
let app=getApp();
// eslint-disable-next-line no-undef
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    display: {
      type: Boolean,
      value: false,
      observer(newVal) {
          console.log(newVal);
        this.setData({
          displayTag: newVal,
        });
      },
    },
 
    doctor_id: {
      type: String,
      value: '',
      observer(newVal) {
        this.setData({
          doctor_id: newVal,
        });
      },
    },
    commentDetail: {
      type: Object,
      value: null,
      observer(newVal) {
        if (newVal) {
          this.setData({
            commentDetail: newVal,
            reply_quality: newVal.reply_quality,
            service_attitude: newVal.service_attitude,
            reply_progress: newVal.reply_progress,
            content: newVal.content?newVal.content:'感谢您的服务',
            score: newVal.avg_score,
            is_evaluation: newVal.is_evaluation,
            order_inquiry_id: newVal.order_inquiry_id
          });
          if(!newVal.is_evaluation){
            this.setData({
              reply_quality: 5,
              service_attitude: 5,
              reply_progress: 5,
              score:5
            })
          }
        }


      },
    },
    doctor_info: {
      type: Object,
      value: {},
      observer(newVal) {
        this.setData({
          doctor_info: newVal,
        });
      },
    }
  },
  lifetimes: {
    attached: function () {

      // 在组件实例进入页面节点树时执行
    },
  },
  pageLifetimes: {
    show: function() {
      this.setData({
        img_host:app.hostConfig().imghost
      });
     
    },
    hide: function() {

      // 页面被隐藏
    },
    resize: function(size) {
      // 页面尺寸变化
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    img_host:'https://oss.prod.applets.igandanyiyuan.com/applet/patient/static',
    displayTag: false,
    doctor_info: null,
    commentDetail: null,
    doctor_id: '',
    is_evaluation: false,
    message: '',
    scoreList: [1, 2, 3, 4, 5],
    score: 0,
    comment: '',
    order_inquiry_id: '',
    reply_quality: 0,
    service_attitude: 0,
    reply_progress: 0,
    content: '感谢您的服务'
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onChangeContent(event) {
      this.setData({
        content: event.detail
      })
    },
    handleEvaluation: throttle(function () {
      let {
        order_inquiry_id,
        doctor_info,
        reply_quality,
        service_attitude,
        reply_progress,
        content
      } = this.data;
      if (reply_quality == '') {
        wx.showToast({
          title: '请评论回复质量',
          icon: "none"
        });
        return false;
      };
      if (service_attitude == '') {
        wx.showToast({
          title: '请评论服务态度',
          icon: "none"
        });
        return false;
      };
      if (content == '') {
        wx.showToast({
          title: '请填写您对医生的印象',
          icon: "none"
        });
        return false;
      };
      if (reply_progress == '') {
        wx.showToast({
          title: '请评论回复速度',
          icon: "none"
        });
        return false;
      };
      evaluation({
        order_inquiry_id,
        doctor_id:doctor_info.doctor_id,
        reply_quality,
        service_attitude,
        reply_progress,
        content,
      }).then(data => {
        this.setData({
          displayTag: false
        });
        this.triggerEvent("freshRate",order_inquiry_id);
        this.triggerEvent('close', {
          score: Number(this.data.score)
        });
        wx.showToast({
          title: '评价成功',
          icon: "none"
        })
      })
    }),
    handleClose() {
      this.triggerEvent('close', {
        score: this.data.is_evaluation ? this.data.score : 0
      });
    },
    onChange(event) {
      this.setData({
        [event.target.dataset.id]: event.detail
      });
      let {
        reply_quality,
        service_attitude,
        reply_progress
      } = this.data;
      if (reply_quality && service_attitude && reply_progress) {
        let score = (reply_quality * 0.4) + (service_attitude * 0.3) + (reply_progress * 0.3);
        this.setData({
          score: Math.floor(score)
        })
      }
    },
    handleScore(e) {
      let {
        score
      } = e.currentTarget.dataset;
      if (score === this.data.score) {
        score = 0;
      }
      this.setData({
        score,
      });
    },
    bindTextAreaInput(e) {
      this.setData({
        comment: e.detail.value,
      });
    },
    sendMessage() {
      const {
        BUSINESS_ID_TEXT,
        STRING_TEXT,
        FEAT_NATIVE_CODE
      } = constant;
      this.triggerEvent('sendCustomMessage', {
        payload: {
          // data 字段作为表示，可以自定义
          data: JSON.stringify({
            businessID: BUSINESS_ID_TEXT.EVALUATION,
            version: FEAT_NATIVE_CODE.NATIVE_VERSION,
            score: this.data.score,
            comment: this.data.comment,
          }),
          description: STRING_TEXT.TYPETEXT, // 获取骰子点数
          extension: STRING_TEXT.TYPETEXT,
        },
      });

      this.setData({
        score: 0,
        comment: '',
      });
      this.handleClose();
    },
  },

});