<template>
  <div class="material-tab-container">
    <!-- 综合性资料 -->
    <div v-if="materialGroups.comprehensive.length > 0" class="material-group">
      <h4 class="group-title">综合性资料</h4>
      <el-table
        v-loading="loading"
        :data="materialGroups.comprehensive"
        style="width: 100%"
        border
      >
        <el-table-column prop="orderNum" label="序号" width="80" align="center">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="informationName"
          label="报送资料"
          header-align="center"
          align="left"
          min-width="200"
        />
        <el-table-column
          prop="informationTypeName"
          label="资料类型"
          min-width="150"
          align="center"
        />
        <el-table-column
          prop="isRequired"
          label="是否必项"
          min-width="120"
          align="center"
        >
          <template slot-scope="scope">
            {{
              scope.row.isRequired === '1' || scope.row.isRequired === 1
                ? '是'
                : '否'
            }}
          </template>
        </el-table-column>
        <el-table-column
          prop="isUpload"
          label="是否上传"
          min-width="120"
          align="center"
        >
          <template slot-scope="scope">
            <span
              :class="
                scope.row.isUpload === '1' || scope.row.isUpload === 1
                  ? 'status-uploaded'
                  : 'status-not-uploaded'
              "
            >
              {{
                scope.row.isUpload === '1' || scope.row.isUpload === 1
                  ? '已上传'
                  : '未上传'
              }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          prop="uploadTime"
          label="上传时间"
          width="150"
          align="center"
        >
          <template slot-scope="scope">
            {{ scope.row.uploadTime ? scope.row.uploadTime : '--' }}
          </template>
        </el-table-column>
        <el-table-column
          prop="auditedStatus"
          label="初审结果"
          min-width="120"
          align="center"
        >
          <template slot-scope="scope">
            {{ formatAuditStatus(scope.row.auditedStatus) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="handleViewDetail(scope.row)"
            >
              查看
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 财务会计资料 -->
    <div v-if="materialGroups.financial.length > 0" class="material-group">
      <h4 class="group-title">财务会计资料</h4>
      <el-table
        v-loading="loading"
        :data="materialGroups.financial"
        style="width: 100%"
        border
      >
        <el-table-column prop="orderNum" label="序号" width="80" align="center">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="informationName"
          label="报送资料"
          header-align="center"
          align="left"
          min-width="200"
        />
        <el-table-column
          prop="informationTypeName"
          label="资料类型"
          min-width="150"
          align="center"
        />
        <el-table-column
          prop="isRequired"
          label="是否必项"
          min-width="120"
          align="center"
        >
          <template slot-scope="scope">
            {{
              scope.row.isRequired === '1' || scope.row.isRequired === 1
                ? '是'
                : '否'
            }}
          </template>
        </el-table-column>
        <el-table-column
          prop="isUpload"
          label="是否上传"
          min-width="120"
          align="center"
        >
          <template slot-scope="scope">
            <span
              :class="
                scope.row.isUpload === '1' || scope.row.isUpload === 1
                  ? 'status-uploaded'
                  : 'status-not-uploaded'
              "
            >
              {{
                scope.row.isUpload === '1' || scope.row.isUpload === 1
                  ? '已上传'
                  : '未上传'
              }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          prop="uploadTime"
          label="上传时间"
          width="150"
          align="center"
        >
          <template slot-scope="scope">
            {{ scope.row.uploadTime ? scope.row.uploadTime : '--' }}
          </template>
        </el-table-column>
        <el-table-column
          prop="auditedStatus"
          label="初审结果"
          min-width="120"
          align="center"
        >
          <template slot-scope="scope">
            {{ formatAuditStatus(scope.row.auditedStatus) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="handleViewDetail(scope.row)"
            >
              查看
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 其他资料 -->
    <div v-if="materialGroups.other.length > 0" class="material-group">
      <h4 class="group-title">其他资料</h4>
      <el-table
        v-loading="loading"
        :data="materialGroups.other"
        style="width: 100%"
        border
      >
        <el-table-column prop="orderNum" label="序号" width="80" align="center">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="informationName"
          label="报送资料"
          header-align="center"
          align="left"
          min-width="200"
        />
        <el-table-column
          prop="informationTypeName"
          label="资料类型"
          min-width="150"
          align="center"
        />
        <el-table-column
          prop="isRequired"
          label="是否必项"
          min-width="120"
          align="center"
        >
          <template slot-scope="scope">
            {{
              scope.row.isRequired === '1' || scope.row.isRequired === 1
                ? '是'
                : '否'
            }}
          </template>
        </el-table-column>
        <el-table-column
          prop="isUpload"
          label="是否上传"
          min-width="120"
          align="center"
        >
          <template slot-scope="scope">
            <span
              :class="
                scope.row.isUpload === '1' || scope.row.isUpload === 1
                  ? 'status-uploaded'
                  : 'status-not-uploaded'
              "
            >
              {{
                scope.row.isUpload === '1' || scope.row.isUpload === 1
                  ? '已上传'
                  : '未上传'
              }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          prop="uploadTime"
          label="上传时间"
          width="150"
          align="center"
        >
          <template slot-scope="scope">
            {{ scope.row.uploadTime ? scope.row.uploadTime : '--' }}
          </template>
        </el-table-column>
        <el-table-column
          prop="auditedStatus"
          label="初审结果"
          min-width="120"
          align="center"
        >
          <template slot-scope="scope">
            {{ formatAuditStatus(scope.row.auditedStatus) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="handleViewDetail(scope.row)"
            >
              查看
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 无数据提示 -->
    <div
      v-if="
        materialGroups.comprehensive.length === 0 &&
        materialGroups.financial.length === 0 &&
        materialGroups.other.length === 0 &&
        !loading
      "
      class="no-data-tip"
    >
      <p>暂无报送资料数据</p>
    </div>
  </div>
</template>

<script>
  import { getMaterialByTaskId } from '@/api/home'

  export default {
    name: 'MaterialTab',
    props: {
      project: {
        type: Object,
        default: () => ({}),
      },
      isView: {
        type: Boolean,
        default: false,
      },
    },
    data() {
      return {
        loading: false,
        materialData: [],
        materialGroups: {
          comprehensive: [], // 综合性资料
          financial: [], // 财务会计资料
          other: [], // 其他资料
        },
      }
    },
    watch: {
      project: {
        handler(newVal) {
          if (newVal && newVal.taskId) {
            this.loadMaterialData()
          }
        },
        deep: true,
        immediate: true,
      },
    },
    methods: {
      // 加载报送资料数据
      async loadMaterialData() {
        if (!this.project || !this.project.taskId) {
          return
        }
        try {
          this.loading = true
          const res = await getMaterialByTaskId({
            taskId: this.project.taskId,
          })

          if (res && res.state && res.value) {
            // 根据接口返回的数据结构进行处理
            // 如果返回是数组，直接使用；否则获取 records 或 data
            const materialList = Array.isArray(res.value)
              ? res.value
              : res.value.records || res.value.data || []

            this.materialData = materialList
            // 按资料类型分组
            this.groupMaterialData(materialList)
          } else {
            this.materialData = []
            this.resetMaterialGroups()
          }
        } catch (error) {
          console.error('加载报送资料失败:', error)
          this.materialData = []
          this.resetMaterialGroups()
        } finally {
          this.loading = false
        }
      },

      // 按资料类型分组
      groupMaterialData(data) {
        this.materialGroups.comprehensive = []
        this.materialGroups.financial = []
        this.materialGroups.other = []

        data.forEach((item, index) => {
          const typeName = item.informationTypeName || ''

          // 根据资料类型名称分类
          if (
            typeName.includes('财务') ||
            typeName.includes('会计') ||
            typeName.includes('金融')
          ) {
            this.materialGroups.financial.push(item)
          } else if (
            typeName.includes('综合') ||
            typeName.includes('基本') ||
            typeName.includes('一般')
          ) {
            this.materialGroups.comprehensive.push(item)
          } else {
            this.materialGroups.other.push(item)
          }
        })
      },

      // 重置分组数据
      resetMaterialGroups() {
        this.materialGroups.comprehensive = []
        this.materialGroups.financial = []
        this.materialGroups.other = []
      },

      // 查看详情
      handleViewDetail(row) {
        this.$message.info(`查看资料：${row.informationName}`)
        // 可在此处添加打开详情弹窗的逻辑
      },

      // 格式化审核状态
      formatAuditStatus(status) {
        const statusMap = {
          0: '未审核',
          1: '已审核',
          2: '审核通过',
          3: '审核不通过',
        }
        return statusMap[status] || '未知'
      },
    },
  }
</script>

<style lang="scss" scoped>
  .material-tab-container {
    padding: 20px 0;
  }

  .material-group {
    margin-bottom: 30px;

    .group-title {
      font-size: 14px;
      font-weight: 600;
      color: #333;
      margin: 0 0 15px 0;
      padding-left: 10px;
      border-left: 4px solid #409eff;
      background-color: #f5f7fa;
      padding: 10px 15px;
      margin-left: -20px;
      margin-right: -20px;
      padding-left: 26px;
    }
  }

  .no-data-tip {
    text-align: center;
    padding: 40px 20px;
    color: #909399;
    font-size: 14px;
  }

  .status-uploaded {
    color: #67c23a;
    font-weight: 500;
  }

  .status-not-uploaded {
    color: #f56c6c;
    font-weight: 500;
  }
</style>
