<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">
          <template v-if="isJsonOpinion(item.deptOpinion)">
            <DeptOpinionPreview :deptId="item.deptId" :jsonData="item.deptOpinion" />
          </template>
          <template v-else>
            <div v-if="item.deptOpinion" class="opinion-content" v-html="item.deptOpinion"></div>
            <div v-else class="opinion-empty">— No opinion yet · 暂无意见 —</div>
          </template>
          <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";
import DeptOpinionPreview from "./DeptOpinionPreview.vue";

export default {
  name: 'DepartmentOpinionSection',
  components: { FileList, DeptOpinionPreview },
  props: {
    enabled: {
      type: Boolean,
      default: true
    },
    taskList: {
      type: Array,
      default: () => []
    }
  },
  methods: {
    isJsonOpinion(str) {
      if (!str) return false;
      try {
        const obj = JSON.parse(str);
        return obj && typeof obj === 'object' && !Array.isArray(obj);
      } catch (e) {
        return false;
      }
    },
    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 {
  width: 100%;
  font-size: 13px;
  font-weight: 600;
  color: #1e293b;
  margin: 20px 0 10px 0;
  padding: 0 0 8px 0;
  border-bottom: 1px solid #cbd5e1;
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 8px;
  letter-spacing: 0.2px;
}
.section-title:first-child {
  margin-top: 0;
}
.section-title .en-sub {
  font-size: 11px;
  font-weight: 400;
  color: #94a3b8;
  letter-spacing: 0.2px;
}
.section-title i {
  font-size: 14px;
  color: #475569;
}

/* ===== 意见卡片 — 工业风格 ===== */
.card-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

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

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

.opinion-dept {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  font-weight: 600;
  color: #1e293b;
  letter-spacing: 0.2px;
}

.opinion-dept-icon {
  font-size: 8px;
  color: #475569;
}

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

.opinion-content {
  font-size: 12px;
  color: #475569;
  line-height: 1.6;
  word-break: break-all;
  max-height: 90px;
  overflow-y: auto;
}

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

.opinion-empty {
  color: #94a3b8;
  font-size: 12px;
}

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

.opinion-card-footer {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 8px;
  padding-top: 6px;
  border-top: 1px solid #e2e8f0;
}

.opinion-footer-item {
  font-size: 11px;
  color: #94a3b8;
}

.empty-data {
  color: #94a3b8;
  font-size: 12px;
  padding: 6px 0;
}
</style>
