<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" v-if="localFormData.isSpecialFund">
            <span>{{localFormData.specialFundId===0?localFormData.specialFund:changeName(fundOption,localFormData.specialFundId) }}</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>{{ localFormData.endTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目类型" prop="projectType">
            <span>{{ filterDict(cacheStore.getDict('project_type'), localFormData.projectType) }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="研发主体" prop="rdSubject">
            <span>{{ filterDict(cacheStore.getDict('rd_subject'), localFormData.rdSubject) }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出资类型" prop="investmentType">
            <span>{{ filterDict(cacheStore.getDict('invest_type'), localFormData.investmentType) }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目影响" prop="projectImpact">
            <span>{{ filterDict(cacheStore.getDict('project_impact'), localFormData.projectImpact) }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属业务板块" prop="businessSegment">
            <span>{{ filterDict(cacheStore.getDict('business_segment'), localFormData.businessSegment) }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="预期成果形式" prop="resultForm">
            <span>{{ filterDict(cacheStore.getDict('result_form'), localFormData.resultForm) }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="预期技术标准制定" prop="technicalStandard">
            <span>{{ filterDict(cacheStore.getDict('technical_standard'), localFormData.technicalStandard) }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="产学研联合" prop="industryUniversityResearch">
            <span>{{
                filterDict(cacheStore.getDict('industry_university'), localFormData.industryUniversityResearch)
              }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开展政府申报" prop="governmentDeclaration">
            <span>{{
                filterDict(cacheStore.getDict('government_declaration'), localFormData.governmentDeclaration)
              }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="知识产权状况" prop="intellectualProperty">
            <span>{{
                filterDict(cacheStore.getDict('intellectual_property'), 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>{{toThousands( localFormData.economicEstimate )}}</span>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="其中申请公司总部科技创新专项资金(元)" prop="specialFundAmount"
                        v-if="localFormData.isSpecialFund">
            <span>{{ toThousands(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)" style="font-size: 16px">
              {{ localFormData.singleFile?.originalFileName }}
            </el-button>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <file-component title="需求上报附件" tag="需求上报"
                          v-model:value="localFormData.fileList" :processViewer="processViewer"
                          :file-list-show="fileListShow"/>
        </el-col>
        <el-col :span="24">
          <div v-if="data.taskId">
            <baseTitle title="审核意见"></baseTitle>
            <el-form-item prop="_value">
              <el-input
                  v-model="_value"
                  :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>
    </el-form>
  </div>
</template>

<script setup lang="jsx">
import {toThousands} from '@/utils/changePrice.js'
import {downloadFile, deleteFile} from "@/api/project-demand";
import OperationRender from '@/views/workflow/common/OperationRender.vue'
import ProcessDiagramViewer from '@/views/workflow/common/ProcessDiagramViewer.vue'
import {useTagsView} from '@/stores/tagsview.js'
import {getFundOption} from "@/api/special-fund";
import {useCacheStore} from '@/stores/cache.js'
import {getSubCompOpt} from "@/api/user/user";
import FileComponent from "./FileComponent.vue";

const emit = defineEmits(['update:value'])
const tagsViewStore = useTagsView()
const cacheStore = useCacheStore()
const props = defineProps({
  formData: {
    type: Object,
    default: {}
  },
  data: {
    type: Object,
    default: {}
  },
  processViewer: {
    type: Boolean,
    default: false
  },
  fileListShow: {
    type: String,
    default: 'READ'
  },
  loading: {
    type: Boolean,
    default: false
  },
  value: {
    type: String,
    default: ''
  }
})
const localFormData = ref({})
const router = useRouter()
const fundOption = ref([])
const companyOption = ref([])
const dictName = ref({})
const rules = reactive({
  auditOpinion: [{required: true, message: '请输入审核意见', trigger: 'blur'}],
})
const _value = computed({
  get() {
    return props.value;
  },
  set(val) {
    emit("update:value", val);
  }
})
const filterDict = (data, value) => {
  if (data === undefined || value === undefined) return;
  let label = ''
  if (data instanceof Array) {
    data.find(item => {
      if (item.value == value) {
        label = item.label
      }
    })
  }
  return label
}
const getFundOptions = async () => {
  const resFund = await getFundOption()
  fundOption.value = resFund.data
  const res = await getSubCompOpt()
  companyOption.value = res.data
}
const changeName = (option, value) => {
  let name = ''
  option.forEach(item => {
    if (item.value == value) {
      name = item.label
    }
  })
  return name
}
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.processViewer, (newVal) => {
  props.processViewer = newVal
}, {deep: true})

watch(() => props.loading, (newVal) => {
  props.loading = newVal
}, {deep: true})

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

getFundOptions()
</script>

<style scoped>
.detail-block {
  overflow-x: hidden;
  overflow-y: auto;
  padding-bottom: 20px;
}
</style>
