<template>
  <view class="px-base" v-if="detail">
    <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="detail" :colums="columns" />
    </view>
    <uv-action-sheet ref="pickerRef" :actions="actions" @select="confirmPicker" />
    <uv-modal ref="modalRef" title="提示" content="确定删除吗?" @confirm="onSubmit" :showCancelButton="true"></uv-modal>
  </view>
</template>
<script setup>
import CuNavbar from "@/components/cu-navbar/index"
import { http } from "@/utils/request"
import { onLoad } from "@dcloudio/uni-app"
import { ref } from "vue"
import BaseData from '../audits/base-data'
import statusFun from '@/utils/status'

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

const columns = [
  { title: '审核状态', dataIndex: 'workflow_check.check_status', format: (value) => statusFun(value, 'statusExpense', 'name') },
  { title: '申请人', dataIndex: 'employee.name' },
  { title: '所属门店', dataIndex: 'store.title' },
  { title: '电话号码', dataIndex: 'employee.phone' },
  { title: '申请时间', dataIndex: 'created_format' },
  { title: '请假类型', dataIndex: 'type.name' },
  { title: '请假开始时间', dataIndex: 'start_format' },
  { title: '请假结束时间', dataIndex: 'end_format' },
  { title: '请假事由', dataIndex: 'reason', labelPosition: 'top' },
  { title: '未通过原因', dataIndex: 'workflow_check.check_remarks', labelPosition: 'top', isShow: (data) => data?.workflow_check?.check_status == 4 },
]

const open = () => {
  pickerRef.value.open()
}
const confirmPicker = e => {
  if (e.value == 'edit') {
    return uni.navigateTo({
      url: `/pages/ask-leave/create?id=${id.value}`
    })
  }
  if (e.value == 'delete') {
    return modalRef.value.open()
  } else if (e.value == 'check-logs') {
    return uni.navigateTo({
      url: `/pages/audits/log?id=${detail.value.workflow_check.id}`
    })
  }
}
const onSubmit = async () => {
  try {
    await http.delete(`/hr/holidays/${id.value}`)
    uni.showToast({
      title: "删除成功",
      icon: "none"
    })
    uni.$emit('refresh')
    uni.navigateBack()
  } catch (error) {
    console.log(error)
  } finally {
    loading.value = false
  }
}
onLoad(options => {
  id.value = options.id
  http.get(`/hr/holidays/${options.id}`).then(res => {
    detail.value = res

    let status = res.workflow_check.check_status
    if ([2, 3].indexOf(status) != -1) {
      actions.value[0].disabled = true
      actions.value[1].disabled = true
      // actions.value.splice(0, 2)
    }
  })
})
</script>
