<template>
  <el-dialog 
    title="水电明细" 
    v-model="visible"
    :close-on-click-modal="false" 
    draggable
    width="1200px">
    <el-table 
      :data="detailList" 
      v-loading="loading" 
      border 
      :cell-style="{ textAlign: 'center' }" 
      :header-cell-style="{ textAlign: 'center', background: 'var(--el-table-row-hover-bg-color)' }">
      <el-table-column type="index" label="序号" width="60" align="center" />
      <el-table-column prop="year" label="年份" show-overflow-tooltip align="center" />
      <el-table-column prop="month" label="月份" show-overflow-tooltip align="center" />
      <el-table-column prop="subiMonthSum" label="用量" show-overflow-tooltip align="center" />
      <el-table-column prop="subWatFlagSum" label="费用" show-overflow-tooltip align="center">
        <template #default="scope">
          <span>{{ scope.row.subWatFlagSum ? Number(scope.row.subWatFlagSum).toFixed(2) : '0.00' }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="flag" label="用电用水" show-overflow-tooltip align="center">
        <template #default="scope">
          <span>{{ formatFlag(scope.row.flag) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="meterNum" label="冷水热水" show-overflow-tooltip align="center">
        <template #default="scope">
          <span>{{ formatMeterNum(scope.row.meterNum) }}</span>
        </template>
      </el-table-column>
    </el-table>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">关 闭</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="WaterDetailDialog">
import { ref, onMounted } from 'vue'
import { useMessage } from '/@/hooks/message'
import { lookDetails } from '/@/api/stuwork/watermonthreport'

// 定义变量内容
const visible = ref(false)
const loading = ref(false)
const detailList = ref<any[]>([])
const currentRoomNo = ref('')

// 格式化用电用水
const formatFlag = (value: string | number) => {
  if (value === '1' || value === 1) {
    return '用电'
  } else if (value === '2' || value === 2) {
    return '用水'
  }
  return value || '-'
}

// 格式化冷水热水
const formatMeterNum = (value: string | number) => {
  if (value === '11' || value === 11) {
    return '冷水'
  } else if (value === '12' || value === 12) {
    return '热水'
  }
  return value || '-'
}

// 打开弹窗
const openDialog = async (roomNo: string) => {
  visible.value = true
  currentRoomNo.value = roomNo
  detailList.value = []
  
  // 获取明细数据
  await getDetailData(roomNo)
}

// 获取明细数据
const getDetailData = async (roomNo: string) => {
  loading.value = true
  try {
    const res = await lookDetails(roomNo)
    if (res.data) {
      if (Array.isArray(res.data)) {
        detailList.value = res.data
      } else if (res.data.records && Array.isArray(res.data.records)) {
        detailList.value = res.data.records
      } else if (res.data.list && Array.isArray(res.data.list)) {
        detailList.value = res.data.list
      } else {
        detailList.value = []
      }
    } else {
      detailList.value = []
    }
  } catch (err: any) {
    useMessage().error(err.msg || '获取明细数据失败')
    detailList.value = []
  } finally {
    loading.value = false
  }
}

// 暴露方法给父组件
defineExpose({
  openDialog
})
</script>

