<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="80px"
    >
      <el-form-item label="机台名称" prop="machineName">
        <el-input
          v-model="queryParams.machineName"
          placeholder="请输入机台名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="matName">
        <el-input
          v-model="queryParams.matName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="spec">
        <el-select
          v-model="queryParams.spec"
          placeholder="请选择规格型号"
          clearable
          filterable
          class="!w-240px"
        >
          <el-option
            v-for="item in specOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
        <el-form-item label="启用状态" prop="enabledStatus">
        <el-select
          v-model="queryParams.enabledStatus"
          placeholder="请选择状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.SYSTEM_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </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="['biz:machine-param:create']"
        >
          <Icon icon="ep:plus" 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" type="index" width="60px"/>
      <el-table-column label="机台名称" align="center" prop="machineName" />
      <el-table-column label="产品名称" align="center" prop="matName" />
      <el-table-column label="规格型号" align="center" prop="spec" />
      <el-table-column label="所属工序" align="center" prop="procName" />
      <el-table-column label="采集周期(分)" align="center" prop="collectCycle" />
      <el-table-column label="状态" align="center" prop="enabledStatus">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SYSTEM_STATUS" :value="scope.row.enabledStatus" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建人" align="center" prop="creator" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter2"
        width="180px"
      />
      <el-table-column label="操作" align="center">
        <template #default="scope">
         
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['biz:machine-param:update']"
          >
            编辑
          </el-button>
           <el-button
            link
            type="primary"
            @click="openDetail(scope.row.id)"
            v-hasPermi="['biz:machine-param:detail']"
          >
            详情
          </el-button>
          <!-- <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['biz:machine-param: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>

  <!-- 表单弹窗：添加/修改 -->
  <MachineParamForm ref="formRef" @success="getList" @close="handleQuery"/>
  <!-- 详情弹窗 -->
  <MachineParamDetail ref="detailRef" />
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter2 } from '@/utils/formatTime'
import * as MachineParamApi from '@/api/biz/machineparam'
import * as MaterialApi from '@/api/biz/material'
import MachineParamForm from './MachineParamForm.vue'
import MachineParamDetail from './MachineParamDetail.vue'

defineOptions({ name: 'MachineParam' })

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

const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  enabledStatus: undefined,
  matName: undefined,
  machineName: undefined,
  spec: undefined,
})
const queryFormRef = ref() // 搜索的表单

// 规格型号下拉选项
const specOptions = ref<{ label: string; value: string }[]>([])

/** 加载规格型号下拉选项 */
const loadSpecOptions = async () => {
  try {
    const data = await MaterialApi.getSpecDropdown({})
    specOptions.value = data.map(item => ({
      label: item.spec,
      value: item.spec
    }))
  } catch (error) {
    console.error('加载规格型号下拉选项失败:', error)
  }
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await MachineParamApi.getMachineParamPage(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 detailRef = ref()
const openDetail = (id: number) => {
  detailRef.value.open(id)
}

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

/** 初始化 **/
onMounted(() => {
  // 加载规格型号下拉选项，不自动查询列表
  loadSpecOptions()
})
</script>