<template>
  <view class="">
    <com-empty-view v-if="jobList.length == 0"></com-empty-view>
    <job-filter ref="filter" otherTitle="ASN" @switchChangeToday="switchChangeToday" @switchChangeWait="switchChangeWait" @onScanNumber="getScanNumber" :checkedToday="checkedToday" :checkedWaitTask="checkedWaitTask"> </job-filter>
    <view v-if="jobList.length > 0" class="u-p-20">
      <u-swipe-action :show="item.show" :index="index" v-for="(item, index) in jobList" :key="index" :options="item.status == '2' ? detailGiveupOptions : detailOptions" bg-color="rgba(255,255,255,0)" class="u-m-b-20" @click="swipeClick">
        <com-inventory-job-card :dataContent="item" @click="openJobDetail(item)"> </com-inventory-job-card>
      </u-swipe-action>
      <job-list-popup ref="jobListPopupRef" @selectedItem="selectedItem"></job-list-popup>
      <job-info-popup ref="jobInfoPopupRef"></job-info-popup>
      <u-loadmore :status="loadingType" v-if="jobList.length > 0" />
    </view>
    <com-message ref="comMessageRef" />
  </view>
</template>

<script setup lang="ts">
import { ref, getCurrentInstance, onMounted, nextTick, watch } from 'vue'
import { getInventoryMoveJobList, cancleTakeInventoryMoveJob } from '@/api/request2.js'

import { goHome, getCurrDate } from '@/common/basic.js'

import { getDetailOption, getDetailGiveupOption } from '@/common/array.js'

import comEmptyView from '@/mycomponents/common/comEmptyView.vue'
import jobFilter from '@/mycomponents/job/jobFilter.vue'

import comInventoryJobCard from '@/pages/inventoryMove/coms/comInventoryJobCard.vue'
import jobListPopup from '@/pages/inventoryMove/coms/jobListPopup.vue'
import jobInfoPopup from '@/pages/inventoryMove/coms/jobInfoPopup.vue'

const { proxy } = getCurrentInstance()
const props = defineProps({
  businessTypeCode: {
    type: String,
    default: 'Move'
  } // 业务类型
})

const jobList = ref([])
const pageNo = ref(1)
const pageSize = ref(10)
const totalCount = ref(0)
const loadingType = ref('nomore')
const checkedToday = ref(false)
const checkedWaitTask = ref(false)
const todayTime = ref('')
const status = ref('1,2') // 待处理 、进行中
const detailOptions = ref([])
const detailGiveupOptions = ref([])
const filter = ref()
const comMessageRef = ref()
const jobInfoPopupRef = ref()
const jobListPopupRef = ref()
onMounted(() => {
  nextTick(() => {
    detailOptions.value = getDetailOption()
    detailGiveupOptions.value = getDetailGiveupOption()
    // refresh()
  })
})

const updateTitle = () => {
  let title = ''
  let name = ''
  if (props.businessTypeCode == 'Move') {
    name = '库存转移'
  } else if (props.businessTypeCode == 'HoldToOk') {
    name = '隔离转合格'
  } else if (props.businessTypeCode == 'HoldToScrap') {
    name = '隔离转报废'
  } else if (props.businessTypeCode == 'OkToHold') {
    name = '合格转隔离'
  } else if (props.businessTypeCode == 'OktoScrap') {
    name = '合格转报废'
  } else if (props.businessTypeCode == 'ScrapToHold') {
    name = '报废转隔离'
  }
  // titleName.value = name
  if (totalCount.value > 0) {
    title = `${name}(${totalCount.value})`
  } else {
    title = name
  }
  uni.setNavigationBarTitle({
    title
  })
}

const getList = (type) => {
  proxy.$modal.loading('加载中­....')
  loadingType.value = 'loading'
  if (type === 'refresh') {
    pageNo.value = 1
    jobList.value = []
  }

  const filters = []
  if (checkedToday.value) {
    filters.push({
      column: 'create_time',
      action: 'betweeen',
      value: todayTime.value
    })
  }
  filters.push({
    column: 'status',
    action: 'in',
    value: status.value
  })

  filters.push({
    column: 'business_type',
    action: '==',
    value: props.businessTypeCode
  })

  const params = {
    filters,
    pageNo: pageNo.value,
    pageSize: pageSize.value
  }
  getInventoryMoveJobList(params)
    .then((res) => {
      uni.hideLoading()
      if (type === 'refresh') {
        uni.stopPullDownRefresh()
      }

      const { list } = res.data
      totalCount.value = res.data.total
      loadingType.value = 'loadmore'
      if (list == null || list.length == 0) {
        loadingType.value = 'nomore'
        return
      }
      jobList.value = type === 'refresh' ? list : jobList.value.concat(list)
      console.log(jobList.value)
      pageNo.value++
      updateTitle()
    })
    .catch((error) => {
      if (type === 'refresh') {
        uni.stopPullDownRefresh()
      }
      loadingType.value = ''
      uni.hideLoading()
      showMessage(error)
    })
}

const openJobDetail = (item) => {
  uni.navigateTo({
    url: `./inventoryMoveDetail?id=${item.masterId}&status=${item.status}&businessTypeCode=${props.businessTypeCode}`
  })
}

const showItemList = (itemList) => {
  comReceiptJobList.value.openPopup(itemList)
}

const selectedItem = (item) => {
  openJobDetail(item)
}
const swipeClick = (index, index1) => {
  // var text = clearTirmAndWrap(requestList.value[index].options[index].text)
  let text = ''
  const dataContent = jobList.value[index]
  if (dataContent.status == '2') {
    text = detailGiveupOptions.value[index1].text
  } else {
    text = detailOptions.value[index1].text
  }
  if (text == '详情') {
    openjobInfoPopup(dataContent)
  } else if (text == '放弃') {
    comMessageRef.value.showQuestionMessage('确定要放弃当前任务?', (res) => {
      if (res) {
        cancleJob(dataContent.masterId)
      }
    })
  }
}
const openjobInfoPopup = (item) => {
  jobInfoPopupRef.value.openPopup(item)
}
const cancleJob = (id) => {
  cancleTakeInventoryMoveJob(id)
    .then((res) => {
      if (res.data) {
        getList('refresh')
        uni.showToast({
          title: '放弃任务成功'
        })
      } else {
        showMessage('放弃任务失败')
      }
    })
    .catch((error) => {
      showMessage(error)
    })
}
const switchChangeToday = (state, creationTime) => {
  checkedToday.value = state
  todayTime.value = creationTime
  getList('refresh')
}
const switchChangeWait = (state, jobStatus) => {
  checkedWaitTask.value = state
  status.value = jobStatus
  getList('refresh')
}

const getScanNumber = (code) => {
  getDataListByType(code)
}

const getDataListByType = (code) => {
  proxy.$modal.loading('加载中­....')
  const filters = []
  filters.push({
    column: 'status',
    action: 'in',
    value: '1,2'
  })
  filters.push({
    column: 'number',
    action: '==',
    value: code
  })

  const params = {
    filters,
    pageNo: 1,
    pageSize: 100
  }
  getInventoryMoveJobList(params)
    .then((res) => {
      uni.hideLoading()
      if (res.data.list.length == 0) {
        showMessage('未查找到' + `【${code}】的收货任务`)
      } else if (res.data.list.length == 1) {
        openJobDetail(res.data.list[0])
      }
    })
    .catch((error) => {
      uni.hideLoading()
      showMessage(error)
    })
}
const showMessage = (message) => {
  comMessageRef.value.showErrorMessage(message, (res) => {
    if (res) {
    }
  })
}
const openFilter = () => {
  filter.value.openFilter()
}
const goHome1 = () => {
  goHome()
}
const refresh = () => {
  getList('refresh')
}
const onReach = (message) => {
  if (loadingType.value == 'loading' || loadingType.value == 'nomore') {
    return
  }
  getList('more')
}
defineExpose({
  refresh,
  openFilter,
  goHome1,
  onReach
})
</script>

<style scoped lang="scss"></style>
