<template>
  <!--  <baseTitle title="审核意见"></baseTitle>-->
  <!--  <fvForm :schema="schema" @getInstance="(e)=>form = e"></fvForm>-->
  <div class="oper-page-btn" style="display: flex">
    <el-button type="danger" @click="handleReject">驳回</el-button>
    <el-button color="#DED0B2" @click="handleAgree">同意</el-button>
  </div>
  <div class="opinion-dialog">
    <el-dialog v-model="showBackNode" title="请选择退回节点" width="400">
      <el-table :data="taskUserOptionList" style="width: 100%"
                :header-cell-style="{background:'#f5f7fa'}">
        <el-table-column width="55">
          <template #default="scope">
            <el-radio
                class="radio"
                :label="scope.row"
                v-model="backNode"
            >&emsp;&emsp;&emsp;
            </el-radio>
          </template>
        </el-table-column>
        <el-table-column
            prop="nodeName"
            label="节点名称">
<!--          <template #default="scope">-->
<!--            {{scope.row.nodeId==-1?'发起节点':scope.row.nodeName}}-->
<!--          </template>-->
        </el-table-column>
        <el-table-column
            prop="name"
            label="操作者">
          <template #default="scope">
            <el-tag v-if="scope.row.userInfo" type="success">{{ scope.row.userInfo.name }}</el-tag>
          </template>
        </el-table-column>
      </el-table>
      <div class="oper" style="display: flex;justify-content: flex-end;margin-top: 10px">
        <el-button type="danger" @click="rollbackHandler">确定</el-button>
        <el-button @click="showBackNode=false">取消</el-button>
      </div>
      <!--      <el-select v-if="taskUserOptionList?.length>0"  v-model="backNode" placeholder="请选择退回节点" clearable>-->
      <!--        <el-option-->
      <!--            v-for="item in taskUserOptionList"-->
      <!--            :key="item.nodeId"-->
      <!--            :label="item.nodeName + (item.userInfo ? ':' + item.userInfo.name : '')"-->
      <!--            :value="item.nodeId">-->
      <!--        </el-option>-->
      <!--      </el-select>-->
      <!--      <el-button type="danger" @click="rollbackHandler">确认</el-button>-->
    </el-dialog>
  </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: {}
  },
  value: {
    type: String,
    default: ''
  },
  //退回节点选择框数据
  taskUserOptionList: {
    type: Array,
    default: []
  }
})

const form = ref()
const backNode = ref({})
const showBackNode = ref(false)
const schema = computed(() => {
  return [
    {
      label: '',
      prop: 'auditOpinion',
      component: 'el-input',
      colProps: {
        span: 24
      },
      props: {
        placeholder: '请输入审核意见',
        type: 'textarea',
        rows: 3
      }
    }
  ]
})
const _value = computed({
  get() {
    return props.value;
  },
  set(val) {
    emit("update:value", val);
  }
})
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':
      if (route.query.source === 'home') {
        router.push('/home')
      } else {
        router.push({name: 'Fund'})
      }
      break;
    case 'Share/detail':
      if (route.query.source === 'home') {
        router.push('/home')
      } else {
        router.push({name: 'Expense/share'})
      }
      break;
    case 'Phase/detail':
      if (route.query.source === 'home') {
        router.push('/home')
      } else {
        router.push({name: 'Implementation'})
      }
      break;
  }
}
// 驳回
const handleReject = async () => {
  if (!_value.value) {
  ElNotification({
    title: '提示',
    message: '请填写审核意见',
    type: 'warning'
  })
  return
}
  showBackNode.value = true
  backNode.value = {}
}
//回退节点
const rollbackHandler = async () => {
  // const values = form.value.getValues()

  const params = {
    taskId: props.taskId,
    // ...values
    auditOpinion: _value.value,
    rollBackId: backNode.value.nodeId
  }
  // console.log('params', params)
  const res = await rejectTask(params)
  ElNotification({
    title: '提示',
    message: res.msg,
    type: res.code === 1000 ? 'success' : 'error'
  })
  if (res.code === 1000) {
    tagsViewStore.delVisitedViews(router.currentRoute.value.path)
    back()
  }
}

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

<style lang="scss">
.opinion-dialog {
  .el-dialog__body {
    padding: 0 !important;
  }
}
</style>
