<template>
  <ContentWrap v-loading="formLoading">
    <ContentWrap>
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        style="margin-right: 4em; margin-left: 0.5em; margin-top: 1em"
        label-width="130px"
      >
        <div class="base-expandable-content">
          <el-row>
            <el-col :span="8">
              <el-form-item label="路线名称" prop="routeName">
                <el-input v-model="formData.routeName" placeholder="路线名称" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="设备类别" prop="deviceClassify">
                <el-tree-select
                  filterable
                  clearable
                  v-model="formData.deviceClassify"
                  :data="productClassifyList"
                  :props="defaultProps"
                  check-strictly
                  node-key="id"
                  placeholder="请选择设备类别"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="设备" prop="deviceName">
                <el-input
                  v-model="formData.deviceName"
                  @clear="handleClear"
                  placeholder="请选择设备"
                  @click="openDevice"
                  clearable
                />
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </el-form>
    </ContentWrap>
    <ContentWrap>
      <!-- 列表 -->
      <ContentWrap>
        <ContentWrap>
          <el-form
            class="-mb-15px"
            :model="queryParams"
            ref="queryFormRef"
            :inline="true"
            label-width="68px"
          >
            <el-form-item>
              <el-button @click="openForm" type="primary"
                ><Icon icon="ep:plus" class="mr-5px" /> 选择巡检项</el-button
              >
            </el-form-item>
          </el-form>
        </ContentWrap>
        <draggable
          v-model="items"
          item-key="id"
          tag="div"
          class="sortable-container"
          handle=".sortable-item"
          :animation="150"
          @start="dragStart"
          @end="dragEnd"
        >
          <template #item="{ element, index }">
            <div class="sortable-item">
              <!-- 序号显示 -->
              <div class="order-number">{{ index + 1 }}</div>

              <!-- 拖动手柄 -->
              <span class="drag-handle">≡</span>

              <!-- 组件内容 -->
              <div class="component-content">
                <span style="font-weight: bold">巡检项：</span
                ><span style="font-size: 14px">{{ element.item }}</span
                >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <span style="font-weight: bold">巡检标准：</span
                ><span style="font-size: 14px">{{ element.standard }}</span>
              </div>
              <div>
                <el-button type="warning" @click="deleteDraggable(index)">删除</el-button>
              </div>
            </div>
          </template>
        </draggable>
      </ContentWrap>
    </ContentWrap>
    <ContentWrap>
      <el-form>
        <el-form-item style="float: right">
          <el-button @click="submitForm" type="primary" :disabled="formLoading">保 存</el-button>
          <el-button @click="close">取 消</el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>
    <InspectItemList
      ref="inspectItemFormRef"
      @choose="inspectItemChoose"
    />
    <DeviceList ref="deviceFormRef" @choose="deviceChoose" />
  </ContentWrap>
</template>
<script setup lang="ts">
import * as UserApi from '@/api/system/user'
import { useUserStore } from '@/store/modules/user'
import { ref } from 'vue'
import { IotMaintainMaterialVO } from '@/api/pms/maintain/material'
import { useTagsViewStore } from '@/store/modules/tagsView'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
import { defaultProps, handleTree } from '@/utils/tree'
import * as ProductClassifyApi from '@/api/pms/productclassify'
import draggable from 'vuedraggable'
import InspectItemList from '@/views/pms/inspect/route/InspectItemList.vue'
import DeviceList from '@/views/pms/failure/DeviceList.vue'
import { IotInspectRouteApi, IotInspectRouteVO } from '@/api/pms/inspect/route'

/** 维修工单 表单 */
defineOptions({ name: 'RouteAdd' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const { delView } = useTagsViewStore() // 视图操作
const { currentRoute, push } = useRouter()
const deptUsers = ref<UserApi.UserVO[]>([]) // 用户列表
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const deviceLabel = ref('') // 表单的类型：create - 新增；update - 修改
const drawerVisible = ref<boolean>(false)
const showDrawer = ref()
const list = ref<IotMaintainMaterialVO[]>([]) // 列表的数据
const { params, name } = useRoute() // 查询参数
const id = params.id
const productClassifyList = ref<Tree[]>([]) // 树形结构
const formData = ref({
  id: undefined,
  routeName: undefined,
  deviceClassify: undefined,
  deviceId: undefined,
  deviceClassifyName: undefined,
  deviceName: undefined,
  itemJson: undefined,
  remark: undefined,
  deptId: undefined,
  deviceCode: undefined
})
const handleClear = () => {
  formData.value.deviceId = undefined
  formData.value.deviceName = undefined
  formData.value.deptId = undefined
  formData.value.deviceCode = undefined
}
// 拖动状态管理
const dragStart = () => {
  document.body.style.cursor = 'grabbing'
}
const deleteDraggable = async (index) => {
  await message.delConfirm()
  items.value.splice(index, 1)
}
const dragEnd = (event) => {
  document.body.style.cursor = ''
  console.log(
    '新顺序:',
    items.value.map((c) => c.id)
  )
  console.log('拖拽后的新位置:', event.newIndex + 1)
}
const items = ref([])
const deviceChoose = (row) => {
  formData.value.deviceId = row.id
  formData.value.deviceName = row.deviceName
  formData.value.deptId = row.deptId
  formData.value.deviceCode = row.deviceCode
  //deviceLabel.value = row.deviceName
}
const formRules = reactive({
  routeName: [{ required: true, message: '路线名称不能为空', trigger: 'blur' }],
  deviceClassify: [{ required: true, message: '设备类别不能为空', trigger: 'blur' }]
})

const formRef = ref() // 表单 Ref
const inspectItemChoose = (rows) => {
  rows.forEach((it) => {
    const ifExist = items.value.find((item) => item.id === it.id)
    if (!ifExist) {
      items.value.push(it)
      console.log(JSON.stringify(it))
    }
  })
  console.log(JSON.stringify(items.value))
  // items.value = rows
}
const inspectItemFormRef = ref()
const openForm = () => {
  if (formData.value.deviceClassify === undefined) {
    message.error('请选择设备类别')
    return
  }
  inspectItemFormRef.value.open(formData.value.deviceClassify, formData.value.deviceId)
}
const deviceFormRef = ref()
const openDevice = () => {
  deviceFormRef.value.open(formData.value.deviceClassify)
}

const close = () => {
  delView(unref(currentRoute))
  push({
    name: 'IotInspectRoute',
    query: {
      date: new Date().getTime()
    }
  })
}
const itemsWithIndex = computed(() => {
  return items.value.map((item, index) => ({
    itemId: item.id,
    item: item.item,
    standard: item.standard,
    index: index + 1 // 序号从1开始
  }))
})
const itemsWithIndexUpdate = computed(() => {
  return items.value.map((item, index) => ({
    itemId: item.itemId,
    item: item.item,
    standard: item.standard,
    index: index + 1 // 序号从1开始
  }))
})
const { wsCache } = useCache()
/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    // const user = wsCache.get(CACHE_KEY.USER)
    // formData.value.deptId = user.user.deptId
    const data = formData.value as unknown as IotInspectRouteVO
    if (formType.value === 'create') {
      const newitems = itemsWithIndex
      formData.value.itemJson = JSON.stringify(newitems.value)
      await IotInspectRouteApi.createIotInspectRoute(data)
      message.success(t('common.createSuccess'))
      close()
    } else {
      const newitems = itemsWithIndexUpdate
      formData.value.itemJson = JSON.stringify(newitems.value)
      await IotInspectRouteApi.updateIotInspectRoute(data)
      message.success(t('common.updateSuccess'))
      close()
    }
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
onMounted(async () => {
  // const deptId = useUserStore().getUser.deptId
  // deptUsers.value = await UserApi.getDeptUsersByDeptId(deptId)
  if (id) {
    formType.value = 'update'
    const iotInspectRoute = await IotInspectRouteApi.getIotInspectRoute(id)
    deviceLabel.value = iotInspectRoute.deviceName
    formData.value = iotInspectRoute
    items.value = JSON.parse(iotInspectRoute.itemJson)
  } else {
    formData.value.type = 'in'
    formType.value = 'create'
    const { wsCache } = useCache()
    const userInfo = wsCache.get(CACHE_KEY.USER)
    formData.value.maintainPerson = userInfo.user.id
  }
  productClassifyList.value = handleTree(
    await ProductClassifyApi.IotProductClassifyApi.getSimpleProductClassifyList()
  )
})
const handleDelete = async (id: number) => {
  try {
    const index = list.value.findIndex((item) => item.code === id)
    if (index !== -1) {
      // 通过 splice 删除元素
      list.value.splice(index, 1)
    }
  } catch {}
}
</script>
<style scoped>
.base-expandable-content {
  overflow: hidden; /* 隐藏溢出的内容 */
  transition: max-height 0.3s ease; /* 平滑过渡效果 */
}
/* 横向布局容器 */
.horizontal-list {
  display: flex;
  gap: 16px;
  padding: 20px;
  overflow-x: auto;
  flex-wrap: nowrap; /* 禁止换行:ml-citation{ref="5" data="citationList"} */
}

/* 拖拽项样式 */
.horizontal-item {
  flex: 0 0 auto;
  min-width: 150px;
  padding: 20px;
  background: #d3a137;
  border: 2px solid #e0e0e0;
  border-radius: 8px;
  cursor: move;
  user-select: none; /* 防止文字选中:ml-citation{ref="7" data="citationList"} */
  transition:
    transform 0.3s,
    box-shadow 0.3s;
  display: flex;
  align-items: center;
  gap: 10px;
}

/* 拖拽手柄样式 */
.drag-handle {
  opacity: 0.5;
  cursor: move;
  transition: opacity 0.3s;
}
.drag-handle:hover {
  opacity: 1;
}

/* 拖拽时的悬停效果 */
.horizontal-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

/* 滚动条样式 */
.horizontal-list::-webkit-scrollbar {
  height: 8px;
}
.horizontal-list::-webkit-scrollbar-thumb {
  background: #888;
  border-radius: 4px;
}

.sortable-container {
  cursor: move;
  display: flex;
  flex-direction: column;
  gap: 9px;
  //max-height: 80vh;
  overflow-y: auto;
  padding: 7px;
}

.sortable-item {
  display: flex;
  align-items: center;
  padding: 8px;
  background: #fff;
  border: 1px solid #ebeef5;
  border-radius: 8px;
  transition:
    transform 0.3s,
    box-shadow 0.3s;
  user-select: none;
  height: 50%;
}

.sortable-item:hover {
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.order-number {
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #409eff;
  color: white;
  border-radius: 50%;
  margin-right: 7px;
  font-weight: bold;
}

.drag-handle {
  padding: 0 12px;
  opacity: 0.4;
  transition: opacity 0.3s;
}

.drag-handle:hover {
  opacity: 1;
}

.component-content {
  flex: 1;
  min-width: 0;
}

/* 优化滚动条 */
.sortable-container::-webkit-scrollbar {
  width: 8px;
}
.sortable-container::-webkit-scrollbar-thumb {
  background: #c0c4cc;
  border-radius: 4px;
}
</style>
