<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="12">
              <el-form-item :label="t('main.planName')" prop="planTitle">
                <el-input v-model="formData.planTitle" :placeholder="t('main.nameHolder')" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item :label="t('workOrderMaterial.unit')" prop="planUnit">
                <el-select
                  v-model="formData.planUnit"
                  :placeholder="t('route.unitHolder')"
                  :disabled="formType === 'update'"
                  clearable
                >
                  <el-option
                    v-for="dict in getStrDictOptions(DICT_TYPE.PMS_INSPECT_UNIT)"
                    :key="dict.label"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item :label="t('route.cycle')" prop="planCycle">
                <el-input-number
                  style="width: 100%"
                  v-model="formData.planCycle"
                  :precision="0"
                  :min="1"
                  :max="99999"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item :label="t('route.beginCreateTime')" prop="beginCreateTime">
                <el-date-picker
                  style="width: 150%"
                  v-model="formData.beginCreateTime"
                  type="datetime"
                  value-format="x"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="日报填报" prop="isReport">
                <el-radio-group v-model="formData.isReport">
                  <el-radio
                    v-for="dict in getIntDictOptions(DICT_TYPE.RQ_IOT_ISCOLLECTION)"
                    :key="dict.value"
                    :value="dict.value"
                  >
                    {{ dict.label }}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>

              <el-col :span="12">
                <el-form-item :label="t('form.remark')" prop="remark">
                  <el-input type="textarea" v-model="formData.remark" />
                </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" />
              {{ t('route.SelectOperationDevice') }}</el-button
            >
          </el-form-item>
        </el-form>
      </ContentWrap>
      <draggable
        v-model="list"
        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">{{ t('iotDevice.code') }}：</span
                ><span style="font-size: 14px">{{ element.deviceCode }}</span
            >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <span style="font-weight: bold">{{ t('iotDevice.name') }}：</span
              ><span style="font-size: 14px">{{ element.deviceName }}</span>
            </div>
            <div>
              <!--              <el-button type="warning" @click="deleteDraggable(index)">{{ t('form.delete') }}</el-button>-->
              <el-button
                style="vertical-align: middle"
                link
                type="danger"
                @click="handleDelete(element.id,element.deviceId)"
              >
                <Icon style="vertical-align: middle; color: #ea3434" icon="ep:zoom-out" />
                {{ t('form.delete') }}
              </el-button>
            </div>
          </div>
        </template>
      </draggable>
    </ContentWrap>

    <ContentWrap>
      <el-form>
        <el-form-item style="float: right">
          <el-button @click="submitForm" type="primary" :disabled="formLoading">{{ t('iotMaintain.save') }}</el-button>
          <el-button @click="close">{{ t('iotMaintain.cancel') }}</el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>
    <InspectItemList
      ref="inspectItemFormRef"
      :classify="formData.deviceClassify"
      :deviceId="formData.deviceId"
      @choose="inspectItemChoose"
    />
    <PlanDeviceList ref="deviceFormRef" @choose="deviceChoose" />
    <RouteInspectItemDrawer
      ref="showDrawer"
      :model-value="drawerVisible"
      @update:model-value="(val) => (drawerVisible = val)"
    />
  </ContentWrap>
</template>
<script setup lang="ts">
import * as UserApi from '@/api/system/user'
import { useUserStore } from '@/store/modules/user'
import { ref } from 'vue'
import { useTagsViewStore } from '@/store/modules/tagsView'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
import InspectItemList from '@/views/pms/inspect/route/InspectItemList.vue'
import PlanDeviceList from '@/views/pms/iotopeationfill/plan/PlanDeviceList.vue'
import { IotInspectRouteVO } from '@/api/pms/inspect/route'
import { DICT_TYPE, getStrDictOptions,getIntDictOptions } from '@/utils/dict'
import { IotInspectPlanApi, IotInspectPlanVO } from '@/api/pms/inspect/plan'
import RouteInspectItemDrawer from '@/views/pms/inspect/plan/RouteInspectItemDrawer.vue'
import draggable from 'vuedraggable'
import {IotDeviceApi, IotDeviceVO} from "@/api/pms/device";
import {defaultProps,handleTree} from "@/utils/tree";
import * as ProductClassifyApi from "@/api/pms/productclassify";
import {IotInspectItemVO} from "@/api/pms/inspect/item";
import {IotOperationPlanApi, IotOperationPlanVO} from "@/api/pms/iotopeationfill/plan";

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

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const { delView } = useTagsViewStore() // 视图操作
const { currentRoute, push } = useRouter()
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const drawerVisible = ref<boolean>(false)
const showDrawer = ref()
const list = ref<IotInspectRouteVO[]>([]) // 列表的数据
const { params, name } = useRoute() // 查询参数
const id = params.id
const productClassifyList = ref<Tree[]>([]) // 树形结构
const deptUsers = ref<UserApi.UserVO[]>([]) // 用户列表
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const formData = ref({
  id: undefined,
  planTitle: undefined,
  planCode: undefined,
  planCycle: undefined,
  beginCreateTime: undefined,
  planUnit: undefined,
  charge: undefined,
  charges: [],
  deviceIds: undefined,
  remark: undefined,
  deptId: undefined,
  planDevList:[]
})
const formRules = reactive({
  planTitle: [{ required: true, message: '运行计划标题不能为空', trigger: 'blur' }],
  planCode: [{ required: true, message: '运行计划编号不能为空', trigger: 'blur' }],
  planCycle: [{ required: true, message: '周期不能为空', trigger: 'blur' }],
  beginCreateTime: [{ required: true, message: '首次执行时间不能为空', trigger: 'blur' }],
  planUnit: [{ required: true, message: '单位不能为空', trigger: 'blur' }]
  // charges: [{ required: true, message: '负责人不能为空', trigger: 'blur' }]
})
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  deviceCode: undefined,
  deviceName: undefined,
  brand: undefined,
  brandName: undefined,
  model: undefined,
  deptId: undefined,
  deviceStatus: undefined,
  assetProperty: undefined,
  picUrl: undefined,
  remark: undefined,
  manufacturerId: undefined,
  supplierId: undefined,
  manDate: [],
  nameplate: undefined,
  expires: undefined,
  plPrice: undefined,
  plDate: [],
  plYear: undefined,
  plStartDate: [],
  plMonthed: undefined,
  plAmounted: undefined,
  remainAmount: undefined,
  infoId: undefined,
  infoType: undefined,
  infoName: undefined,
  infoRemark: undefined,
  infoUrl: undefined,
  templateJson: undefined,
  creator: undefined,
  sortingFields: [],
  assetClass: undefined,
})
const queryFormRef = ref() // 搜索的表单
/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}
// 添加全选相关状态
const isAllSelected = ref(false) // 是否全选所有数据
const totalSelectedCount = ref(0) // 总选中数量
const deselectedRows = ref([]) // 全选状态下取消选择的行
const allDataCache = ref([])

// 修改表格复选框绑定

const selectedRows = ref<IotDeviceVO[]>([]) // 多选数据（存储所有选中行的数组）
// 修改全选方法，同步更新selectedRows
const selectAll = async () => {
  try {
    // 显示加载状态
    loading.value = true;
    isAllSelected.value = true;
    deselectedRows.value = [];
    selectedRows.value = []; // 清空现有选中项
    allDataCache.value = []; // 清空缓存

    const paramsCopy = { ...queryParams };
    const originalPageNo = paramsCopy.pageNo;
    const originalPageSize = paramsCopy.pageSize;

    paramsCopy.pageNo = 1;
    paramsCopy.pageSize = 100; // 按后台限制的最大页数获取

    // 获取第一页数据
    const firstPageData = await IotDeviceApi.getPlanDevicePage(paramsCopy);
    totalSelectedCount.value = firstPageData.total;
    allDataCache.value.push(...firstPageData.list);

    // 分批获取剩余数据
    if (firstPageData.total > 100) {
      const totalPages = Math.ceil(firstPageData.total / 100);
      const fetchPromises = [];

      for (let page = 2; page <= totalPages; page++) {
        fetchPromises.push(IotDeviceApi.getPlanDevicePage({ ...paramsCopy, pageNo: page }));
      }

      const remainingPages = await Promise.all(fetchPromises);
      remainingPages.forEach(pageData => {
        allDataCache.value.push(...pageData.list);
      });
    }

    // 全选时，将所有数据放入selectedRows
    selectedRows.value = [...allDataCache.value];

    // 恢复原始分页参数
    queryParams.pageNo = originalPageNo;
    queryParams.pageSize = originalPageSize;

    message.success(t('operationFill.allSelected', { count: totalSelectedCount.value }));
  } catch (error) {
    console.error('全选失败', error);
    message.error(t('operationFill.selectAllFailed'));
    isAllSelected.value = false;
    selectedRows.value = [];
  } finally {
    loading.value = false;
  }
};

// 修改取消全选方法，同步清空selectedRows
const deselectAll = () => {
  isAllSelected.value = false;
  deselectedRows.value = [];
  selectedRows.value = []; // 清空所有选中项
  allDataCache.value = []; // 清空缓存
  totalSelectedCount.value = 0;
  message.info(t('operationFill.allDeselected'));
};

// 修改单选逻辑，确保selectedRows正确更新
const selectRow = (row) => {
  if (isAllSelected.value) {
    // 全选状态下的单选操作
    const deselectedIndex = deselectedRows.value.findIndex(
      item => item.id === row.id && item.deviceCode === row.deviceCode
    );

    const selectedIndex = selectedRows.value.findIndex(
      item => item.id === row.id && item.deviceCode === row.deviceCode
    );

    if (deselectedIndex > -1) {
      // 从取消列表中移除，添加到选中列表
      deselectedRows.value.splice(deselectedIndex, 1);
      if (selectedIndex === -1) {
        selectedRows.value.push({ ...row });
      }
    } else {
      // 从选中列表中移除，添加到取消列表
      if (selectedIndex > -1) {
        selectedRows.value.splice(selectedIndex, 1);
      }
      deselectedRows.value.push({ ...row });
    }
  } else {
    // 非全选状态下的单选操作
    const index = selectedRows.value.findIndex(
      item => item.id === row.id && item.deviceCode === row.deviceCode
    );

    if (index > -1) {
      // 取消选择
      selectedRows.value.splice(index, 1);
    } else {
      // 选中
      selectedRows.value.push({ ...row });
    }
  }
};



// 生成行唯一标识
const rowKey = (row: any) => {
  return `${row.id}`; // 确保行更新时重新渲染
};

// 修改移除标签方法，确保selectedRows同步更新
const removeTag = (route) => {
  // 从选中列表中移除
  const selectedIndex = selectedRows.value.findIndex(
    item => item.id === route.id && item.deviceCode === route.deviceCode
  );

  if (selectedIndex > -1) {
    selectedRows.value.splice(selectedIndex, 1);
  }

  // 全选状态下还要添加到取消列表
  if (isAllSelected.value) {
    const deselectedIndex = deselectedRows.value.findIndex(
      item => item.id === route.id && item.deviceCode === route.deviceCode
    );

    if (deselectedIndex === -1) {
      deselectedRows.value.push({ ...route });
    }
  }

  // 刷新表格
  list.value = [...list.value];
};
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await IotDeviceApi.getPlanDevicePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
// 拖动状态管理
const items = ref([])
const deviceChoose = (rows) => {
  debugger
  rows.forEach((it) => {
    const ifExist = list.value.find((item) => item.id === it.id&&item.deviceId===it.deviceId)
    if (!ifExist) {
      list.value.push(it)
      console.log(JSON.stringify(it))
    }
  })
  // list.value = rows
}
const dragStart = () => {
  document.body.style.cursor = 'grabbing'
}
const dragEnd = (event) => {
  document.body.style.cursor = ''
  console.log(
    '新顺序:',
    items.value.map((c) => c.id)
  )
  console.log('拖拽后的新位置:', event.newIndex + 1)
}
const viewRoute = (itemJson) => {
  drawerVisible.value = true
  showDrawer.value.openDrawer(JSON.parse(itemJson))
}
const formRef = ref() // 表单 Ref
const inspectItemChoose = (rows) => {
  items.value = []
  items.value = rows
}
const deviceFormRef = ref()
const openForm = () => {
  deviceFormRef.value.open()
}

const close = () => {
  delView(unref(currentRoute))
  push({ name: 'iotOperationPlan', params: {} })
}
const itemsWithIndex = computed(() => {
  return list.value.map((item, index) => ({
    ...item,
    index: index + 1 // 序号从1开始
  }))
})
const { wsCache } = useCache()
/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    formData.value.planDevList = list;
    const user = wsCache.get(CACHE_KEY.USER)
    const data = formData.value as unknown as IotOperationPlanVO
    if (formType.value === 'create') {
      //校验是否是已有计划
      const newitems = itemsWithIndex
      formData.value.deviceIds = JSON.stringify(newitems.value)
      if (!formData.value.deptId) {formData.value.deptId = user.user.deptId}
      await IotOperationPlanApi.createIotOperationPlan(data).then((res) => {
      })
      message.success(t('common.createSuccess'))
      close()
    } else {
      const newitems = itemsWithIndex
      formData.value.deviceIds = JSON.stringify(newitems.value)
      await IotOperationPlanApi.updateIotOperationPlan(data)
      message.success(t('common.updateSuccess'))
      close()
    }
    //创建定时任务
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
onMounted(async () => {

  const route = useRoute()
  const deptId = useUserStore().getUser.deptId
  formData.value.deptId = Number(route.query.deptId)
  if (route.query.deptName) {
    formData.value.planTitle = `${route.query.deptName} - 运行计划`
  }
  deptUsers.value = await UserApi.getDeptUsersByDeptId(deptId)
  if (id) {
    formType.value = 'update'
    const iotInspectPlan = await IotOperationPlanApi.getIotOperationPlan(id)
    formData.value = iotInspectPlan
    list.value = JSON.parse(iotInspectPlan.deviceIds)
  } else {
    formType.value = 'create'
  }
  productClassifyList.value = handleTree(
    await ProductClassifyApi.IotProductClassifyApi.getSimpleProductClassifyList()
  )
})
const handleDelete = async (id: number, deviceId: number) => {
  try {
    debugger
    const index = list.value.findIndex((item) => item.id === id&&item.deviceId===deviceId)
    if (index !== -1) {
      // 通过 splice 删除元素
      list.value.splice(index, 1)
    }
  } catch {}
}
</script>
<style scoped>
.base-expandable-content {
  overflow: hidden; /* 隐藏溢出的内容 */
  transition: max-height 0.3s ease; /* 平滑过渡效果 */
}
/* 横向布局容器 */


/* 拖拽手柄样式 */
.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>
