<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="隔离点名称" prop="pointName">
        <el-input
          v-model="queryParams.pointName"
          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="deptOptions"
          :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 label="锁定站" prop="lotoId">
        <el-select
          v-model="queryParams.lotoId"
          placeholder="请选择锁定站"
          class="!w-240px"
        >
          <el-option
            v-for="dict in lotoOptions"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="能量源" prop="powerType">
        <el-select
          v-model="queryParams.powerType"
          placeholder="请选择能量源"
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.POWER_TYPE)"
            :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="['iscs:point:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="danger"
          plain
          :disabled="multiple"
          @click="handleDelete()"
          v-hasPermi="['iscs:point: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="id" width="100"/>

      <el-table-column label="隔离点名称" align="center" prop="pointName" />
      <el-table-column label="隔离点图标" align="center" prop="pointIcon" width="100">
        <template #default="scope">
          <el-image
            v-if="scope.row.pointIcon"
            style="width: 50px; height: 50px"
            :src="scope.row.pointIcon"
            :preview-src-list="[scope.row.pointIcon]"
          />
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="开关状态" align="center" prop="switchStatus">
        <template #default="scope">
          <el-switch
            style="pointer-events: none"
            v-if="scope.row.switchStatus!==null"
            v-model="scope.row.switchStatus"
            active-value="1"
            inactive-value="0"
            active-text="ON"
            inactive-text="OFF"
            active-color="#13ce66"
            inactive-color="#ff4949"
          />
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="隔离点NFC" align="center" prop="pointNfc" />
      <el-table-column label="岗位" align="center" prop="workstationName" />
      <el-table-column label="设备/工艺" align="center" prop="machineryName" />
      <el-table-column label="锁定站" align="center" prop="lotoName" />
      <el-table-column label="隔离点序列号" align="center" prop="pointSerialNumber" />
      <el-table-column label="作用" align="center" prop="remark" />
      <el-table-column label="隔离点图片" align="center" prop="pointPicture" width="100">
        <template #default="scope">
          <el-image
            v-if="scope.row.pointPicture"
            style="width: 50px; height: 50px"
            :src="scope.row.pointPicture"
            :preview-src-list="[scope.row.pointPicture]"
          />
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="能量源" align="center" prop="powerType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.POWER_TYPE" :value="scope.row.powerType" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="120">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['iscs:point:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['iscs:point: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>

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

<script lang="ts" setup>
import {DICT_TYPE, getStrDictOptions} from '@/utils/dict'
import * as SegregationPointApi from '@/api/dv/spm/index'
import SegregationPointForm from './SegregationPointForm.vue'
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'
defineOptions({ name: 'SegregationPoint' })

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 deptOptions = ref([]) // 部门树选项
const machineryOptions = ref([]) // 工艺树选项
const lotoOptions = ref([]) // 锁定站选项


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

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

/** 查询隔离点列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await SegregationPointApi.getIsIsolationPointPage(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) => {
  const pointIds = id || ids.value
  try {
    await message.delConfirm()
    await SegregationPointApi.deleteIsIsolationPointByPointIds(pointIds)
    message.success(t('common.delSuccess'))
    await getList()
  } catch {}
}

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


/** 初始化 **/
onMounted(async () => {
  await getList()
  // 获取岗位数据
  const deptRes = await MarsDeptApi.listMarsDept({ pageNo: 1, pageSize: -1 })
  deptOptions.value = handleTree(deptRes.list, 'id', 'parentId')
  // 获取锁定站数据
  const lotoRes = await LotoStationApi.listLoto({ pageNo: 1, pageSize: -1})
  lotoOptions.value = lotoRes.list.map(item => ({
    value: item.id,
    label: item.lotoName
  }))
  // 获取设备/工艺数据
  const techRes = await TechnologyApi.listTechnology({ pageNo: 1, pageSize: -1 })
  const data = techRes.list.filter(item => item.machineryType == '工艺')
  machineryOptions.value = handleTree(data, 'id', 'parentId')

})
</script>
