<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="保养计划id" prop="planId">
        <el-input
          v-model="queryParams.planId"
          placeholder="请输入保养计划id"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="保养计划明细id" prop="planDetailId">
        <el-input
          v-model="queryParams.planDetailId"
          placeholder="请输入保养计划明细id"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="保养工单id" prop="workOrderId">
        <el-input
          v-model="queryParams.workOrderId"
          placeholder="请输入保养工单id"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="所属设备分类" prop="deviceCategoryId">
        <el-input
          v-model="queryParams.deviceCategoryId"
          placeholder="请输入所属设备分类"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="设备id" prop="deviceId">
        <el-input
          v-model="queryParams.deviceId"
          placeholder="请输入设备id"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="保养规则(量程 运行时间 自然日期) 可多选" prop="rule">
        <el-input
          v-model="queryParams.rule"
          placeholder="请输入保养规则(量程 运行时间 自然日期) 可多选"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="保养规则-里程(0启用 1停用)" prop="mileageRule">
        <el-input
          v-model="queryParams.mileageRule"
          placeholder="请输入保养规则-里程(0启用 1停用)"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="保养规则-自然日期(0启用 1停用)" prop="naturalDateRule">
        <el-input
          v-model="queryParams.naturalDateRule"
          placeholder="请输入保养规则-自然日期(0启用 1停用)"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="保养规则-运行时间(0启用 1停用)" prop="runningTimeRule">
        <el-input
          v-model="queryParams.runningTimeRule"
          placeholder="请输入保养规则-运行时间(0启用 1停用)"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="上次保养运行时长(小时)" prop="lastRunningTime">
        <el-date-picker
          v-model="queryParams.lastRunningTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="下次保养运行时长(小时)" prop="nextRunningTime">
        <el-date-picker
          v-model="queryParams.nextRunningTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="推迟运行时长(小时)" prop="delay duration">
        <el-input
          v-model="queryParams.delayDuration"
          placeholder="请输入推迟运行时长(小时)"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="上次保养运行公里数(千米)" prop="lastRunningKilometers">
        <el-input
          v-model="queryParams.lastRunningKilometers"
          placeholder="请输入上次保养运行公里数(千米)"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="下次保养运行公里数(千米)" prop="nextRunningKilometers">
        <el-input
          v-model="queryParams.nextRunningKilometers"
          placeholder="请输入下次保养运行公里数(千米)"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="推迟运行公里数(千米)" prop="delayKilometers">
        <el-input
          v-model="queryParams.delayKilometers"
          placeholder="请输入推迟运行公里数(千米)"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="上次保养自然日期(天)" prop="lastNaturalDate">
        <el-date-picker
          v-model="queryParams.lastNaturalDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="下次保养自然日期(天)" prop="nextNaturalDate">
        <el-date-picker
          v-model="queryParams.nextNaturalDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="推迟自然日期(天)" prop="delayNaturalDate">
        <el-date-picker
          v-model="queryParams.delayNaturalDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="推迟原因" prop="delayReason">
        <el-input
          v-model="queryParams.delayReason"
          placeholder="请输入推迟原因"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="上次保养日期" prop="lastMainDate">
        <el-date-picker
          v-model="queryParams.lastMainDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="时间周期(小时)" prop="timePeriod">
        <el-input
          v-model="queryParams.timePeriod"
          placeholder="请输入时间周期(小时)"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="时间周期-提前量(小时)" prop="timePeriodLead">
        <el-input
          v-model="queryParams.timePeriodLead"
          placeholder="请输入时间周期-提前量(小时)"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="公里数周期(千米)" prop="kilometerCycle">
        <el-input
          v-model="queryParams.kilometerCycle"
          placeholder="请输入公里数周期(千米)"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="公里数周期-提前量(千米)" prop="kiloCycleLead">
        <el-input
          v-model="queryParams.kiloCycleLead"
          placeholder="请输入公里数周期-提前量(千米)"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="自然日周期(天)" prop="naturalDatePeriod">
        <el-input
          v-model="queryParams.naturalDatePeriod"
          placeholder="请输入自然日周期(天)"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="自然日周期-提前量(天)" prop="naturalDatePeriodLead">
        <el-input
          v-model="queryParams.naturalDatePeriodLead"
          placeholder="请输入自然日周期-提前量(天)"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="BOM节点id" prop="bomNodeId">
        <el-input
          v-model="queryParams.bomNodeId"
          placeholder="请输入BOM节点id"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="BOM名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入BOM名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="BOM编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入BOM编码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="父BOM id 顶级为0" prop="parentId">
        <el-input
          v-model="queryParams.parentId"
          placeholder="请输入父BOM id 顶级为0"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="子节点id 逗号分隔" prop="childIds">
        <el-input
          v-model="queryParams.childIds"
          placeholder="请输入子节点id 逗号分隔"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="层级" prop="level">
        <el-input
          v-model="queryParams.level"
          placeholder="请输入层级"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="是否叶子节点 0是 1否" prop="leafFlag">
        <el-input
          v-model="queryParams.leafFlag"
          placeholder="请输入是否叶子节点 0是 1否"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="显示顺序" prop="sort">
        <el-input
          v-model="queryParams.sort"
          placeholder="请输入显示顺序"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="1维修 2保养 维修+保养 (1,2)" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择1维修 2保养 维修+保养 (1,2)"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="保养类型(1临时保养  2计划保养)" prop="mainType">
        <el-select
          v-model="queryParams.mainType"
          placeholder="请选择保养类型(1临时保养  2计划保养)"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态 0启用  1停用" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态 0启用  1停用"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入备注"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="版本" prop="version">
        <el-input
          v-model="queryParams.version"
          placeholder="请输入版本"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['pms:iot-main-work-order-bom:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['pms:iot-main-work-order-bom:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="保养计划id" align="center" prop="planId" />
      <el-table-column label="保养计划明细id" align="center" prop="planDetailId" />
      <el-table-column label="保养工单id" align="center" prop="workOrderId" />
      <el-table-column label="所属设备分类" align="center" prop="deviceCategoryId" />
      <el-table-column label="设备id" align="center" prop="deviceId" />
      <el-table-column label="保养规则(量程 运行时间 自然日期) 可多选" align="center" prop="rule" />
      <el-table-column label="保养规则-里程(0启用 1停用)" align="center" prop="mileageRule" />
      <el-table-column label="保养规则-自然日期(0启用 1停用)" align="center" prop="naturalDateRule" />
      <el-table-column label="保养规则-运行时间(0启用 1停用)" align="center" prop="runningTimeRule" />
      <el-table-column label="上次保养运行时长(小时)" align="center" prop="lastRunningTime" />
      <el-table-column label="下次保养运行时长(小时)" align="center" prop="nextRunningTime" />
      <el-table-column label="推迟运行时长(小时)" align="center" prop="delayDuration" />
      <el-table-column label="上次保养运行公里数(千米)" align="center" prop="lastRunningKilometers" />
      <el-table-column label="下次保养运行公里数(千米)" align="center" prop="nextRunningKilometers" />
      <el-table-column label="推迟运行公里数(千米)" align="center" prop="delayKilometers" />
      <el-table-column label="上次保养自然日期(天)" align="center" prop="lastNaturalDate" />
      <el-table-column label="下次保养自然日期(天)" align="center" prop="nextNaturalDate" />
      <el-table-column label="推迟自然日期(天)" align="center" prop="delayNaturalDate" />
      <el-table-column label="推迟原因" align="center" prop="delayReason" />
      <el-table-column
        label="上次保养日期"
        align="center"
        prop="lastMainDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="时间周期(小时)" align="center" prop="timePeriod" />
      <el-table-column label="时间周期-提前量(小时)" align="center" prop="timePeriodLead" />
      <el-table-column label="公里数周期(千米)" align="center" prop="kilometerCycle" />
      <el-table-column label="公里数周期-提前量(千米)" align="center" prop="kiloCycleLead" />
      <el-table-column label="自然日周期(天)" align="center" prop="naturalDatePeriod" />
      <el-table-column label="自然日周期-提前量(天)" align="center" prop="naturalDatePeriodLead" />
      <el-table-column label="BOM节点id" align="center" prop="bomNodeId" />
      <el-table-column label="BOM名称" align="center" prop="name" />
      <el-table-column label="BOM编码" align="center" prop="code" />
      <el-table-column label="父BOM id 顶级为0" align="center" prop="parentId" />
      <el-table-column label="子节点id 逗号分隔" align="center" prop="childIds" />
      <el-table-column label="层级" align="center" prop="level" />
      <el-table-column label="是否叶子节点 0是 1否" align="center" prop="leafFlag" />
      <el-table-column label="显示顺序" align="center" prop="sort" />
      <el-table-column label="1维修 2保养 维修+保养 (1,2)" align="center" prop="type" />
      <el-table-column label="保养类型(1临时保养  2计划保养)" align="center" prop="mainType" />
      <el-table-column label="状态 0启用  1停用" align="center" prop="status" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="版本" align="center" prop="version" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['pms:iot-main-work-order-bom:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['pms:iot-main-work-order-bom:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <IotMainWorkOrderBomForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { IotMainWorkOrderBomApi, IotMainWorkOrderBomVO } from '@/api/pms/iotmainworkorderbom'
import IotMainWorkOrderBomForm from './IotMainWorkOrderBomForm.vue'

/** PMS 保养计划明细BOM 列表 */
defineOptions({ name: 'IotMainWorkOrderBom' })

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

const loading = ref(true) // 列表的加载中
const list = ref<IotMainWorkOrderBomVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  planId: undefined,
  planDetailId: undefined,
  workOrderId: undefined,
  deviceCategoryId: undefined,
  deviceId: undefined,
  rule: undefined,
  mileageRule: undefined,
  naturalDateRule: undefined,
  runningTimeRule: undefined,
  lastRunningTime: [],
  nextRunningTime: [],
  delayDuration: undefined,
  lastRunningKilometers: undefined,
  nextRunningKilometers: undefined,
  delayKilometers: undefined,
  lastNaturalDate: [],
  nextNaturalDate: [],
  delayNaturalDate: [],
  delayReason: undefined,
  lastMainDate: [],
  timePeriod: undefined,
  timePeriodLead: undefined,
  kilometerCycle: undefined,
  kiloCycleLead: undefined,
  naturalDatePeriod: undefined,
  naturalDatePeriodLead: undefined,
  bomNodeId: undefined,
  name: undefined,
  code: undefined,
  parentId: undefined,
  childIds: undefined,
  level: undefined,
  leafFlag: undefined,
  sort: undefined,
  type: undefined,
  mainType: undefined,
  status: undefined,
  remark: undefined,
  version: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

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

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

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

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

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await IotMainWorkOrderBomApi.exportIotMainWorkOrderBom(queryParams)
    download.excel(data, 'PMS 保养计划明细BOM.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
