<template>
    <el-form :class="['reply-item', replyItem.parentId == 0 && 'no-b-l', isLastItem && 'no-p-b']">
        <div class="reply-item__content">
            <el-form-item>
                <template #label>{{ replyItem.createName }}<span>:</span>
                    <template v-if="replyItem.replyId"><span>回复@</span>{{ replyItem.replyName }}<span>:</span></template>
                </template>
                {{ replyItem.messageContent }}
            </el-form-item>
            <el-form-item class="reply-date-item" :label="formatDate(replyItem.createTime)"></el-form-item>
        </div>

        <div class="reply-item__button-group">
            <el-tooltip content="删除" :show-after="delay" placement="top">
                <el-button v-if="replyItem.createId == loginUser" icon="delete" circle @click="reply_del"></el-button>
            </el-tooltip>
            <el-tooltip content="留言" :show-after="delay" placement="top">
                <el-button circle @click="reply_add">
                    <template #icon><tjm-icon-mdi-comment-text-outline /></template>
                </el-button>
            </el-tooltip>
        </div>
    </el-form>

    <reply-detail v-if="dialog" ref="replyDetail" :refId="replyItem.refId" :refType="replyItem.refType" @success="$emit('success')" @closed="dialog = false"></reply-detail>
</template>

<script>
import moment from "moment";
import API from "@/api/policy/message";
import { useUserStore } from "@/store/user";

import replyDetail from "@/components/ReplyCard/dialog.vue";

export default {
    components: {
        replyDetail
    },
    props: {
        replyItem: { type: Object, default: () => {} },
        isLastItem: { type: Boolean, default: false }
    },

    data() {
        return {
            loginUser: useUserStore().userInfo.id,
            delay: 800,

            dialog: false
        }
    },

    methods: {
        formatDate(value) {
            return value && moment(value).format("YY-MM-DD HH:mm") || "";
        },

        reply_add() {
            this.dialog = true;
            nextTick(() => this.$refs.replyDetail.open().setData(this.replyItem));
        },

        reply_del() {
            ElMessageBox.confirm("是否确认删除?", "删除警告", {
                type: "warning",
                confirmButtonText: "确定",
                cancelButtonText: "取消"
            }).then(() => {
                API.del({ ids: this.replyItem.id }).then(res => {
                    if (res.code == 200) {
                        ElMessage.success("操作成功");
                        if (this.replyItem.parentId == 0) this.$emit("closeDialog");
                        else this.$emit("success");
                    } else ElMessage.error(res.msg);
                });
            }).catch(() => ElMessage.success("已取消"));
        }
    }
}
</script>

<style lang="scss" scoped>
.text-reverse {
  display: flex;
  flex-direction: row-reverse;

  :deep([class*='el-icon'] + span) {
    margin-left: 0;
    margin-right: 6px;
    font-size: 13px;
  }
}

.reply-item {
  display: flex;
  align-items: center;
  padding-left: 10px;
  padding-bottom: 10px;
  border-left: 1px solid var(--el-border-color-lighter);

  .reply-item__content {
    flex: 1;

    .el-form-item {
      margin-bottom: 0;

      :deep(.el-form-item__label) {
        height: fit-content;
        padding-right: 6px;
        line-height: 22px;
        font-size: 13px;
        color: var(--el-color-primary);

        span {
          color: var(--el-text-color-regular);
        }
      }

      :deep(.el-form-item__content) {
        height: fit-content;
        line-height: 22px;
        font-size: 13px;
        color: var(--el-text-color-regular);
      }
    }

    .reply-date-item :deep(.el-form-item__label) {
      color: var(--el-text-color-placeholder);
    }
  }

  .reply-item__button-group {
    display: none;
    margin-left: 10px;

    .el-button {
      width: 20px;
      height: 20px;
      padding: 0;
      background-color: transparent;
      border: none;

      &:hover {
        background-color: var(--el-color-primary-light-8);
      }
    }
  }

  &:hover .reply-item__button-group {
    display: inline-flex;
  }
}

.no-b-l {
  border-left: none;
}

.no-p-b {
  margin-bottom: 10px;
  padding-bottom: 0;
}
</style>