<script setup lang="ts" name="LeaveList">
import { ref } from 'vue'
import { storeToRefs } from 'pinia'
import { useRouter } from 'vue-router'
import { showConfirmDialog, showSuccessToast } from 'vant'
import { useUserStore } from '@/store/modules/user'
import { useDict } from '@/common/utils/dict'
import { getBusinessTAmCarApplylist, delTAmCarApply } from '@/api/carUse/index'
import { revokeProcess } from '@/api/flowEngine/flowInterface'
import PullList from '@/component/List/PullList.vue'
import ApplyListItem from './modules/ApplyListItem.vue'

const { audit_status, att_trip_type } = useDict('audit_status', 'att_trip_type')

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

const { userInfo } = storeToRefs(userStore)
const pullListRef = ref<InstanceType<typeof PullList>>()
const queryParams = ref({
  pageNum: 1,
  pageSize: 5,
  createBy: userInfo.value.userId
})

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


function onClickItem(item) {
  // 已退回 和 已撤回
  if (['5', '6'].includes(item.auditStatus)) {
    // router.push({ path: '/carUse/apply/add', query: { id: item.id } })
      uni.navigateTo({
        url:'/carUse/apply/add?id=' + item.id
      })
    return
  }
  // router.push({ path: '/carUse/apply/detail', query: { id: item.id, instanceId: item.instanceId } })
  uni.navigateTo({
    url:'/carUse/apply/detail?id=' + item.id+ '&instanceId='+item.instanceId
  })
}


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


// 外出撤办
function onRevoke(item) {
  showConfirmDialog({
    title: '撤办',
    message: `用车事由：${item.applyReason}，确认撤办吗？`,
    beforeClose: (action) => {
      if (action !== 'confirm') return true
      return new Promise((resolve, reject) => {
        revokeProcess({
          instanceId: item.instanceId
        }).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"
          :audit_status="audit_status"
          @onRevoke="onRevoke"
          @click="onClickItem(item)"
        />
      </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>
