<template>
  <baseTitle title="审核意见"></baseTitle>
  <fvForm :schema="schema" @getInstance="(e)=>form = e"></fvForm>
  <div class="oper-page-btn">
    <el-button type="danger" @click="handleReject">驳回</el-button>
    <el-button color="#DED0B2" @click="handleAgree">同意</el-button>
  </div>
</template>

<script setup lang="jsx">
import {ElNotification} from 'element-plus';
import {agreeTask, rejectTask} from "@/api/project-demand/index.js";

import { useTagsView } from '@/stores/tagsview.js'
const tagsViewStore = useTagsView()
const route = useRoute()
const router = useRouter()
const props = defineProps({
  taskId: {
    type: String,
    default: ''
  },
  formData: {
    type: Object,
    default: {}
  }
})

const form = ref()
const schema = computed(() => {
  return [
    {
      label: '',
      prop: 'auditOpinion',
      component: 'el-input',
      colProps: {
        span: 24
      },
      props: {
        placeholder: '请输入审核意见',
        type: 'textarea',
        rows: 3
      }
    }
  ]
})
const back = () => {
  switch (route.name) {
    case 'Initiation/detail':
      router.push({name: 'Initiation'})
      break;
    case 'Filing/detail':
      router.push({name: 'Filing'})
      break;
    case 'Implementation/detail':
      if (route.query.source === 'home') {
        router.push('/home')
      } else {
        if (route.query.step === '10') {
          router.push({name: 'Summary'})
        } else if (route.query.step === '20') {
          router.push({name: 'Initiation'})
        } else if (route.query.step === '40') {
          router.push({name: 'Implementation'})
        } else if (route.query.step === '50') {
          router.push({name: 'Filing'})
        } else if (route.query.step === '00') {
          router.push({name: 'Requirement'})
        }
      }
      break;
    case 'Summary/detail':
      if (route.query.source === 'home') {
        router.push('/home')
      } else {
        router.push({name: 'Summary'})
      }
      break;
    case 'Requirement/detail':
      if (route.query.source === 'home') {
        router.push('/home')
      } else {
        router.push({name: 'Requirement'})
      }
      break;
    case 'Fund/detail':
      router.push({name: 'Fund'})
      break;
    case 'Share/detail':
      router.push({name: 'Expense/share'})
      break;
  }
}
// 驳回
const handleReject = async () => {
  const values = form.value.getValues()
  if (!values.auditOpinion) {
    ElNotification({
      title: '提示',
      message: '请填写审核意见',
      type: 'warning'
    })
    return
  }
  const params = {
    taskId: props.taskId,
    ...values
  }
  const res = await rejectTask(params)
  ElNotification({
    title: '提示',
    message: res.msg,
    type: res.code === 1000 ? 'success' : 'error'
  })
  tagsViewStore.delVisitedViews(router.currentRoute.value.path)
  back()
}

const handleAgree = async () => {
  const values = form.value.getValues()
  const params = {
    taskId: props.taskId,
    formData: props.formData,
    ...values
  }
  const res = await agreeTask(params)
  ElNotification({
    title: '提示',
    message: res.msg,
    type: res.code === 1000 ? 'success' : 'error'
  })
  tagsViewStore.delVisitedViews(router.currentRoute.value.path)
  back()
}
</script>

<style lang="scss" scoped>

</style>
