<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="锁定站名称" prop="lotoName">
        <el-input
          v-model="queryParams.lotoName"
          placeholder="请输入锁定站名称"
          clearable
          class="!w-240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="岗位" prop="workstationId">
        <el-tree-select
          v-model="queryParams.workstationId"
          :data="marsOptions"
          :props="{ label: 'workstationName', value: 'id', children: 'children' }"
          placeholder="选择岗位"
          class="!w-240px"
        />
      </el-form-item>
<!--      <el-form-item label="设备/工艺" prop="machineryId">-->
<!--        <el-tree-select-->
<!--          v-model="queryParams.machineryId"-->
<!--          :data="machineryOptions"-->
<!--          :props="{ label: 'machineryName', value: 'id', children: 'children' }"-->
<!--          placeholder="选择设备/工艺"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </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="['iscs:station:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="danger"
          plain
          :disabled="multiple"
          @click="handleDelete()"
          v-hasPermi="['iscs:station:delete']"
        >
          <Icon icon="ep:delete" class="mr-5px" /> 批量删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="锁定站名称" align="center" prop="lotoName" />
      <el-table-column label="排序" align="center" prop="orderNum" />
      <el-table-column label="岗位" align="center" prop="workstationName" />
      <el-table-column label="地图名称" align="center" prop="mapName" />
      <el-table-column label="所属硬件序列号" align="center" prop="lotoSerialNumber" />
      <el-table-column label="锁定站详情" align="center" >
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="handleView(scope.row)"
            v-hasPermi="['iscs:station:query']"
          >
            查看
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" >
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id,scope.row.mapId)"
            v-hasPermi="['iscs:station:update']"
          >
            修改
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['iscs:station: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>

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

<script lang="ts" setup>
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import * as LotoStationApi from '@/api/dv/lotoStation/index'
import * as MarsDeptApi from "@/api/system/marsdept/index"
import * as TechnologyApi from "@/api/dv/technology/index"
import { handleTree } from '@/utils/tree'
import LotoStationForm from './LotoStationForm.vue'
const router = useRouter() // 路由

defineOptions({ name: 'LotoStation' })

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

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const ids = ref<number[]>([]) // 选中的数据
const multiple = ref(true) // 非多个禁用
const marsOptions = ref([]) // 岗位树选项
const machineryOptions = ref([]) // 设备工艺树选项

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  lotoName: undefined,
  workstationId: undefined,
  lotoId: undefined,
  machineryId:undefined,
})

const queryFormRef = ref() // 搜索的表单

/** 查询锁定站列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await LotoStationApi.listLoto(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,mapId?:number) => {
  formRef.value.open(type, id,mapId)
}

/** 删除按钮操作 */
const handleDelete = async (id?: number) => {
  const lotoIds = id || ids.value
  try {
    await message.delConfirm()
    await LotoStationApi.delLoto(lotoIds)
    message.success(t('common.delSuccess'))
    await getList()
  } catch {}
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: any[]) => {
  ids.value = selection.map(item => item.id)
  multiple.value = !selection.length
}

/** 查看按钮操作 */
const handleView = (row: any) => {
  router.push({
    path: '/dv/lotoStation/LookDetail',
    query: {
      id: row.id
    }
  })
}

/** 初始化 **/
onMounted(async () => {
  await getList()
  // 获取岗位数据
  const deptRes = await MarsDeptApi.listMarsDept({ pageNo: 1, pageSize: -1 })
  marsOptions.value = handleTree(deptRes.list, 'id', 'parentId')

  // 获取设备/工艺数据
  const techRes = await TechnologyApi.listTechnology({ pageNo: 1, pageSize: -1 })
  const data = techRes.list.filter(item => item.machineryType == '工艺')
  machineryOptions.value = handleTree(data, 'id', 'parentId')
})
</script>
