<template>
  <div v-if="enabled" class="section-container">
    <div class="section-title">
      <span>部门处理意见 <span class="en-sub">· Department Opinions</span></span>
      <el-button size="mini" type="text" icon="el-icon-refresh" @click="$emit('refresh')" title="刷新部门处理意见"></el-button>
    </div>
    <div v-if="taskList.length > 0" class="card-grid">
      <div v-for="item in taskList" :key="item.taskId" class="opinion-card">
        <div class="opinion-card-header">
          <div class="opinion-dept">
            <span class="opinion-dept-icon">◆</span>
            <span>{{ getDeptName(item.deptId) }}</span>
          </div>
          <el-tag v-if="item.rejectMark === 1" type="danger" size="mini">已驳回</el-tag>
          <el-tag v-else-if="item.rejectMark === 2" type="info" size="mini">已隐藏</el-tag>
          <el-tag v-else-if="item.taskStatus === 0" type="warning" size="mini">待填写</el-tag>
          <el-tag v-else-if="item.taskStatus === 1" type="success" size="mini">已完成</el-tag>
        </div>
        <div class="opinion-card-body">
          <div v-if="item.deptOpinion" class="opinion-content" v-html="item.deptOpinion"></div>
          <div v-else class="opinion-empty">— No opinion yet · 暂无意见 —</div>
          <div v-if="item.fillFile" class="opinion-file">
            <FileList :ossIds="item.fillFile" />
          </div>
        </div>
        <div class="opinion-card-footer">
          <span v-if="item.fillNo" class="opinion-footer-item">{{ item.fillNo }}</span>
          <span v-if="item.fillTime" class="opinion-footer-item">{{ parseTime(item.fillTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </div>
      </div>
    </div>
    <div v-else class="empty-data">暂无部门处理意见</div>
  </div>
</template>

<script>
import FileList from "@/components/FileList/index.vue";

export default {
  name: 'DepartmentOpinionSection',
  components: { FileList },
  props: {
    enabled: {
      type: Boolean,
      default: true
    },
    taskList: {
      type: Array,
      default: () => []
    }
  },
  methods: {
    getDeptName(deptId) {
      const map = { 1: '生产部', 2: '质量部', 3: '销售部' };
      return map[deptId] || '部门' + deptId;
    },
    parseTime(time, option) {
      if (!time) return '-';
      const date = new Date(time);
      if (isNaN(date.getTime())) return time;
      const formatStr = option || '{y}-{m}-{d} {h}:{i}:{s}';
      const year = date.getFullYear();
      const month = date.getMonth() + 1;
      const day = date.getDate();
      const hours = date.getHours();
      const minutes = date.getMinutes();
      const seconds = date.getSeconds();
      return formatStr.replace('{y}', year)
                      .replace('{m}', month < 10 ? '0' + month : month)
                      .replace('{d}', day < 10 ? '0' + day : day)
                      .replace('{h}', hours < 10 ? '0' + hours : hours)
                      .replace('{i}', minutes < 10 ? '0' + minutes : minutes)
                      .replace('{s}', seconds < 10 ? '0' + seconds : seconds);
    }
  }
}
</script>

<style scoped>
.section-title {
  font-family: 'Georgia', 'Times New Roman', 'Noto Serif SC', 'SimSun', serif;
  width: 100%;
  font-size: 15px;
  font-weight: 700;
  color: #1a1a1a;
  margin: 32px 0 16px 0;
  padding: 0 0 10px 0;
  border-bottom: 1px solid #d4d0c8;
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 10px;
  letter-spacing: 0.3px;
}
.section-title:first-child {
  margin-top: 0;
}
.section-title .en-sub {
  font-size: 11px;
  font-weight: 400;
  color: #8c8c8c;
  letter-spacing: 0.5px;
  font-family: 'Georgia', 'Times New Roman', serif;
  font-style: italic;
}
.section-title i {
  font-size: 16px;
  color: #1a3c6e;
}

/* ===== 意见卡片（正式文档风格） ===== */
.card-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
}

.opinion-card {
  flex: 0 0 calc((100% - 14px) / 2);
  background: #ffffff;
  border: 1px solid #e8e4de;
  border-radius: 2px;
  padding: 14px 16px 12px;
  display: flex;
  flex-direction: column;
}

.opinion-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
  padding-bottom: 8px;
  border-bottom: 1px dashed #e0dcd6;
}

.opinion-dept {
  display: flex;
  align-items: center;
  gap: 6px;
  font-family: 'Georgia', 'Times New Roman', 'Noto Serif SC', 'SimSun', serif;
  font-size: 14px;
  font-weight: 700;
  color: #1a3c6e;
  letter-spacing: 0.3px;
}

.opinion-dept-icon {
  font-size: 10px;
  color: #1a3c6e;
}

.opinion-card-body {
  flex: 1;
}

.opinion-content {
  font-size: 13px;
  color: #3a3a3a;
  line-height: 1.7;
  word-break: break-all;
  max-height: 100px;
  overflow-y: auto;
}

.opinion-content /deep/ p {
  margin: 0;
}

.opinion-empty {
  color: #bab5ae;
  font-size: 12px;
  font-style: italic;
  font-family: 'Georgia', 'Times New Roman', serif;
}

.opinion-file {
  margin-top: 8px;
}

.opinion-card-footer {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px dashed #e0dcd6;
}

.opinion-footer-item {
  font-family: 'Georgia', 'Times New Roman', serif;
  font-size: 11px;
  color: #8c8c8c;
}

.empty-data {
  color: #8c8c8c;
  font-size: 13px;
  padding: 8px 0;
  font-style: italic;
}
</style>
