<template>
  <view class="px-base">
    <CuNavbar title="任务详情">
      <template v-if="actions.length > 0" #right>
        <uv-icon color="white" @click="open" name="more-dot-fill"></uv-icon>
      </template>
    </CuNavbar>
    <view
      class="mt-30rpx card-shadow bg-white rounded-19rpx px-base text-[#333333] text-27rpx"
    >
      <BaseData :data="data" :colums="baseColums" />
    </view>

    <uv-action-sheet
      ref="pickerRef"
      :actions="actions"
      @select="confirmPicker"
    />
  </view>
</template>
<script setup>
import CuNavbar from '@/components/cu-navbar/index'
import { onLoad } from '@dcloudio/uni-app'
import { http } from '@/utils/request'
import { ref, computed } from 'vue'
import { timeFormat } from '@climblee/uv-ui/libs/function'
import BaseData from '@/pages/audits/base-data'
import statusFun from '@/utils/status'

const pickerRef = ref(null)
const actions = ref([
  { name: '修改', value: 'edit', disabled: false },
  { name: '审核流程', value: 'check-logs', disabled: false },
])

const baseColums = [
  {
    title: '审核状态',
    dataIndex: 'taskable.status',
    format: (value) => statusFun(value, 'task_hygienes', 'name'),
  },
  {
    title: '申请人',
    dataIndex: 'taskable.store_master.name',
  },
  {
    title: '所属门店',
    dataIndex: 'taskable.store.title',
  },
  {
    title: '电话号码',
    dataIndex: 'taskable.store_master.phone',
  },
  {
    title: '申请时间',
    dataIndex: 'created_format',
    isShow: (row) => {
      return row?.taskable?.status != 1
    },
  },
  {
    title: '清洁范围',
    dataIndex: 'taskable.description',
    labelPosition: 'top',
  },
  {
    title: '清洁结果',
    dataIndex: 'taskable.photos',
    labelPosition: 'top',
    type: 'album',
  },
  {
    title: '未通过原因',
    dataIndex: 'taskable.workflow_check.check_remarks',
    labelPosition: 'top',
    isShow: (row) => {
      return row?.taskable?.status == 6
    },
  },
]
const id = ref(null)
const data = ref({})

onLoad((opt) => {
  id.value = opt.id
  getDetail()
})

const open = () => {
  pickerRef.value.open()
}

const getDetail = async () => {
  const resdata = await http.get(`/tasks/${id.value}`)
  data.value = resdata
  actions.value[0].disabled = resdata?.taskable?.status != 6
}
const confirmPicker = (e) => {
  
  if (e.value == 'edit') {
    uni.navigateTo({
      url: `/pages/task/task_hygienes_submit?id=${id.value}`,
    })
  } else if (e.value == 'check-logs') {
    return uni.navigateTo({
      url: `/pages/audits/log?id=${data.value.taskable.workflow_check.id}`,
    })
  }
}


const getValue = (obj, path) => {
  return path.split('.').reduce((acc, key) => (acc ? acc[key] : undefined), obj)
}

const isFunction = (fn) => {
  return typeof fn === 'function'
}
</script>
