<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="搬迁安装天数(D)" prop="relocationDays">
        <el-input v-model="formData.relocationDays" placeholder="请输入搬迁安装天数(D)" />
      </el-form-item>
      <el-form-item label="上井次完井时间" prop="lastestWellDoneTime">
        <el-date-picker
          v-model="formData.lastestWellDoneTime"
          type="date"
          value-format="x"
          placeholder="选择上井次完井时间"
        />
      </el-form-item>
      <el-form-item label="当前井深(m)" prop="currentDepth">
        <el-input v-model="formData.currentDepth" placeholder="请输入当前井深(m)" />
      </el-form-item>
      <el-form-item label="日进尺(m)" prop="dailyFootage">
        <el-input v-model="formData.dailyFootage" placeholder="请输入日进尺(m)" />
      </el-form-item>
      <el-form-item label="月进尺(m)" prop="monthlyFootage">
        <el-input v-model="formData.monthlyFootage" placeholder="请输入月进尺(m)" />
      </el-form-item>
      <el-form-item label="年累计进尺(m)" prop="annualFootage">
        <el-input v-model="formData.annualFootage" placeholder="请输入年累计进尺(m)" />
      </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="当月用电量(kWh)" prop="monthlyPowerUsage">
        <el-input v-model="formData.monthlyPowerUsage" placeholder="请输入当月用电量(kWh)" />
      </el-form-item>
      <el-form-item label="当日油耗(吨)" prop="dailyFuel">
        <el-input v-model="formData.dailyFuel" placeholder="请输入当日油耗(吨)" />
      </el-form-item>
      <el-form-item label="当月油耗(吨)" prop="monthlyFuel">
        <el-input v-model="formData.monthlyFuel" placeholder="请输入当月油耗(吨)" />
      </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="nptReason">
        <el-input v-model="formData.nptReason" 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="nextPlan">
        <el-input v-model="formData.nextPlan" placeholder="请输入下步工作计划" />
      </el-form-item>
      <el-form-item label="施工状态(动迁 准备 施工 完工)" prop="rigStatus">
        <el-radio-group v-model="formData.rigStatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="人员情况" prop="personnel">
        <el-input v-model="formData.personnel" placeholder="请输入人员情况" />
      </el-form-item>
      <el-form-item label="泥浆性能-密度(g/cm³)" prop="mudDensity">
        <el-input v-model="formData.mudDensity" placeholder="请输入泥浆性能-密度(g/cm³)" />
      </el-form-item>
      <el-form-item label="泥浆性能-粘度(S)" prop="mudViscosity">
        <el-input v-model="formData.mudViscosity" placeholder="请输入泥浆性能-粘度(S)" />
      </el-form-item>
      <el-form-item label="水平段长度(m) 适用于水平井" prop="lateralLength">
        <el-input v-model="formData.lateralLength" placeholder="请输入水平段长度(m) 适用于水平井" />
      </el-form-item>
      <el-form-item label="井斜(°)" prop="wellInclination">
        <el-input v-model="formData.wellInclination" placeholder="请输入井斜(°)" />
      </el-form-item>
      <el-form-item label="方位(°)" prop="azimuth">
        <el-input v-model="formData.azimuth" 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 { IotRyDailyReportApi, IotRyDailyReportVO } from '@/api/pms/iotrydailyreport'

/** 瑞鹰日报 表单 */
defineOptions({ name: 'IotRyDailyReportForm' })

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,
  relocationDays: undefined,
  lastestWellDoneTime: undefined,
  currentDepth: undefined,
  dailyFootage: undefined,
  monthlyFootage: undefined,
  annualFootage: undefined,
  dailyPowerUsage: undefined,
  monthlyPowerUsage: undefined,
  dailyFuel: undefined,
  monthlyFuel: undefined,
  nonProductionTime: undefined,
  nptReason: undefined,
  constructionStartDate: undefined,
  constructionEndDate: undefined,
  productionStatus: undefined,
  nextPlan: undefined,
  rigStatus: undefined,
  personnel: undefined,
  mudDensity: undefined,
  mudViscosity: undefined,
  lateralLength: undefined,
  wellInclination: undefined,
  azimuth: 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 IotRyDailyReportApi.getIotRyDailyReport(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 IotRyDailyReportVO
    if (formType.value === 'create') {
      await IotRyDailyReportApi.createIotRyDailyReport(data)
      message.success(t('common.createSuccess'))
    } else {
      await IotRyDailyReportApi.updateIotRyDailyReport(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,
    relocationDays: undefined,
    lastestWellDoneTime: undefined,
    currentDepth: undefined,
    dailyFootage: undefined,
    monthlyFootage: undefined,
    annualFootage: undefined,
    dailyPowerUsage: undefined,
    monthlyPowerUsage: undefined,
    dailyFuel: undefined,
    monthlyFuel: undefined,
    nonProductionTime: undefined,
    nptReason: undefined,
    constructionStartDate: undefined,
    constructionEndDate: undefined,
    productionStatus: undefined,
    nextPlan: undefined,
    rigStatus: undefined,
    personnel: undefined,
    mudDensity: undefined,
    mudViscosity: undefined,
    lateralLength: undefined,
    wellInclination: undefined,
    azimuth: undefined,
    extProperty: undefined,
    sort: undefined,
    remark: undefined,
    status: undefined,
    processInstanceId: undefined,
    auditStatus: undefined,
  }
  formRef.value?.resetFields()
}
</script>