<script name="auditDialog" setup>
import { ref, getCurrentInstance, watch } from 'vue'
import {
  fileFormatVerification,
  handleFileSuccess,
  handleFileError,
  fileTypes,
  fileDesc,
  changFilesParam,
  handleFilePreview
} from '@/utils/uploadAction.js'
import { getConstructionSuggestions, handler } from '@/http/apis/toDoCenter/todoList'
import store from '@/store'
import { useRoute, useRouter } from 'vue-router'
const route = useRoute(),
  router = useRouter(),
  { proxy } = getCurrentInstance(),
  uploadUrl = store.dictStore.uploadUrl,
  props = defineProps({
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    title: {
      type: String,
      default: '通过'
    },
    taskId: {
      type: String,
      default: ''
    },
    action: {
      type: String,
      default: ''
    },
    constructionSuggestions: {
      type: Boolean,
      default: false
    },
    projectCode: {
      type: String,
      default: ''
    },
    isPerformance: {
      type: Boolean,
      default: false
    }
  }),
  emits = defineEmits(['close', 'reload']),
  formRef = ref(),
  form = ref({
    attachments: []
  }),
  rules = {
    comment: [{ required: true, message: '请输入意见', trigger: 'blur' }],
    proposeTotalInvest: [{ required: true, message: '请输入建议总投资' }],
    proposeAnnualBudget: [{ required: true, message: '请输入建议年度预算' }],
    proposeAttachFiles: [{ required: true, message: '请上传' }]
  },
  submit = async formEl => {
    if (!formEl) {
      return
    }
    await formEl.validate(async valid => {
      if (valid) {
        const postData = {
          instanceId: route.query.instanceId,
          projectId: route.query.projectId,
          projectCode: props.projectCode,
          taskId: props.taskId,
          action: props.action,
          auditInfo: {
            text: form.value.comment,
            attachments: form.value.attachments.map((i) => ({
              fileId: i.response.data.id,
              fileName: i.response.data.originalFileName
            }))
          }
        }
        if (props.constructionSuggestions && props.action === 'PASS') {
          await getConstructionSuggestions({
            projectCode: props.projectCode,
            instanceId: route.query.instanceId,
            proposeTotalInvest: form.value.proposeTotalInvest,
            proposeAnnualBudget: form.value.proposeAnnualBudget,
            proposeAttachFiles: form.value.proposeAttachFiles && JSON.stringify(changFilesParam(form.value.proposeAttachFiles))
          })
        }
        const res = await handler(postData)
        if (res.data === 'TASKID_ERROR') {
          proxy.$message.warning('当前审批任务有变动，请返回待我处理列表！')
          router.push({ name: 'waitMeToHandle' })
        } else {
          proxy.$message.success('提交成功！')
          if (res.data) {
            router.push({
              query: {
                ...route.query,
                taskId: res.data
              }
            })
          }
          emits('close', true)
          emits('reload', res.data)
          await store.countStore.setCountStore()
        }
      }
    })
  }
watch(
  () => props.visible,
  async val => {
    if (!val) {
      formRef.value.resetFields()
      form.value = {}
    }
  }
)
</script>

<template>
  <el-dialog
    :model-value="visible"
    :title="title"
    width="600px"
    destroy-on-close
    @close="emits('close')"
  >
    <div
      v-if="title==='驳回'&&!isPerformance"
      class="infoTip flex items-center mb-16"
    >
      <el-icon class="mr-8">
        <Warning />
      </el-icon>
      <span>驳回后，需要申报单位修改项目信息后，重新发起该流程。</span>
    </div>
    <div
      v-if="title==='退回'"
      class="infoTip flex items-center mb-16"
    >
      <el-icon class="mr-8">
        <Warning />
      </el-icon>
      <span>退回后，需要申报单位修改项目信息后，再由您进行审核确认。</span>
    </div>
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="auto"
      label-suffix=":"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item
            :label="`${title}意见`"
            prop="comment"
          >
            <el-input
              v-model="form.comment"
              maxlength="200"
              placeholder="请输入"
              show-word-limit
              type="textarea"
            />
          </el-form-item>
        </el-col>
        <template v-if="constructionSuggestions&&action==='PASS'">
          <el-col :span="24">
            <el-form-item label="建议总投资" prop="proposeTotalInvest">
              <el-input-number
                v-model="form.proposeTotalInvest"
                class="input-amount"
                placeholder="请填写"
                :min="0.000001"
                :controls="false"
                @mousewheel.prevent
              >
                <template #suffix>万元</template>
              </el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="建议年度预算" prop="proposeAnnualBudget">
              <el-input-number
                v-model="form.proposeAnnualBudget"
                class="input-amount"
                placeholder="请填写"
                :min="0.000001"
                :controls="false"
                @mousewheel.prevent
              >
                <template #suffix>万元</template>
              </el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item
              label="评审意见附件"
              prop="proposeAttachFiles"
            >
              <el-upload
                v-model:file-list="form.proposeAttachFiles"
                class="w-full"
                :action="uploadUrl"
                :on-success="res => handleFileSuccess(res, form.proposeAttachFiles)"
                :on-error="handleFileError"
                :before-upload="file=>fileFormatVerification(file, {types: fileTypes})"
                :accept="fileTypes.map(i=>`.${i}`).join(',')"
                multiple
                :limit="10"
                :on-preview="handleFilePreview"
              >
                <el-button type="primary" class="mr-4">上传附件</el-button>
                <template #tip>
                  <div class="el-upload__tip">支持{{ fileDesc }}文件</div>
                </template>
              </el-upload>
            </el-form-item>
          </el-col>
        </template>
        <el-col :span="24">
          <el-form-item
            :label="`${constructionSuggestions&&action==='PASS'?'其他附件':'附件'}`"
            prop="files"
          >
            <el-upload
              v-model:file-list="form.attachments"
              class="w-full"
              :action="uploadUrl"
              :on-success="res => handleFileSuccess(res, form.attachments)"
              :on-error="handleFileError"
              :before-upload="file=>fileFormatVerification(file, {types: fileTypes})"
              :accept="fileTypes.map(i=>`.${i}`).join(',')"
              multiple
              :limit="10"
              :on-preview="handleFilePreview"
            >
              <el-button type="primary" class="mr-4">上传附件</el-button>
              <template #tip>
                <div class="el-upload__tip">支持{{ fileDesc }}文件</div>
              </template>
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="flex justify-center">
        <el-button
          class="ml-22"
          type="primary"
          @click="submit(formRef)"
        >
          提交
        </el-button>
        <el-button
          @click="emits('close')"
        >
          关闭
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
