<script lang="ts" setup>
import { IotRhDailyReportApi } from '@/api/pms/iotrhdailyreport'
import { useUserStore } from '@/store/modules/user'
import { rangeShortcuts } from '@/utils/formatTime'
import { useDebounceFn } from '@vueuse/core'
import dayjs from 'dayjs'
import rhForm from './rh-form.vue'
import RhTable from './rh-table.vue'

defineOptions({ name: 'IotRhDailyReportFill' })

const id = useUserStore().getUser.deptId

const deptId = id

interface Query {
  pageNo: number
  pageSize: number
  deptId?: number
  contractName?: string
  taskName?: string
  createTime?: string[]
}

const initQuery: Query = {
  pageNo: 1,
  pageSize: 10,
  deptId: id,
  createTime: [
    ...rangeShortcuts[2].value().map((item) => dayjs(item).format('YYYY-MM-DD HH:mm:ss'))
  ]
}

const query = ref<Query>({ ...initQuery })

const list = ref<any[]>([])
const total = ref(0)

const loading = ref(false)

const loadList = useDebounceFn(async function () {
  loading.value = true
  try {
    const data = await IotRhDailyReportApi.getIotRhDailyReportPage(query.value)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
})

function handleSizeChange(val: number) {
  query.value.pageSize = val
  handleQuery()
}

function handleCurrentChange(val: number) {
  query.value.pageNo = val
  loadList()
}

function handleQuery(setPage = true) {
  if (setPage) {
    query.value.pageNo = 1
  }
  loadList()
}

function resetQuery() {
  query.value = { ...initQuery }

  handleQuery()
}

watch(
  [
    () => query.value.deptId,
    () => query.value.contractName,
    () => query.value.taskName,
    () => query.value.createTime
  ],
  () => {
    handleQuery()
  },
  { immediate: true }
)

const visible = ref(false)

const formRef = ref()

function handleOpenForm(id: number, type: 'edit' | 'readonly') {
  if (formRef.value) {
    formRef.value.handleOpenForm(id, type)
  }
}

const route = useRoute()

onMounted(() => {
  if (Object.keys(route.query).length > 0) {
    handleOpenForm(Number(route.query.id), 'edit')
  }
})
</script>

<template>
  <div
    class="grid grid-cols-[auto_1fr] grid-rows-[62px_1fr] gap-4 h-[calc(100vh-20px-var(--top-tool-height)-var(--tags-view-height)-var(--app-footer-height))]"
  >
    <DeptTreeSelect
      :top-id="157"
      :deptId="deptId"
      v-model="query.deptId"
      :show-title="false"
      class="row-span-2"
    />
    <!-- <div class="p-4 bg-white dark:bg-[#1d1e1f] shadow rounded-lg row-span-2"> </div> -->
    <el-form
      size="default"
      class="bg-white dark:bg-[#1d1e1f] rounded-lg shadow px-8 gap-8 flex items-center justify-between"
    >
      <div class="flex items-center gap-8">
        <el-form-item label="项目">
          <el-input
            v-model="query.contractName"
            placeholder="请输入项目"
            clearable
            @keyup.enter="handleQuery()"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="任务">
          <el-input
            v-model="query.taskName"
            placeholder="请输入任务"
            clearable
            @keyup.enter="handleQuery()"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker
            v-model="query.createTime"
            value-format="YYYY-MM-DD HH:mm:ss"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :shortcuts="rangeShortcuts"
            :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
            class="!w-220px"
          />
        </el-form-item>
      </div>
      <el-form-item>
        <el-button type="primary" @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-form-item>
    </el-form>

    <rh-table
      :list="list"
      :total="total"
      :loading="loading"
      :page-no="query.pageNo"
      :page-size="query.pageSize"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    >
      <template #action="{ row }">
        <el-button
          link
          type="success"
          @click="handleOpenForm(row.id, 'readonly')"
          v-hasPermi="['pms:iot-rh-daily-report:query']"
        >
          查看
        </el-button>
        <el-button
          v-show="row.status === 0"
          link
          type="primary"
          @click="handleOpenForm(row.id, 'edit')"
          v-hasPermi="['pms:iot-rh-daily-report:create']"
        >
          编辑
        </el-button>
      </template>
    </rh-table>
  </div>
  <rh-form v-model:visible="visible" type="edit" ref="formRef" :load-list="loadList" />
</template>

<style scoped>
:deep(.el-form-item) {
  margin-bottom: 0;
}
</style>
