<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="80px"
    >
      
      <el-form-item label="人员编码" prop="userNo">
        <el-input
          v-model="queryParams.userNo"
          placeholder="请输入人员编码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="班组" prop="classGroup">
        <el-select
          v-model="queryParams.classGroup"
          placeholder="请选择班组"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.TEAM_OR_GROUP)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
        <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-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:user-machine: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" @row-click="handleRowClick" :row-class-name="({ row }) => row.id === selectedRowId ? 'table-row-active' : ''">
      <el-table-column label="序号" align="center" type="index" width="60px" />
      <el-table-column label="人员名称" align="center" prop="userName" />
      <el-table-column label="人员编码" align="center" prop="userNo" />
      <el-table-column label="所属班组" align="center" prop="classGroup">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.TEAM_OR_GROUP" :value="scope.row.classGroup" />
        </template>
      </el-table-column>
       <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:user-machine:update']"
          >
            编辑
          </el-button>
          <!-- <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['biz:user-machine: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>

  <!-- 机台信息列表 -->
  <ContentWrap>
    <div class="font-weight-600 mb-15px">机台信息</div>
    <el-table v-loading="machineLoading" :data="machineList" :show-overflow-tooltip="true" :stripe="true">
      <el-table-column label="序号" align="center" type="index" width="60px" />
      <el-table-column label="机台编码" align="center" prop="machineCd" />
      <el-table-column label="机台名称" align="center" prop="machineName" />
      <el-table-column label="创建人" align="center" prop="creator" />
      <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter2" />
    </el-table>
  </ContentWrap>

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

<script setup lang="ts">
import { getIntDictOptions, getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter2 } from '@/utils/formatTime'
import * as UserMachineApi from '@/api/biz/usermachine'
import UserMachineForm from './UserMachineForm.vue'

defineOptions({ name: 'UserMachine' })

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

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

// 机台信息
const machineLoading = ref(false)
const machineList = ref([])
const selectedRowId = ref(undefined)

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await UserMachineApi.getUserMachinePage(queryParams)
    list.value = data.list
    total.value = data.total
    // 默认选择第一条数据
    if (list.value.length > 0) {
      await getMachineList(list.value[0].id)
    } else {
      // 搜索结果为空时，清空机台列表
      machineList.value = []
      selectedRowId.value = undefined
    }
  } finally {
    loading.value = false
  }
}

/** 查询机台列表 */
const getMachineList = async (id: number) => {
  machineLoading.value = true
  try {
    const data = await UserMachineApi.getUserMachine(id)
    machineList.value = data.machineList || []
    selectedRowId.value = id
  } finally {
    machineLoading.value = false
  }
}

/** 行点击事件 */
const handleRowClick = (row: any) => {
  getMachineList(row.id)
}

/** 搜索按钮操作 */
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 UserMachineApi.deleteUserMachine(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

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

<style scoped>
.table-row-active {
  background-color: #e8f4ff !important;
}
</style>