<template>
  <el-row class="loss-list-pc" :gutter="24">
    <template v-for="(item, index) in configs" :key="index">
      <el-col :span="colSpan">
        <el-form-item :label="item.lossTypeName">
          <el-input :modelValue="getValue(item)" @update:modelValue="(event) => changeValue(item, event)">
            <template #suffix>
              <span>{{ item.unit }}</span>
            </template>
          </el-input>
        </el-form-item>
      </el-col>
      <el-col :span="24" v-if="item.lossTypeCode == 'OTHER_LOSS'">
        <el-form-item label="其它损失描述">
          <el-input v-model="getValueItem(item).remark"  placeholder="请填写"> </el-input>
        </el-form-item>
      </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 item = getValueItem(config)
  return item?.totalAmount || 0
}

const getValueItem = (config) => {
  let item = props.modelValue.find((v) => v.lossTypeId === config.lossTypeId)
  if (item == null) {
    item = { ...config }
    props.modelValue.push(item)
  }
  return item
}

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-pc {
  width: 100%;

  .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;
        }
      }
    }
  }
}
</style>
