<template>
  <!-- 列表 -->
  <ContentWrap>
<!--    <el-button-->
<!--      type="primary"-->
<!--      plain-->
<!--      @click="openForm('create')"-->
<!--      v-hasPermi="['mes:energy-device:create']"-->
<!--    >-->
<!--      <Icon icon="ep:plus" class="mr-5px" /> 新增抄表-->
<!--    </el-button>-->
<!--    <el-text style="color:#339c1e;">{{props.deviceName}}</el-text>-->
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column
        :label="t('EnergyManagement.EnergyDeviceCheck.dialogCheckTimeLabel')"
        align="center"
        prop="checkTime"
        :formatter="dateFormatter"
        width="170px"
        sortable
      />
       <el-table-column
        :label="t('EnergyManagement.EnergyDeviceCheck.dialogLastCheckTimeLabel')"
        align="center"
        prop="lastCheckTime"
        :formatter="dateFormatter"
        width="170px"
        sortable
      />
      <el-table-column :label="t('EnergyManagement.EnergyDeviceCheck.dialogCheckValueLabel')" align="center" prop="checkValue" />
      <el-table-column :label="t('EnergyManagement.EnergyDeviceCheck.dialogLastCheckValueLabel')" align="center" prop="lastCheckValue" />
      <el-table-column :label="t('EnergyManagement.EnergyDeviceCheck.dialogDiffValueLabel')" align="center" prop="diffValue" />
      <el-table-column :label="t('EnergyManagement.EnergyDeviceCheck.dialogUnitPriceLabel')" align="center" prop="unitPrice" />
      <el-table-column :label="t('EnergyManagement.EnergyDeviceCheck.dialogRemarkLabel')" align="center" prop="remark" />
      <el-table-column
        :label="t('common.createTime')"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center">
        <template #default="scope">
<!--          <el-button-->
<!--            link-->
<!--            type="primary"-->
<!--            @click="openForm('update', scope.row.id)"-->
<!--            v-hasPermi="['mes:energy-device:update']"-->
<!--          >-->
<!--            编辑-->
<!--          </el-button>-->
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['mes:energy-device:delete']"
          >
            {{ t('common.delMessage') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
    <!-- 表单弹窗：添加/修改 -->
    <EnergyDeviceCheckRecordForm ref="formRef" @success="getList" />
</template>
<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import { EnergyDeviceApi } from '@/api/mes/energydevice'
import EnergyDeviceCheckRecordForm from './EnergyDeviceCheckRecordForm.vue'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const props = defineProps<{
  deviceId?: number // 能源设备Id（主表的关联字段）
  deviceName?: string
}>()
const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  deviceId: undefined as unknown
})

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.deviceId,
  (val: number) => {
    if (!val) {
      return
    }
    queryParams.deviceId = val
    handleQuery()
  },
    { immediate: true, deep: true }
)

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await EnergyDeviceApi.getEnergyDeviceCheckRecordPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  if (!props.deviceId) {
    message.error('请选择一个能源设备')
    return
  }
  formRef.value.open(type, id, props.deviceId,props.deviceName)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await EnergyDeviceApi.deleteEnergyDeviceCheckRecord(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
</script>
