<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="施工队伍id" prop="deptId">
        <el-input v-model="formData.deptId" placeholder="请输入施工队伍id" />
      </el-form-item>
      <el-form-item label="项目id" prop="projectId">
        <el-input v-model="formData.projectId" placeholder="请输入项目id" />
      </el-form-item>
      <el-form-item label="任务id" prop="taskId">
        <el-input v-model="formData.taskId" placeholder="请输入任务id" />
      </el-form-item>
      <el-form-item label="项目类别(钻井 修井 注氮 酸化压裂... )" prop="projectClassification">
        <el-input v-model="formData.projectClassification" placeholder="请输入项目类别(钻井 修井 注氮 酸化压裂... )" />
      </el-form-item>
      <el-form-item label="施工工艺([123,233])" prop="techniqueIds">
        <el-input v-model="formData.techniqueIds" placeholder="请输入施工工艺([123,233])" />
      </el-form-item>
      <el-form-item label="施工设备([123,233])" prop="deviceIds">
        <el-input v-model="formData.deviceIds" placeholder="请输入施工设备([123,233])" />
      </el-form-item>
      <el-form-item label="时间节点-开始" prop="startTime">
        <el-date-picker
          v-model="formData.startTime"
          type="date"
          value-format="x"
          placeholder="选择时间节点-开始"
        />
      </el-form-item>
      <el-form-item label="时间节点-结束" prop="endTime">
        <el-date-picker
          v-model="formData.endTime"
          type="date"
          value-format="x"
          placeholder="选择时间节点-结束"
        />
      </el-form-item>
      <el-form-item label="累计施工井" prop="cumulativeWorkingWell">
        <el-input v-model="formData.cumulativeWorkingWell" placeholder="请输入累计施工井" />
      </el-form-item>
      <el-form-item label="累计施工层" prop="cumulativeWorkingLayers">
        <el-input v-model="formData.cumulativeWorkingLayers" placeholder="请输入累计施工层" />
      </el-form-item>
      <el-form-item label="当日泵车台次" prop="dailyPumpTrips">
        <el-input v-model="formData.dailyPumpTrips" placeholder="请输入当日泵车台次" />
      </el-form-item>
      <el-form-item label="当日仪表/混砂" prop="dailyToolsSand">
        <el-input v-model="formData.dailyToolsSand" placeholder="请输入当日仪表/混砂" />
      </el-form-item>
      <el-form-item label="趟数" prop="runCount">
        <el-input v-model="formData.runCount" placeholder="请输入趟数" />
      </el-form-item>
      <el-form-item label="桥塞" prop="bridgePlug">
        <el-input v-model="formData.bridgePlug" placeholder="请输入桥塞" />
      </el-form-item>
      <el-form-item label="水方量" prop="waterVolume">
        <el-input v-model="formData.waterVolume" placeholder="请输入水方量" />
      </el-form-item>
      <el-form-item label="时间H" prop="hourCount">
        <el-input v-model="formData.hourCount" placeholder="请输入时间H" />
      </el-form-item>
      <el-form-item label="当日油耗(吨)" prop="dailyFuel">
        <el-input v-model="formData.dailyFuel" placeholder="请输入当日油耗(吨)" />
      </el-form-item>
      <el-form-item label="当日用电量(kWh)" prop="dailyPowerUsage">
        <el-input v-model="formData.dailyPowerUsage" placeholder="请输入当日用电量(kWh)" />
      </el-form-item>
      <el-form-item label="生产时间(H)" prop="productionTime">
        <el-date-picker
          v-model="formData.productionTime"
          type="date"
          value-format="x"
          placeholder="选择生产时间(H)"
        />
      </el-form-item>
      <el-form-item label="非生产时间(H)" prop="nonProductionTime">
        <el-date-picker
          v-model="formData.nonProductionTime"
          type="date"
          value-format="x"
          placeholder="选择非生产时间(H)"
        />
      </el-form-item>
      <el-form-item label="非生产时间原因" prop="rdNptReason">
        <el-input v-model="formData.rdNptReason" placeholder="请输入非生产时间原因" />
      </el-form-item>
      <el-form-item label="施工开始日期" prop="constructionStartDate">
        <el-date-picker
          v-model="formData.constructionStartDate"
          type="date"
          value-format="x"
          placeholder="选择施工开始日期"
        />
      </el-form-item>
      <el-form-item label="施工结束日期" prop="constructionEndDate">
        <el-date-picker
          v-model="formData.constructionEndDate"
          type="date"
          value-format="x"
          placeholder="选择施工结束日期"
        />
      </el-form-item>
      <el-form-item label="当日生产情况生产动态" prop="productionStatus">
        <el-radio-group v-model="formData.productionStatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="外租情况(可能有附件)" prop="externalRental">
        <el-input v-model="formData.externalRental" placeholder="请输入外租情况(可能有附件)" />
      </el-form-item>
      <el-form-item label="下步工作计划" prop="nextPlan">
        <el-input v-model="formData.nextPlan" placeholder="请输入下步工作计划" />
      </el-form-item>
      <el-form-item label="施工状态(动迁上井/动迁下井/施工准备/施工...)" prop="rdStatus">
        <el-radio-group v-model="formData.rdStatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="故障情况" prop="malfunction">
        <el-input v-model="formData.malfunction" placeholder="请输入故障情况" />
      </el-form-item>
      <el-form-item label="故障误工h" prop="faultDowntime">
        <el-date-picker
          v-model="formData.faultDowntime"
          type="date"
          value-format="x"
          placeholder="选择故障误工h"
        />
      </el-form-item>
      <el-form-item label="人员情况" prop="personnel">
        <el-input v-model="formData.personnel" placeholder="请输入人员情况" />
      </el-form-item>
      <el-form-item label="全员数量" prop="totalStaffNum">
        <el-input v-model="formData.totalStaffNum" placeholder="请输入全员数量" />
      </el-form-item>
      <el-form-item label="休假人员数量" prop="leaveStaffNum">
        <el-input v-model="formData.leaveStaffNum" placeholder="请输入休假人员数量" />
      </el-form-item>
      <el-form-item label="不同专业公司的扩展属性值" prop="extProperty">
        <el-input v-model="formData.extProperty" placeholder="请输入不同专业公司的扩展属性值" />
      </el-form-item>
      <el-form-item label="排序值" prop="sort">
        <el-input v-model="formData.sort" placeholder="请输入排序值" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="状态(0启用 1禁用)" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="流程实例id" prop="processInstanceId">
        <el-input v-model="formData.processInstanceId" placeholder="请输入流程实例id" />
      </el-form-item>
      <el-form-item label="审批状态 未提交、审批中、审批通过、审批不通过、已取消" prop="auditStatus">
        <el-radio-group v-model="formData.auditStatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
    </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 { IotRdDailyReportApi, IotRdDailyReportVO } from '@/api/pms/iotrddailyreport'

/** 瑞都日报 表单 */
defineOptions({ name: 'IotRdDailyReportForm' })

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

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  deptId: undefined,
  projectId: undefined,
  taskId: undefined,
  projectClassification: undefined,
  techniqueIds: undefined,
  deviceIds: undefined,
  startTime: undefined,
  endTime: undefined,
  cumulativeWorkingWell: undefined,
  cumulativeWorkingLayers: undefined,
  dailyPumpTrips: undefined,
  dailyToolsSand: undefined,
  runCount: undefined,
  bridgePlug: undefined,
  waterVolume: undefined,
  hourCount: undefined,
  dailyFuel: undefined,
  dailyPowerUsage: undefined,
  productionTime: undefined,
  nonProductionTime: undefined,
  rdNptReason: undefined,
  constructionStartDate: undefined,
  constructionEndDate: undefined,
  productionStatus: undefined,
  externalRental: undefined,
  nextPlan: undefined,
  rdStatus: undefined,
  malfunction: undefined,
  faultDowntime: undefined,
  personnel: undefined,
  totalStaffNum: undefined,
  leaveStaffNum: undefined,
  extProperty: undefined,
  sort: undefined,
  remark: undefined,
  status: undefined,
  processInstanceId: undefined,
  auditStatus: undefined,
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await IotRdDailyReportApi.getIotRdDailyReport(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as IotRdDailyReportVO
    if (formType.value === 'create') {
      await IotRdDailyReportApi.createIotRdDailyReport(data)
      message.success(t('common.createSuccess'))
    } else {
      await IotRdDailyReportApi.updateIotRdDailyReport(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    deptId: undefined,
    projectId: undefined,
    taskId: undefined,
    projectClassification: undefined,
    techniqueIds: undefined,
    deviceIds: undefined,
    startTime: undefined,
    endTime: undefined,
    cumulativeWorkingWell: undefined,
    cumulativeWorkingLayers: undefined,
    dailyPumpTrips: undefined,
    dailyToolsSand: undefined,
    runCount: undefined,
    bridgePlug: undefined,
    waterVolume: undefined,
    hourCount: undefined,
    dailyFuel: undefined,
    dailyPowerUsage: undefined,
    productionTime: undefined,
    nonProductionTime: undefined,
    rdNptReason: undefined,
    constructionStartDate: undefined,
    constructionEndDate: undefined,
    productionStatus: undefined,
    externalRental: undefined,
    nextPlan: undefined,
    rdStatus: undefined,
    malfunction: undefined,
    faultDowntime: undefined,
    personnel: undefined,
    totalStaffNum: undefined,
    leaveStaffNum: undefined,
    extProperty: undefined,
    sort: undefined,
    remark: undefined,
    status: undefined,
    processInstanceId: undefined,
    auditStatus: undefined,
  }
  formRef.value?.resetFields()
}
</script>