<template>
  <el-dialog
    class="notice-detail-dialog"
    :visible.sync="dialogVisible"
    :title="title"
    width="700px"
    append-to-body
    :close-on-click-modal="false"
    @closed="handleClosed"
  >
    <div class="notice-detail-dialog__body">
      <div v-if="publishedTime" class="notice-detail-dialog__meta">
        <span class="notice-detail-dialog__time">{{ publishedTime }}</span>
      </div>

      <div class="notice-detail-dialog__content">
        {{ content || '-' }}
      </div>

      <div v-if="filePath" class="notice-detail-dialog__attachment">
        <div class="notice-detail-dialog__attachment-label">{{ $t('trials:notice:table:appendix') }}</div>
        <div class="notice-detail-dialog__attachment-card">
          <i class="el-icon-paperclip" />
          <el-button size="small" type="text" @click="$emit('open-file', filePath)">
            {{ fileName}}
          </el-button>
        </div>
      </div>
    </div>

    <span v-if="showConfirmButton" slot="footer" class="notice-detail-dialog__footer">
      <el-button size="small" @click="handleClose">{{ $t('common:button:close') }}</el-button>
      <el-button
        size="small"
        type="primary"
        :loading="confirmLoading"
        @click="$emit('confirm')"
      >
        {{ $t('common:button:confirm') }}
      </el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  name: 'NoticeDetailDialog',
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
      default: '',
    },
    content: {
      type: String,
      default: '',
    },
    publishedTime: {
      type: String,
      default: '',
    },
    fileName: {
      type: String,
      default: '',
    },
    filePath: {
      type: String,
      default: '',
    },
    showConfirmButton: {
      type: Boolean,
      default: false,
    },
    confirmLoading: {
      type: Boolean,
      default: false,
    },
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible
      },
      set(value) {
        this.$emit('update:visible', value)
      },
    },
  },
  methods: {
    handleClose() {
      this.dialogVisible = false
    },
    handleClosed() {
      this.$emit('closed')
    },
  },
}
</script>

<style lang="scss" scoped>
.notice-detail-dialog {
  ::v-deep .el-dialog__header {
    padding: 18px 20px 10px;
    border-bottom: 1px solid #ebeef5;
  }

  ::v-deep .el-dialog__body {
    padding: 0;
  }

  ::v-deep .el-dialog__footer {
    padding: 12px 20px 18px;
    border-top: 1px solid #ebeef5;
  }
}

.notice-detail-dialog__body {
  max-height: 70vh;
  overflow-y: auto;
  padding: 18px 20px;
}

.notice-detail-dialog__meta {
  margin-bottom: 12px;
  color: #909399;
  font-size: 13px;
}

.notice-detail-dialog__content {
  padding: 14px 16px;
  border: 1px solid #ebeef5;
  border-radius: 8px;
  background-color: #f8f9fb;
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.7;
  color: #303133;
}

.notice-detail-dialog__attachment {
  margin-top: 16px;
}

.notice-detail-dialog__attachment-label {
  margin-bottom: 8px;
  color: #606266;
  font-size: 13px;
}

.notice-detail-dialog__attachment-card {
  display: flex;
  align-items: center;
  padding: 12px 14px;
  border: 1px solid #ebeef5;
  border-radius: 8px;
  background-color: #fff;
}

.notice-detail-dialog__attachment-card .el-icon-paperclip {
  margin-right: 8px;
  color: #409eff;
  font-size: 16px;
}

.notice-detail-dialog__attachment-name {
  flex: 1;
  min-width: 0;
  margin-right: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: #303133;
}
</style>
