<script setup lang="ts" name="LeaveList">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { showConfirmDialog, showSuccessToast } from 'vant'
import { useUserStore } from '@/store/modules/user'
import { listTAmCarOil, delTAmCarApply, delTAmCarOil } from '@/api/carUse/index'
import PullList from '@/component/List/PullList.vue'
import ApplyListItem from './modules/ApplyListItem.vue'

const userStore = useUserStore()
const router = useRouter()

const pullListRef = ref<InstanceType<typeof PullList>>()
const queryParams = ref({
  pageNum: 1,
  pageSize: 5
})

// 请求方法，传入PullList组件，需返回{rows: [], total: number}
function onRequest(query?: any, isRefresh?: boolean): Promise<{rows: any[], total: number}> {
  return listTAmCarOil(query)
}


function onDetail(item) {
  // router.push({ path: '/carUse/oilConsumption/add', query: { id: item.id, type: 'detail' } })
  uni.navigateTo({
    url:'/carUse/oilConsumption/add?id=' + item.id + '&type=detail'
  })
}

// 点击添加
function onClickAdd() {
  // router.push({ path: '/carUse/oilConsumption/add', query: { type: 'add' } })
  uni.navigateTo({
    url:'/carUse/oilConsumption/add?type=add'
  })
}


function onUpdate(item) {
  // router.push({ path: '/carUse/oilConsumption/add', query: { id: item.id, type: 'update' } })
  uni.navigateTo({
    url:'/carUse/oilConsumption/add?id=' + item.id + '&type=update'
  })
}


function onDelete(item) {
  showConfirmDialog({
    message: '确认删除吗？',
    beforeClose: (action) => {
      if (action !== 'confirm') return true
      return new Promise((resolve, reject) => {
        delTAmCarOil(item.id).then(() => {
          resolve(true)
          showSuccessToast('删除成功')
          pullListRef.value?.refresh()
        }).catch(() => {
          reject(false)
        })
      })
    }
  })
}


function onSearch() {
  pullListRef.value?.refresh()
}
</script>

<template>
  <div class="container full">
    <!-- <div class="input-box"> -->
    <!-- <van-search
      v-model="queryParams.reason"
      show-action
      shape="round"
      placeholder="请输入搜索关键词"
      @clear="onSearch"
      @search="onSearch">
      <template #action>
        <div @click="onSearch">搜索</div>
      </template>
    </van-search> -->
    <!-- </div> -->
    <PullList ref="pullListRef" v-model:query-string="queryParams" :request-method="onRequest">
      <template #default="{ item, index }">
        <ApplyListItem
          :item="item"
          :user-info="userStore"
          @onDetail="onDetail"
          @onUpdate="onUpdate"
          @onDelete="onDelete" />
      </template>
    </PullList>
    <van-floating-bubble
      axis="xy"
      icon="plus"
      magnetic="x"
      @click="onClickAdd"
    />
  </div>
</template>

<style lang="less" scoped>
.van-search {
  position: relative;
  z-index: 1;
  padding: 1rem;
  // min-height: 4.4rem;
  border-radius: 24rpx;
  box-shadow: 0 4rpx 40rpx  rgba(64, 103, 251, 0.14);
  // overflow: hidden;
}
</style>
