<template>
  <el-dialog title="查看详情" :visible.sync="show" width="670px" :before-close="close">
    <div style="pading-left: 15px;" v-loading="loading">
      <div class="rpList">
        <div v-for="(item, idx) in replyList" :key="idx">
          <!-- "replyType": 0,  // 回复类型 （0：会员，1：商家） -->
          <i class="iconfont sign" :class="`${item.replyType ? 'icon-kefu1 warning-color' : 'icon-huiyuan-copy blue'}`"></i>
          <div class="head">
            <p class="name">{{ item.replyType ? item.clerkName : item.memberName }}</p>
            <p>{{ formatDateTimeByType(item.replyTime, 'yyyy-MM-dd-HH-mm') }}</p>
            <div class="del">
              <dm-delete @confirm="del(item)" tips="评论一旦删除，将无法在小程序端展示，请确认是否删除该条评论？" v-if="!['firstEvaluate', 'firstReply'].includes(item.evaluateReplyId)">
                <el-button type="text">删除</el-button>
              </dm-delete>
            </div>
          </div>
          <div class="content">
            <div class="text">{{ item.replyContent }}</div>
            <!-- <div class="imgl" v-if="item.imgList && item.imgList.length">
              <img v-for="(img, idx) in item.imgList" :key="idx" style="width: 52px; height: 52px;cursor: pointer;" title="点击查看大图" class="mr10" :src="img" preview-title-enable="true" preview-nav-enable="true" @click="showImage(img)" />
            </div> -->
          </div>
        </div>
      </div>
      <div class="empty" v-show="!replyList.length">暂无回复</div>
      <el-form :model="form" :rules="rules" ref="form" label-width="0" class="demo-ruleForm">
        <p class="mb5 regular-font-color">添加回复：</p>
        <el-form-item prop="content">
          <dm-input type="textarea" v-model="form.content" :rows="4" placeholder="请输入回复内容" :maxlength="200"></dm-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close">关 闭</el-button>
      <el-button type="primary" @click="submit('form')">提 交</el-button>
    </span>
    <vue-gic-img-preview :imgUrl="imgUrl" :imgShowFlag="imgShowFlag" @hideImage="hideImage"></vue-gic-img-preview>
  </el-dialog>
</template>
<script>
import { formatDateTimeByType } from '@/utils/index.js';
import { addEvaluateReply, getEvaluateReplyList, delEvaluateReply } from '@/service/api/evaluationApi.js';
export default {
  props: {
    show: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      form: {
        content: ''
      },
      shouldRefresh: false,
      loading: false,
      rules: {
        content: [{ required: true, message: '请输入回复内容', trigger: 'blur' }, { max: 200, message: '字数不能大于200', trigger: 'blur' }] // eslint-disable-line
      },
      srcList: [],
      replyList: [],
      imgShowFlag: false,
      imgUrl: ''
    };
  },
  watch: {
    show(val) {
      if (val) {
        // 追评商户和用户评论暂无图片
        // let arr = [];
        // this.replyList.forEach(v => {
        //   if (v.sign && v.imgList && v.imgList.length) {
        //     arr = arr.concat(v.imgList);
        //   }
        // });
        // this.srcList = arr;
        this.getEvaluateReplyList();
      }
    }
  },
  methods: {
    formatDateTimeByType,
    del({ evaluateReplyId }) {
      this.loading = true;
      delEvaluateReply({ evaluateReplyId }).then(res => {
        this.loading = false;
        if (res.errorCode === 0) {
          this.getEvaluateReplyList();
          this.$tips({ type: 'success', message: '删除成功' });
        } else {
          this.$tips({ type: 'warning', message: res.message || '删除失败' });
        }
      });
    },
    getEvaluateReplyList() {
      this.loading = true;
      getEvaluateReplyList({ evaluateId: this.id }).then(res => {
        this.loading = false;
        this.replyList = res.result || [];
      });
    },
    close() {
      if (this.shouldRefresh) {
        this.$emit('refresh');
      }
      this.$refs.form.resetFields();
      this.replyList = [];
      this.srcList = [];
      this.shouldRefresh = false;
      this.$emit('update:show', false);
    },
    showImage(src) {
      if (!src || src == '') {
        return false;
      }
      this.imgShowFlag = true;
      this.imgUrl = src;
    },
    hideImage(val) {
      this.imgShowFlag = val;
    },
    submit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          addEvaluateReply({ evaluateId: this.id, replyContent: this.form.content }).then(res => {
            if (res.errorCode === 0) {
              this.getEvaluateReplyList();
              this.form.content = '';
              this.shouldRefresh = true;
              this.$tips({ type: 'success', message: '回复成功' });
            } else {
              this.$tips({ type: 'warning', message: res.message || '回复失败' });
            }
          });
        } else {
          return false;
        }
      });
    }
  }
};
</script>

<style scoped lang="scss">
.rpList {
  max-height: 400px;
  overflow-y: auto;
  > div {
    position: relative;
    padding-left: 40px;
    padding-bottom: 20px;
    i.sign {
      position: absolute;
      left: 0;
      top: 3px;
      width: 32px;
      height: 32px;
      line-height: 32px;
      text-align: center;
      font-size: 18px;
      background: rgba(235, 236, 240, 1);
      border-radius: 50%;
    }
    &:after {
      top: 40px;
      position: absolute;
      width: 1px;
      background: #e4e7ed;
      left: 15px;
      content: '';
      height: calc(100% - 42px);
    }
  }
  .head {
    overflow: hidden;
    position: relative;
    .del {
      // float: right;
      position: absolute;
      right: 0px;
      top: 0px;
    }
    p.name {
      color: rgba(48, 49, 51, 1);
      line-height: 20px;
      & + p {
        color: rgba(96, 98, 102, 1);
        line-height: 18px;
      }
    }
  }
  .content {
    margin-top: 10px;
    padding: 12px 18px;
    background: #f5f7fa;
    .text {
      width: 532px;
      color: rgba(48, 49, 51, 1);
      line-height: 20px;
    }
    .imgl {
      margin-top: 7px;
    }
  }
}
.demo-ruleForm {
  margin-top: 25px;
  padding-left: 40px;
}
.empty {
  text-align: center;
  color: #909399;
  height: 80px;
  line-height: 80px;
}
</style>
