<script name="approvalDialog" setup>
import { ref, getCurrentInstance, watch } from 'vue'
import { changFilesParam, fileFormatVerification, handleFileSuccess, handleFilePreview, fileTypes, fileDesc } from '@/utils/uploadAction.js'
import store from '@/store'
import { preExamineDeclare } from '@/http/apis/declareMange'
const { proxy } = getCurrentInstance(),
  uploadUrl = store.dictStore.uploadUrl,
  props = defineProps({
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    data: Object
  }),
  emits = defineEmits(['close']),
  loading = ref(false),
  dialogForm = ref({
    higherLineSuperOrgReviewComments: []
  }),
  rules = {
    'higherLineSuperOrgReviewComments': [{ required: true, message: '请选择' }]
  },
  dialogFormRef = ref(),
  submit = async (formEl) => {
    if (!formEl) {
      save()
      return
    }
    formEl.validate(async (valid) => {
      if (valid) {
        save()
      }
    }
    )
  },
  save = async () => {
    loading.value = true
    try {
      await preExamineDeclare({
        projectInfo: {
          ...dialogForm.value,
          higherLineSuperOrgReviewComments: dialogForm.value?.higherLineSuperOrgReviewComments?.length && JSON.stringify(changFilesParam(dialogForm.value.higherLineSuperOrgReviewComments)) || undefined,
          id: props.data.id
        }
      })
      proxy.$message.success('发起成功！')
      loading.value = false
      emits('close', true)
    } catch (e) {
      loading.value = false
    }
  }

watch(
  () => props.visible,
  async val => {
    if (val) {
      console.log('props.data', props.data)
    } else {
      dialogForm.value = { higherLineSuperOrgReviewComments: [] }
    }
  }
)
</script>

<template>
  <el-dialog
    :model-value="visible"
    title="预审申报"
    width="600px"
    destroy-on-close
    @close="emits('close')"
  >
    <div>
      <p>根据《浙江省电子政务重大应用项目联审》要求，提交预审后：</p>
      <p>1、市级大于1000万元的重大项目将由省级数改牵头部门及业务主管单位进行联审</p>
      <p>2、区县申报项目将由市级数改牵头部门及业务主管单位进行联审，大于500万元的重大项目联审后会提交省里进行备案。</p>
    </div>
    <template v-if="false">
      <p>提交后将发起项目预审流程，确定提交吗?</p>
      <el-form
        v-if="data.needUploadSuperLineFile"
        ref="dialogFormRef"
        :model="dialogForm"
        :rules="rules"
        label-position="top"
        label-width="auto"
        status-icon
        class="mt-16"
      >
        <el-form-item label="上级条线主管单位审核意见:" prop="higherLineSuperOrgReviewComments">
          <el-upload
            v-model:file-list="dialogForm.higherLineSuperOrgReviewComments"
            class=" w-full"
            :action="uploadUrl"
            :limit="1"
            :on-success="res => handleFileSuccess(res, dialogForm.higherLineSuperOrgReviewComments, true)"
            :before-upload="file=>fileFormatVerification(file, {types: fileTypes})"
            :accept="fileTypes.map(i=>`.${i}`).join(',')"
            :on-preview="handleFilePreview"
          >
            <el-button type="primary" plain>选择文件</el-button>
            <template #tip>
              <div class="el-upload__tip">
                支持{{ fileDesc }}文件
              </div>
            </template>
          </el-upload>
        </el-form-item>
      </el-form>
    </template>
    <template #footer>
      <el-button
        type="primary"
        :loading="loading"
        @click="submit(dialogFormRef)"
      >
        提交
      </el-button>
      <el-button
        @click="emits('close')"
      >
        关闭
      </el-button>
    </template>
  </el-dialog>
</template>
