<script setup>
import { adminTabs, maintainTabs } from 'constants/work'
import { getWorkDataListApi } from '@/api/elevator'
import { usePaging } from '@/composables/use-paging'
import { useUniApi } from '@/composables/use-uni-api'
import { useUserStore } from '@/store/user'
const { navigateTo } = useUniApi()

const tabs = ref([])
const currentTab = ref(0)
const currentState = ref(0)
const handleTab = (e) => {
  console.log('handleTab', e)
  const { index, stateList, state } = e
  currentTab.value = index
  currentState.value = state
  query.value.stateList = stateList
  reload()
}

const addHandler = () => {
  isLeave.value = true
  const { id, elevatorNum, enterType } = options.value
  if (enterType === 'hasId') {
    navigateTo(
      `/pages/views/elevator-rescue/create?id=${id}&elevatorNum=${elevatorNum}`
    )
  } else {
    navigateTo(
      '/pages/views/choose-elevator/index?redirect=/pages/views/elevator-rescue/create'
    )
  }
}

const isLeave = ref(false)
const toDetail = ({ id, elevatorNum }) => {
  const queryStr = `id=${id}&elevatorNum=${elevatorNum}`
  switch (currentState.value) {
    case 1:
      navigateTo({
        url: `/pages/views/elevator-rescue/not-started?${queryStr}`,
        events: {
          done: () => reload()
        }
      })
      break
    case 2:
      navigateTo(`/pages/views/elevator-rescue/undone-detail?${queryStr}`)
      break
    default:
      navigateTo(`/pages/views/elevator-rescue/event-detail?${queryStr}`)
      break
  }
}

const query = ref({
  // 1 救援 2维修 3 维保
  workType: 1,
  stateList: '',
  elevatorNum: '',
  elevatorName: ''
})
const { paging, dataList, reload, queryHandler, clearHandler, enterHandler } =
  usePaging({
    apiFn: getWorkDataListApi,
    query
  })

const { isWeibao } = storeToRefs(useUserStore())
const options = ref({})
onLoad(async (e) => {
  options.value = e
  const { elevatorNum } = options.value
  query.value.elevatorNum = elevatorNum || ''

  let currTab
  if (isWeibao.value) {
    [currTab] = tabs.value = maintainTabs
  } else {
    [currTab] = tabs.value = adminTabs
  }
  currentState.value = currTab.state
  query.value.stateList = currTab.stateList
})

onShow(() => {
  if (isLeave.value) {
    reload()
    isLeave.value = false
  }
})
</script>

<template>
  <view class="elevator-rescue" w-full h-100vh flex-col f-s-c bg="#F8F8F8">
    <view w-full>
      <up-tabs
        :current="currentTab"
        :list="tabs"
        :scrollable="false"
        :item-style="{
          width: '50%',
          height: '80rpx'
        }"
        :active-style="{
          color: '#3c9cff',
          fontWeight: 'bold'
        }"
        @click="handleTab"
      />
    </view>

    <view f-c-c mt-8 w-full flex-none bg-white>
      <up-input
        v-model="query.elevatorName"
        placeholder="请输入电梯名称"
        prefix-icon="search"
        prefix-icon-style="font-size: 22px;color: #909399"
        clearable
        :border="`${'none'}`"
        :custom-style="{
          height: '80rpx',
          padding: '0 20rpx'
        }"
        @clear="
          () => {
            query.elevatorName = ''
            clearHandler()
          }
        "
        @confirm="enterHandler"
      />
    </view>

    <view w-full flex-auto overflow-hidden mt-8 bg-white>
      <z-paging
        ref="paging"
        v-model="dataList"
        :fixed="false"
        @query="queryHandler"
      >
        <up-card
          v-for="(item, index) in dataList"
          :key="index"
          :title="item.elevatorName"
          @click="toDetail(item)"
        >
          <template #body>
            <view>
              <view px-30 text-36 font-500 f-c-c justify-between>
                <text v-if="[1].includes(item.state)">未接单</text>
                <text v-else-if="[2, 3].includes(item.state)">进行中</text>
                <text v-else>已完成</text>
              </view>
              <up-cell title="电梯名称" :value="item.elevatorName" />
              <up-cell title="发生时间" :value="item.createdTime" />
              <up-cell title="故障类型" :value="item.description" />
            </view>
            <view v-if="[4, 7].includes(item.state)" mt-20>
              <up-button size="normal" @click.stop="toDetail(item)">
                查看报警详情
              </up-button>
            </view>
          </template>
        </up-card>
      </z-paging>
    </view>
  </view>

  <FloatFixedAddBtn @click="addHandler" />
</template>

<style>
.elevator-rescue .u-tabs {
  background-color: #fff !important;
}
</style>
