<template>
  <el-row :gutter="20">
    <el-col :span="4" :xs="24">
      <ContentWrap class="h-1/1">
        <DeptTree @node-click="handleDeptNodeClick" />
      </ContentWrap>
    </el-col>
    <el-col :span="20" :xs="24">
      <ContentWrap>
        <!-- 搜索工作栏 -->
        <el-form
          class="-mb-15px"
          :model="queryParams"
          ref="queryFormRef"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="查询条件" prop="orderName">
            <el-input
              v-model="queryParams.orderName"
              placeholder="请输入查询条件"
              clearable
              @keyup.enter="handleQuery"
              class="!w-240px"
            />
          </el-form-item>
          <el-form-item label="创建时间" prop="createTime">
            <el-date-picker
              v-model="queryParams.createTime"
              value-format="YYYY-MM-DD HH:mm:ss"
              type="daterange"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
              class="!w-220px"
            />
          </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="success"
              plain
              @click="handleExport"
              :loading="exportLoading"
              v-hasPermi="['rq:iot-opeation-fill:export']"
            >
              <Icon icon="ep:download" 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">
          <el-table-column :label="t('iotDevice.serial')" width="70" align="center">
            <template #default="scope">
              {{ scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column label="工单名称" align="center" prop="orderName" />
          <el-table-column label="所属组织" align="center" prop="deptName" />
          <el-table-column label="设备负责人" align="center" prop="nickname" />
          <el-table-column label="设备编号" align="center" prop="deviceCode" />
          <el-table-column label="设备名称" align="center" prop="deviceName" />
          <el-table-column label="是否填写" align="center" prop="isFill">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.OPERATION_FILL_DEVICE_STATUS" :value="scope.row.isFill" />
            </template>
          </el-table-column>
          <el-table-column
            label="创建时间"
            align="center"
            prop="createTime"
            :formatter="dateFormatter"
            width="180px"
          />
        </el-table>
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </ContentWrap>
    </el-col>
  </el-row>


</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { IotOpeationFillApi, IotOpeationFillVO } from '@/api/pms/iotopeationfill'
import IotOpeationFillForm from './IotOpeationFillForm.vue'
import * as UserApi from "@/api/system/user";
import {defaultProps,handleTree} from "@/utils/tree";
import * as DeptApi from "@/api/system/dept";
import * as PostApi from "@/api/system/post";
import {onMounted, ref} from "vue";
import {DeptTreeItem} from "@/api/system/dept";
import DeptTree from "@/views/system/user/DeptTree.vue";
import {useUserStore} from "@/store/modules/user";
import {DICT_TYPE, getStrDictOptions} from "@/utils/dict";
/** 运行记录填报 列表 */
defineOptions({ name: 'StatisticsForm' })
const deptList = ref<DeptTreeItem[]>([]) // 树形结构部门列表
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<IotOpeationFillVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const { params, name } = useRoute() // 查询参数
const deptId = params.deptId;
const isFill = params.isFill;
const createTime = params.createTime;
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const selectedDeptId = ref(null);  // 通过store存储的部门id 由父组件传递过来

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  deviceCode: undefined,
  deviceName: undefined,
  fillContent: undefined,
  deviceType: undefined,
  deviceComponent: undefined,
  deptId: undefined,
  orgName: undefined,
  proId: undefined,
  proName: undefined,
  teamId: undefined,
  teamName: undefined,
  dutyName: undefined,
  creDate: [],
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const handleDeptNodeClick = async (row) => {
  queryParams.deptId = row.id
  await getList()
}
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await IotOpeationFillApi.getDeviceData(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}


/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = type
  formType.value = type
  queryParams.deptId = id;
  // 修改时，设置数据
  if (id) {
    getList();
  }

}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryParams.deptId = useUserStore().getUser.deptId;
  queryParams.createTime = null
  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 IotOpeationFillApi.deleteIotOpeationFill(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await IotOpeationFillApi.exportIotOpeationFill(queryParams)
    download.excel(data, '运行记录填报.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(async () => {
  if (deptId) {
    queryParams.deptId = deptId;
  }
  if (isFill == 2) {
    queryParams.isFill = null;
  } else {
    if (isFill) {
      queryParams.isFill = isFill;
    }
  }
  if(createTime){
    queryParams.createTime = createTime;
  }
  getList()
  deptList.value = handleTree(await DeptApi.getSimpleDeptList());
})
</script>
