<template>
  <el-card class="card-box notice-panel" v-loading="loading">
    <div slot="header">
      <span>公告</span>
      <el-button 
        style="float: right;" 
        size="mini" 
        type="text" 
        v-has-permi="['bst:notice:list']"
        @click="handleClickAllNotice">
        查看全部 <i class="el-icon-d-arrow-right"/>
      </el-button>
    </div>
    <div class="notice-list">
      <div v-for="item in noticeList" :key="item.id" class="notice-item" @click="handleView(item)">
        <div class="notice-content">
          <div class="notice-main">
            <div class="notice-title-row">
              <h3 class="notice-title" :class="{ 'is-top': item.top }">{{ item.title | dv }}</h3>
              <div class="notice-badges">
                <el-tag v-if="item.top" size="mini" type="danger" class="badge-item">置顶</el-tag>
                <dict-tag :options="dict.type.notice_level" :value="item.level" size="mini" class="badge-item"/>
              </div>
            </div>
            <div class="notice-meta">
              <span class="meta-item">
                <i class="el-icon-user"></i>
                {{ item.userName | dv }}
              </span>
              <span class="meta-item">
                <i class="el-icon-time"></i>
                {{ item.createTime | dv }}
              </span>
            </div>
          </div>
        </div>
      </div>
    </div>
    <el-empty v-if="noticeList.length === 0" description="暂无公告" />

    <!-- 查看详情弹窗 -->
    <el-dialog
      :visible.sync="dialogVisible"
      width="720px"
      append-to-body
      custom-class="notice-dialog"
    >
      <template #title>
        <div class="dialog-header">
          <h2 class="header-title">{{ detail.title | dv }}</h2>
          <div class="header-meta">
            <div class="meta-left">
              <el-tag v-if="detail.top" size="mini" type="danger">置顶</el-tag>
              <dict-tag :options="dict.type.notice_level" :value="detail.level" size="mini"/>
            </div>
            <div class="meta-right">
              <span class="meta-item">
                <i class="el-icon-user"></i>
                {{ detail.userName | dv }}
              </span>
              <span class="meta-item">
                <i class="el-icon-time"></i>
                {{ detail.createTime | dv }}
              </span>
            </div>
          </div>
        </div>
      </template>
      <div v-loading="detailLoading" class="dialog-body">
        <div class="notice-detail">{{ detail.content | dv}}</div>
        <div v-if="detail.attaches != null && detail.attaches.length > 0" class="notice-attachments">
          <div class="attachments-title">附件</div>
          <attach-list :file-list="detail.attaches" :column="4" :max-show="4" />
        </div>
      </div>
      

      <template #footer>
        <el-button @click="dialogVisible = false">关闭</el-button>
      </template>
    </el-dialog>
  </el-card>
</template>

<script>
import { listNotice, getNotice } from '@/api/bst/notice';
import AttachList from '@/components/AttachList/index.vue';

export default {
  name: 'NoticeBoard',
  components: { AttachList },
  dicts: ['notice_level'],
  data() {
    return {
      noticeList: [],
      loading: false,
      dialogVisible: false,
      detailLoading: false,
      detail: {},
      queryParams: {
        pageNum: 1,
        pageSize: 2,
      },
    }
  },
  created() {
    this.getList();
  },
  methods: {
    handleClickAllNotice() {
      this.$router.push("/notice");
    },
    getList() {
      this.loading = true;
      listNotice(this.queryParams).then(response => {
        this.noticeList = response.rows;
      }).finally(() => {
        this.loading = false;
      });
    },
    handleView(row) {
      this.dialogVisible = true;
      this.detailLoading = true;
      getNotice(row.id).then(res => {
        this.detail = res.data;
      }).finally(() => {
        this.detailLoading = false;
      });
    }
  }
}
</script>

<style lang="scss" scoped>
.notice-panel {
  background: #fff;
  border-radius: 6px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
}

.notice-list {
  .notice-item {
    padding: 12px 16px;
    cursor: pointer;
    border-bottom: 1px solid #f0f2f5;
    transition: all 0.3s ease;
    
    &:last-child {
      border-bottom: none;
    }
    
    &:hover {
      background-color: #fafafa;
      transform: translateX(4px);
    }

    .notice-content {
      display: flex;
    }

    .notice-main {
      flex: 1;
      min-width: 0;
    }

    .notice-title-row {
      display: flex;
      align-items: center;
      gap: 12px;
      margin-bottom: 4px;
    }

    .notice-title {
      margin: 0;
      font-size: 14px;
      font-weight: 500;
      color: #2c3e50;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      flex: 1;
      min-width: 0;

      &.is-top {
        color: #f56c6c;
      }
    }

    .notice-badges {
      flex-shrink: 0;
      display: flex;
      gap: 6px;

      .badge-item {
        border-radius: 3px;
      }
    }

    .notice-meta {
      display: flex;
      align-items: center;
      gap: 12px;
      color: #909399;
      font-size: 12px;

      .meta-item {
        display: flex;
        align-items: center;
        gap: 3px;

        i {
          font-size: 13px;
        }
      }
    }
  }
}

:deep(.notice-dialog) {
  .el-dialog__header {
    padding: 0;
    margin: 0;
  }

  .el-dialog__body {
    padding: 0;
  }
}

:deep(.el-dialog.notice-dialog > .el-dialog__body) {
  padding-top: 8px !important;
}

.dialog-header {
  background: #fff;

  .header-title {
    margin: 0 0 12px;
    font-size: 18px;
    font-weight: 600;
    color: #2c3e50;
    line-height: 1.4;
  }

  .header-meta {
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #909399;
    font-size: 12px;

    .meta-left {
      display: flex;
      gap: 6px;

      :deep(.el-tag) {
        border-radius: 3px;
        padding: 0 6px;
        height: 20px;
        line-height: 20px;
      }
    }

    .meta-right {
      display: flex;
      align-items: center;
      gap: 16px;

      .meta-item {
        display: flex;
        align-items: center;
        gap: 4px;

        i {
          font-size: 13px;
        }
      }
    }
  }
}

.dialog-body {

  .notice-detail {
    margin-top: -16px;
    line-height: 1.7;
    color: #2c3e50;
    font-size: 14px;
    background: #f8f9fa;
    padding: 16px 20px;
    border-radius: 6px;
    min-height: 100px;
    white-space: pre-wrap;
  }

  .notice-attachments {
    margin-top: 16px;

    .attachments-title {
      font-size: 14px;
      font-weight: 500;
      color: #2c3e50;
      margin-bottom: 12px;
      padding-left: 10px;
      border-left: 2px solid #409eff;
    }
  }
}
</style>