<template>
  <el-row class="loss-list-detail" :gutter="24">
    <template  v-for="(item, index) in configs" :key="index">
      <el-col :span="colSpan">
        <div class="info-item">
          <span class="info-label">{{ item.lossTypeName }}：</span>
          <span class="info-value">{{ getValue(item) }}{{ item.unit }}</span>
        </div>
      </el-col>
      <el-col :span="colSpan" v-if="item.lossTypeCode == 'OTHER_LOSS'">
        <div class="info-item">
          <span class="info-label">其它损失描述：</span>
          <span class="info-value">{{ getRemark(item) }}</span>
        </div>
      </el-col>
    </template>
  </el-row>
</template>

<script setup>
import { ref, computed, onMounted, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { Delete, Plus } from '@element-plus/icons-vue'
import { request } from '@shared/utils/request'

const emit = defineEmits(['update:modelValue'])

const props = defineProps({
  modelValue: {
    type: Array,
    default: () => []
  },
  colSpan: {
    type: Number,
    default: 8
  }
})

const getValue = (config) => {
  const value = props.modelValue?.find((v) => v.lossTypeId === config.lossTypeId)
  if (value == null) props.modelValue?.push({ ...config })
  return value?.totalAmount || 0
}

const getRemark = (config) => {
  const value = props.modelValue?.find((v) => v.lossTypeId === config.lossTypeId)
  if (value == null) props.modelValue?.push({ ...config })
  return value?.remark || ''
}

const configs = ref([])

// 获取损失类型字典
const getLossDict = async () => {
  try {
    const res = await request({
      url: '/snow-ops-platform/water-damage/loss/typeAndInfo',
      method: 'get'
    })
    configs.value = res.data?.records
  } catch (error) {
    console.error('获取损失类型失败:', error)
    ElMessage.error('获取损失类型失败')
  }
}

onMounted(async () => {
  await getLossDict()
})
</script>

<style scoped lang="scss">
.loss-list-detail {

  .loss-table {
    margin-bottom: 16px;

    :deep(.el-table) {
      .amount-cell {
        display: flex;
        align-items: center;
        gap: 8px;

        .unit-text {
          color: #909399;
          font-size: 14px;
          white-space: nowrap;
        }
      }
    }
  }

  .add-button-wrapper {
    display: flex;
    justify-content: flex-start;
  }

  .calculate-form {
    padding: 8px 0;

    :deep(.el-form-item) {
      margin-bottom: 20px;
    }
  }

  .calculation-preview {
    background-color: #f5f7fa;
    border-radius: 8px;
    padding: 16px;
    margin-top: 16px;

    .preview-item {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .preview-label {
        color: #606266;
        font-size: 14px;
      }

      .preview-value {
        color: #f56c6c;
        font-size: 16px;
        font-weight: 500;
      }
    }
  }

  .loss-picker-content {
    max-height: 400px;
    overflow-y: auto;
    padding: 8px 0;

    .loss-radio-group {
      display: flex;
      flex-direction: column;
      gap: 12px;
      width: 100%;

      .loss-radio-item {
        margin: 0;
        padding: 10px 12px;
        border-radius: 6px;
        width: 100%;
        box-sizing: border-box;

        :deep(.el-radio__label) {
          width: calc(100% - 22px);
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
      }
    }
  }
}
.info-item {
  display: flex;
  align-items: flex-start;
  line-height: 1.5;
  margin-top: 10px;

  .info-label {
    white-space: nowrap;
    flex-shrink: 0;
    color: #909399;
    font-size: 14px;
  }

  .info-value {
    flex: 1;
    color: #606266;
    font-size: 14px;
    word-break: break-all;
  }
}
</style>
