<template>
  <Dialog title="能源消耗记录审核" v-model="dialogVisible" :width="600">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <!-- 基本信息展示 -->
      <el-form-item label="编号">
        <span>{{ recordData.code }}</span>
      </el-form-item>
      <el-form-item label="工单编号">
        <span>{{ recordData.workOrderCode }}</span>
      </el-form-item>
      <el-form-item label="消耗时间">
        <span>{{ formatDate(recordData.consumptionTime) }}</span>
      </el-form-item>
      
      <!-- 消耗量信息 -->
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="煤消耗量">
            <span>{{ recordData.coalConsumption }} 吨</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="蒸汽产量">
            <span>{{ recordData.steamOutput }} 吨</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="用电量">
            <span>{{ recordData.electricityConsumption }} 千瓦时</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="用水量">
            <span>{{ recordData.waterConsumption }} 立方米</span>
          </el-form-item>
        </el-col>
      </el-row>

      <el-divider content-position="left">审核信息</el-divider>
      
      <!-- 审核表单 -->
      <el-form-item label="审核结果" prop="auditStatus">
        <el-radio-group v-model="formData.auditStatus">
          <el-radio :label="1">审核通过</el-radio>
          <el-radio :label="2">审核拒绝</el-radio>
        </el-radio-group>
      </el-form-item>
      
      <el-form-item label="审核意见" prop="auditRemark">
        <el-input
          v-model="formData.auditRemark"
          type="textarea"
          placeholder="请输入审核意见"
          :rows="3"
        />
      </el-form-item>

      <!-- 煤消耗提醒 -->
      <el-alert
        v-if="formData.auditStatus === 1 && recordData.coalConsumption > 0"
        title="提醒"
        type="warning"
        :closable="false"
        show-icon
      >
        <template #default>
          审核通过后将自动扣减煤库存 {{ recordData.coalConsumption }} 吨，请确认库存充足。
        </template>
      </el-alert>
    </el-form>
    
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">
        确 定
      </el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { formatDate } from '@/utils/formatTime'
import { auditEnergyConsumption } from '@/api/mes/energy/consumption'

const { t } = useI18n()
const message = useMessage()

const dialogVisible = ref(false)
const formLoading = ref(false)
const recordData = ref({
  id: undefined,
  code: '',
  workOrderCode: '',
  consumptionTime: '',
  coalConsumption: 0,
  steamOutput: 0,
  electricityConsumption: 0,
  waterConsumption: 0
})
const formData = ref({
  id: undefined,
  auditStatus: 1,
  auditRemark: ''
})
const formRules = reactive({
  auditStatus: [{ required: true, message: '请选择审核结果', trigger: 'change' }]
})
const formRef = ref()

/** 打开弹窗 */
const open = async (row: any) => {
  dialogVisible.value = true
  recordData.value = { ...row }
  formData.value = {
    id: row.id,
    auditStatus: 1,
    auditRemark: ''
  }
  formRef.value?.resetFields()
}

/** 提交表单 */
const emit = defineEmits(['success'])
const submitForm = async () => {
  const valid = await formRef.value.validate()
  if (!valid) return
  
  formLoading.value = true
  try {
    await auditEnergyConsumption(formData.value)
    message.success('审核成功')
    dialogVisible.value = false
    emit('success')
  } finally {
    formLoading.value = false
  }
}

defineExpose({ open })
</script>
