<template>
  <div v-loading="loading" class="chat-wrapper">
    <div class="chat-content">
      <div v-for="(record,index) in recordContent" :key="index">
        <div :class="[!record.IsCurrentUser?'word':'word-my']">
          <img v-if="record.UserTypeEnum*1 === 8" class="avatar" :src="piAvatar" alt="PI">
          <img v-else class="avatar" :src="srAvatar" alt="SR">
          <div class="info">
            <p class="user-info">
              <span style="font-weight:700;">{{ record.CreateUserName }} </span>
              <span>({{ record.CreateTime }}) </span>
            </p>
            <div class="info-content">
              <template v-if="record.UserTypeEnum === 8 && !record.ReplyContent">
                <div>审核结果：{{ $fd('PIAuditState',record.PIAuditState) }}</div>
                <div v-if="record.NotAgreeReason">不认可原因：{{ record.NotAgreeReason }}</div>
                <div v-if="record.PIAuditNote">备注：{{ record.PIAuditNote }}</div>
                <div v-if="record.PIAuditImagePathList && record.PIAuditImagePathList.length > 0" style="margin-top:5px;">
                  相关截图如下：
                  <ol>
                    <li v-for="filePath in record.PIAuditImagePathList" :key="filePath" style="list-style: none;width: 50px;">
                      <viewer :images="record.PIAuditImagePathList">
                        <img
                          v-if="filePath"
                          style="object-fit: cover !important;width: 100%;border-radius:0 !important"
                          :src="OSSclientConfig.basePath + filePath"
                          alt=""
                          crossorigin="anonymous"
                        >
                      </viewer>
                    </li>
                  </ol>
                </div>
              </template>
              <template v-else-if="record.UserTypeEnum === 8 && record.ReplyContent">
                <div>{{ record.ReplyContent }}</div>
              </template>
              <template v-else-if="record.UserTypeEnum !== 8 && record.ReplyContent">
                <div>{{ record.ReplyContent }}</div>
              </template>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div
      class="chat-message"
    >
      <div class="message">
        <el-input
          v-model="newMessage"
          type="textarea"
          :rows="2"
        />
      </div>
      <div class="function">
        <el-button
          type="primary"
          :disabled="newMessage===''"
          @click="handleReply"
        >
          <!-- 回复 -->
          {{ $t('trials:medicalFeedback:table:reply') }}
        </el-button>
      </div>

    </div>
    <el-dialog
      v-if="previewDialog"
      append-to-body
      :close-on-click-modal="false"
      :visible.sync="previewDialog"
      width="600px"
    >
      <!-- <img width="100%" :src="imagePath" alt="图片未找到"> -->
      <el-image :src="imagePath" width="100%">
        <div slot="placeholder" class="image-slot">
          <!-- 加载中 -->
          {{ $t('trials:medicalFeedback:message:loading') }}<span class="dot">...</span>
        </div>
      </el-image>

    </el-dialog>
  </div>
</template>
<script>
import { getPIAuditDialogList, pIAuditTaskReply } from '@/api/reading'
import piAvatar from '@/assets/MIM.png'
import srAvatar from '@/assets/IR.png'
export default {
  name: 'ChatForm',
  props: {
    rowData: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      recordContent: [],
      newMessage: '',
      loading: false,
      piAvatar,
      srAvatar,
      userTypeEnumInt: zzSessionStorage.getItem('userTypeEnumInt') * 1,
      currentUserId: zzSessionStorage.getItem('userId'),
      imagePath: '',
      previewDialog: false
    }
  },
  mounted() {
    this.getMessageList()
  },
  methods: {
    // 初始化聊天信息
    getMessageList() {
      this.loading = true
      var param = {
        visitTaskId: this.rowData.Id
      }
      getPIAuditDialogList(param).then(res => {
        this.recordContent = res.Result
        this.setScrollHeight()
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    handleReply() {
      if (!this.newMessage) return
      this.loading = true
      var params = {
        visitTaskId: this.rowData.Id,
        replyContent: this.newMessage
      }
      pIAuditTaskReply(params).then(res => {
        this.loading = false
        if (res.IsSuccess) {
          this.newMessage = ''
          this.getMessageList()
        }
      }).catch(() => { this.loading = false })
    },
    setScrollHeight() {
      setTimeout(() => {
        var container = document.querySelectorAll('.chat-content')[0]
        container.scrollTop = container.scrollHeight
      }, 200)
    },
    previewImage(path) {
      this.imagePath = path
      this.previewDialog = true
    }
  }

}

</script>
<style lang="scss" scoped>
.chat-wrapper{
::-webkit-scrollbar {
    width: 7px;
    height: 7px;
  }
  ::-webkit-scrollbar-thumb {
    border-radius: 10px;
    background: #d0d0d0;
  }
  .chat-content{
    width:100%;
    padding: 20px;
    height: 500px;
    overflow-y: auto;
    .word{
      display: flex;
      margin-bottom: 20px;
      .avatar{
        width:40px;
        height: 40px;
        border-radius: 50%;
      }
      .info{
        margin-left: 10px;
        .user-info{
          font-size: 12px;
          color:rgba(51,51,51,0.8);
          margin:0;
          height: 20px;
          line-height: 20px;
          margin-top: -5px;
        }
        .info-content{
          padding: 10px;
          font-size: 14px;
          background-color: #ebeef5;
          position: relative;
          margin-top: 8px;
        }
        .info-content::before{
          position: absolute;
          left: -8px;
          top: 8px;
          content: '';
          border-right: 10px solid #ebeef5;
          border-top: 8px solid transparent;
          border-bottom: 8px solid transparent;
        }
      }
    }
    .word-my{
      display: flex;
      justify-content: flex-start;
      flex-direction: row-reverse;
      margin-bottom: 20px;
      .avatar{
        width: 40px;
        height: 40px;
        border-radius: 50%;
      }
      .info{
        width: 90%;
        margin-left: 10px;
        text-align: right;
        .user-info{
          font-size: 12px;
          color: rgba(51,51,51,0.8);
          margin: 0;
          height: 20px;
          line-height: 20px;
          margin-top: -5px;
          margin-right: 10px;
        }
        .info-content{
          position: relative;
          max-width: 70%;
          padding: 10px;
          font-size: 14px;
          float: right;
          margin-right: 10px;
          margin-top: 8px;
          background-color: #7574d9;
          color: #fff;
          text-align: left;
        }
        .info-content::after{
          position: absolute;
          right: -8px;
          top: 8px;
          content: '';
          border-left: 10px solid #7574d9;
          border-top: 8px solid transparent;
          border-bottom: 8px solid transparent;
        }
      }
    }
  }
  .chat-message{
    padding: 0 50px;
    .function{
      margin-top: 10px;
      text-align: right;
    }
  }
  ol{
    margin: 5px;
    padding: 0px 10px;
    counter-reset: num;
    li{
      counter-increment: num;
      // list-style:none;
    }
  }
  ol.example{counter-reset: num;}
  ol.example li{counter-increment: num; list-style:none;}
}
</style>
