<template>
  <div class="detail-block" v-loading="loading">
    <el-form :model="localFormData" ref="summaryForm" :rules="rules">
      <el-row gutter="50">
        <el-col :span="12">
          <el-form-item label="名称" prop="projectName">
            <span>{{ localFormData.projectName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="专项资金" prop="specialFund">
            <span>{{ localFormData.specialFund }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开始时间" prop="startTime">
            <span>{{ localFormData.startTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="结束时间" prop="endTime">
            <span>{{ formData.endTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属公司" prop="affiliatedCompanyId">
            <span>{{ localFormData.affiliatedCompanyId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目类型" prop="projectType">
            <span>{{ localFormData.projectType }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="研发主体" prop="rdSubject">
            <span>{{ localFormData.rdSubject }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出资类型" prop="investmentType">
            <span>{{ localFormData.investmentType }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目影响" prop="projectImpact">
            <span>{{ localFormData.projectImpact }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属业务板块" prop="businessSegment">
            <span>{{ localFormData.businessSegment }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="预期成果形式" prop="resultForm">
            <span>{{ localFormData.resultForm }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="预期技术标准制定" prop="technicalStandard">
            <span>{{ localFormData.technicalStandard }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="产学研联合" prop="industryUniversityResearch">
            <span>{{ localFormData.industryUniversityResearch }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开展政府申报" prop="governmentDeclaration">
            <span>{{ localFormData.governmentDeclaration }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="知识产权状况" prop="intellectualProperty">
            <span>{{ localFormData.intellectualProperty }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="发明专利(项)" prop="inventionPatent">
            <span>{{ localFormData.inventionPatent }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="实用性新型专利(项)" prop="newPatent">
            <span>{{ localFormData.newPatent }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="软件著作权(项)" prop="softwareCopyright">
            <span>{{ localFormData.softwareCopyright }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="著作权(项)" prop="copyright">
            <span>{{ localFormData.copyright }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="其他(项)" prop="other">
            <span>{{ localFormData.other }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="经济概算(万元)" prop="economicEstimate">
            <span>{{ localFormData.economicEstimate }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="其中申请公司总部科技创新专项资金(万元)" prop="specialFundAmount">
            <span>{{ localFormData.specialFundAmount }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="现有业务描述" prop="serviceDescription">
            <span>{{ localFormData.serviceDescription }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="研发项目关键内容描述" prop="contentDescription">
            <span>{{ localFormData.contentDescription }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="需求上报申请书">
            <el-button type="primary" link @click="handleDownload(localFormData.singleFile)">
              {{ localFormData.singleFile?.originalFileName }}
            </el-button>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="需求上报附件">
            <fvTable style="width: 100%;max-height: 400px;" v-if="processViewer" :tableConfig="tableConfig"
                     :data="localFormData.fileList" :isSettingCol="false" :pagination="false">
              <template #empty>
                <el-empty :image-size="90" description="暂无数据" style="padding: 0"/>
              </template>
            </fvTable>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <div v-if="data.taskId">
            <baseTitle title="审核意见"></baseTitle>
            <el-form-item prop="auditOpinion">
              <el-input
                  v-model="localFormData.auditOpinion"
                  :rows="3"
                  type="textarea"
                  placeholder="请输入审核意见"
              />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <div class="approval-record">
        <baseTitle title="审批记录"></baseTitle>
        <div class="process">
          <operation-render v-if="processViewer" :operation-list="data.operationList"
                            :state="data.state"/>
          <process-diagram-viewer  v-if="processViewer" id-name="summaryProcess"/>
        </div>
      </div>
      <div class="oper-page-btn" v-if="data.state === '1' && data.taskId">
        <el-button @click="handleReject(summaryForm)">驳回</el-button>
        <el-button color="#DED0B2" @click="handleAgree">同意</el-button>
      </div>
    </el-form>
  </div>
</template>

<script setup lang="jsx">
import {downloadFile} from "@/api/project-demand";
import OperationRender from '@/views/workflow/common/OperationRender.vue'
import ProcessDiagramViewer from '@/views/workflow/common/ProcessDiagramViewer.vue'
import {ElMessage, ElNotification} from "element-plus";
import {agreeTask, rejectTask} from "@/api/project-demand";
import {useTagsView} from '@/stores/tagsview.js'

const tagsViewStore = useTagsView()
const props = defineProps({
  formData: {
    type: Object,
    default: {}
  },
  data: {
    type: Object,
    default: {}
  },
  processViewer: {
    type: Boolean,
    default: false
  }, loading: {
    type: Boolean,
    default: false
  }
})
const localFormData = ref({})
const tableConfig = reactive({
  columns: [
    {
      prop: 'index',
      type: 'index',
      label: '序号',
      align: 'center',
      width: '80',
    },
    {
      prop: 'originalFileName',
      label: '文件名',
      align: 'center',
    },
    {
      prop: 'tag',
      label: '标签',
      align: 'center'
    },
    {
      prop: 'size',
      label: '文件大小',
      align: 'center',
      currentRender: ({row, index}) => (parseInt(row.size / 1024) + 'KB')
    },
    {
      prop: 'oper',
      label: '操作',
      align: 'center',
      currentRender: ({row, index}) => {
        return (
            <el-button type="primary" link onClick={() => handleDownload(row)}>下载</el-button>
        )
      }
    }
  ]
})
const router = useRouter()
const summaryForm = ref()
const rules = reactive({
  auditOpinion: [{required: true, message: '请输入审核意见', trigger: 'blur'}],
})
const handleReject = (instance) => {
  if (!instance) return
  instance.validate(async (valid) => {
    if (!valid) return
    let approve = {
      taskId: props.data.taskId,
      auditOpinion: localFormData.value.auditOpinion,
    }
    rejectTask(approve).then(res => {
      ElNotification({
        title: '提示',
        message: res.msg,
        type: res.code === 1000 ? 'success' : 'error'
      })
      if (res.code === 1000) {
        tagsViewStore.delVisitedViews(router.currentRoute.value.path)
        router.push({
          name: 'Summary'
        })
      }
    })
  })
}
const handleAgree = () => {
  let approve = {
    taskId: props.data.taskId,
    auditOpinion: localFormData.value.auditOpinion,
    formData: localFormData.value
  }
  agreeTask(approve).then(res => {
    ElNotification({
      title: '提示',
      message: res.msg,
      type: res.code === 1000 ? 'success' : 'error'
    })
    if (res.code === 1000) {
      tagsViewStore.delVisitedViews(router.currentRoute.value.path)
      router.push({
        name: 'Summary'
      })
    }
  })
}
const handleDownload = (row) => {
  downloadFile(row.fileId).then(res => {
    const blob = new Blob([res])
    let a = document.createElement('a')
    a.href = URL.createObjectURL(blob)
    a.download = row.originalFileName
    a.click()
  })
}

watch(
    () => props.formData,
    (val) => {
      console.log(val, 'ssss');
    }
)
watch(() => props.processViewer, (newVal) => {
  props.processViewer = newVal
}, {deep: true})
watch(() => props.loading, (newVal) => {
  props.loading = newVal
}, {deep: true})

watchEffect(() => {
  Object.keys(props.formData).length && (localFormData.value = props.formData)
})
</script>

<style scoped>
:deep(.el-table--fit) {
  height: auto !important;
}

.detail-block {
  overflow-x: hidden;
  overflow-y: auto;
  padding-bottom: 20px;
}

</style>
