<template>
  <doc-alert title="【MES】工位绑定" url="https://doc.iocoder.cn/mes/workstation-binding/" />

  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="80px"
    >
      <el-form-item label="工位编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入工位编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="工位名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入工位名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable class="!w-240px">
          <el-option label="启用" :value="1" />
          <el-option label="禁用" :value="0" />
        </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="['mes:workstation-binding:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="danger"
          plain
          @click="handleDelete(selectionList.map((item) => item.id))"
          v-hasPermi="['mes:workstation-binding:delete']"
          :disabled="selectionList.length === 0"
        >
          <Icon icon="ep:delete" 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"
      @selection-change="handleSelectionChange"
      @row-click="handleRowClick"
    >
      <el-table-column type="selection" width="50" />
      <el-table-column width="80" label="ID" align="center" prop="id" />
      <el-table-column min-width="120" label="工位编号" align="center" prop="code" />
      <el-table-column min-width="120" label="工位名称" align="center" prop="name" />
      <el-table-column min-width="150" label="绑定工序" align="center" prop="processName" />
      <el-table-column min-width="120" label="报工人" align="center" prop="reporterName" />
      <el-table-column min-width="100" label="状态" align="center" prop="status">
        <template #default="scope">
          <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
            {{ scope.row.status === 1 ? '启用' : '禁用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column min-width="150" label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" fixed="right" width="120">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['mes:workstation-binding:update']"
            >编辑</el-button
          >
          <el-button
            link
            type="danger"
            @click="handleDelete([scope.row.id])"
            v-hasPermi="['mes:workstation-binding: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>

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

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  YVHgetWorkstationBindingPage,
  YVHdeleteWorkstationBinding
} from '@/api/mes/production/workstation'
import { Icon } from '@/components/Icon'
import WorkstationBindingForm from './WorkstationBindingForm.vue'

const loading = ref(true)
const list = ref<any[]>([])
const total = ref(0)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  code: undefined,
  name: undefined,
  status: undefined
})
const queryFormRef = ref()
const formRef = ref()
const selectionList = ref<any[]>([])

const getList = async () => {
  loading.value = true
  try {
    const res = await YVHgetWorkstationBindingPage(queryParams)
    list.value = res.list
    total.value = res.total
  } finally {
    loading.value = false
  }
}

const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

const resetQuery = () => {
  queryFormRef.value?.resetFields?.()
  queryParams.code = undefined
  queryParams.name = undefined
  queryParams.status = undefined
  handleQuery()
}

const openForm = (type: 'create' | 'update', id?: number) => {
  formRef.value.open(type, id)
}

const handleDelete = async (ids: number[]) => {
  if (!ids || ids.length === 0) return
  try {
    await ElMessageBox.confirm('确定要删除选中的工位绑定吗？', '提示', { type: 'warning' })
    for (const id of ids) {
      await YVHdeleteWorkstationBinding(id)
    }
    ElMessage.success('删除成功')
    getList()
    selectionList.value = selectionList.value.filter((item) => !ids.includes(item.id))
  } catch {}
}

const handleSelectionChange = (rows: any[]) => {
  selectionList.value = rows
}

/** 行点击操作 */
const handleRowClick = (row: any, column: any, event: MouseEvent) => {
  // 检查是否点击了按钮、链接或其他交互元素
  const target = event.target as HTMLElement
  if (
    target.tagName === 'BUTTON' ||
    target.tagName === 'A' ||
    target.tagName === 'I' ||
    target.tagName === 'svg' ||
    target.closest('button') ||
    target.closest('a') ||
    target.closest('.el-button') ||
    target.closest('.el-checkbox')
  ) {
    return
  }
  
  // 直接打开编辑页面
  openForm('update', row.id)
}

onMounted(() => {
  getList()
})
</script>
