<template>
  <div class="detail-block">
    <baseTitle title="需求上报"></baseTitle>
    <el-form :model="formData" ref="summaryForm" :rules="rules">
      <el-row gutter="50">
        <el-col :span="12">
          <el-form-item label="名称" prop="projectName">
            <el-input v-model="formData.projectName" placeholder="请输入名称" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="专项资金" prop="specialFund">
            <el-select v-model="formData.specialFund" placeholder="请选择专项资金" clearable filterable>
              <el-option
                  v-for="item in cacheStore.getDict('todo_type')"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开始时间" prop="startTime">
            <el-config-provider>
              <el-date-picker
                  v-model="formData.startTime"
                  type="datetime"
                  placeholder="开始时间"
                  value-format="YYYY-MM-DD HH:mm:ss"
                  style="width: 100%"
              />
            </el-config-provider>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="结束时间" prop="endTime">
            <el-config-provider>
              <el-date-picker
                  v-model="formData.endTime"
                  type="datetime"
                  placeholder="结束时间"
                  value-format="YYYY-MM-DD HH:mm:ss"
                  style="width: 100%"
              />
            </el-config-provider>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属公司" prop="affiliatedCompanyId">
            <el-select v-model="formData.affiliatedCompanyId" placeholder="请选择所属公司" clearable filterable>
              <el-option
                  v-for="item in companyOption"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目类型" prop="projectType">
            <el-select v-model="formData.projectType" placeholder="请选择项目类型" clearable filterable>
              <el-option
                  v-for="item in cacheStore.getDict('todo_type')"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="研发主体" prop="rdSubject">
            <el-input v-model="formData.rdSubject" placeholder="请输入研发主体" clearable></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="出资类型" prop="investmentType">
            <el-select v-model="formData.investmentType" placeholder="请选择出资类型" clearable filterable>
              <el-option
                  v-for="item in cacheStore.getDict('todo_type')"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目影响" prop="projectImpact">
            <el-select v-model="formData.projectImpact" placeholder="请选择项目影响" clearable filterable>
              <el-option
                  v-for="item in cacheStore.getDict('todo_type')"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属业务板块" prop="businessSegment">
            <el-select v-model="formData.businessSegment" placeholder="请选择所属业务板块" clearable filterable>
              <el-option
                  v-for="item in cacheStore.getDict('todo_type')"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="预期成果形式" prop="resultForm">
            <el-select v-model="formData.resultForm" placeholder="请选择预期成果形式" clearable filterable>
              <el-option
                  v-for="item in cacheStore.getDict('todo_type')"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="预期技术标准制定" prop="technicalStandard">
            <el-select v-model="formData.technicalStandard" placeholder="请选择预期技术标准制定" clearable filterable>
              <el-option
                  v-for="item in cacheStore.getDict('todo_type')"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="产学研联合" prop="industryUniversityResearch">
            <el-select v-model="formData.industryUniversityResearch" placeholder="请选择产学研联合" clearable filterable>
              <el-option
                  v-for="item in cacheStore.getDict('todo_type')"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开展政府申报" prop="governmentDeclaration">
            <el-select v-model="formData.governmentDeclaration" placeholder="请选择开展政府申报" clearable filterable>
              <el-option
                  v-for="item in cacheStore.getDict('todo_type')"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="知识产权状况" prop="intellectualProperty">
            <el-select v-model="formData.intellectualProperty" placeholder="请选择知识产权状况" clearable filterable>
              <el-option
                  v-for="item in cacheStore.getDict('todo_type')"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="发明专利(项)" prop="inventionPatent">
            <el-input v-model="formData.inventionPatent" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="实用性新型专利(项)" prop="newPatent">
            <el-input v-model="formData.newPatent" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="软件著作权(项)" prop="softwareCopyright">
            <el-input v-model="formData.softwareCopyright" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="著作权(项)" prop="copyright">
            <el-input v-model="formData.copyright" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="其他(项)" prop="other">
            <el-input v-model="formData.other" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="经济概算(万元)" prop="economicEstimate">
            <el-input v-model="formData.economicEstimate" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="其中申请公司总部科技创新专项资金(万元)" prop="specialFundAmount">
            <el-input v-model="formData.specialFundAmount" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="现有业务描述" prop="serviceDescription">
            <el-input v-model="formData.serviceDescription" type="textarea" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="研发项目关键内容描述" prop="contentDescription">
            <el-input v-model="formData.contentDescription" type="textarea" clearable></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <AttachmentUpload ref="attachment" label="需求申请书附件" :showTable="showTable" :otherFileList="otherFileList"
                      @getAttachment="getAttachment"
                      @getOtherFile="getOtherFile" :showFileList="true"/>
    <!--  <fvForm :schema="schame" @getInstance="getInstance"></fvForm>-->
    <div class="oper-page-btn">
      <el-button type="primary" @click="staging">存为草稿</el-button>
      <el-button type="primary" @click="handleSubmit(summaryForm)">发布</el-button>
    </div>
  </div>
</template>

<script setup lang="jsx">
import {getProcessInfo, requirementReported} from "./api";
import {ElNotification} from "element-plus";
import {useTagsView} from '@/stores/tagsview.js'
import {useCacheStore} from '@/stores/cache.js'
import {getSubCompOpt} from "../../../api/user/user";

const cacheStore = useCacheStore()
const router = useRouter()
const tagsViewStore = useTagsView()
const companyOption = ref([])
const summaryForm = ref()
const attachment = ref()
const deploymentId = ref()
const showTable = ref(true)
const otherFileList = ref([])
const fileList = ref([])
const formData = ref({
  specialFund: null
})
const rules = reactive({
  projectName: [{required: true, message: '请输入名称', trigger: 'blur'}],
  specialFund: [{required: true, message: '请选择专项资金', trigger: 'blur'}],
  startTime: [{required: true, message: '请选择开始时间', trigger: 'blur'}],
  rdSubject: [{required: true, message: '请输入研发主体', trigger: 'blur'}],
  affiliatedCompanyId: [{required: true, message: '请输入所属公司', trigger: 'blur'}],
  projectType: [{required: true, message: '请选择项目类型', trigger: 'blur'}],
  endTime: [{required: true, message: '请选择结束时间', trigger: 'blur'}],
  investmentType: [{required: true, message: '请选择出资类型', trigger: 'blur'}],
  projectImpact: [{required: true, message: '请选择项目影响', trigger: 'blur'}],
  businessSegment: [{required: true, message: '请选择所属业务板块', trigger: 'blur'}],
  resultForm: [{required: true, message: '请选择预期成果形式', trigger: 'blur'}],
  technicalStandard: [{required: true, message: '请选择预期技术标准制定', trigger: 'blur'}],
  industryUniversityResearch: [{required: true, message: '请选择产学研联合', trigger: 'blur'}],
  governmentDeclaration: [{required: true, message: '请选择开展政府申报', trigger: 'blur'}],
  intellectualProperty: [{required: true, message: '请选择知识产权状况', trigger: 'blur'}],
  inventionPatent: [{required: true, message: '请输入发明专利', trigger: 'blur'}],
  newPatent: [{required: true, message: '请输入实用性新型专利', trigger: 'blur'}],
  softwareCopyright: [{required: true, message: '请输入软件著作权', trigger: 'blur'}],
  copyright: [{required: true, message: '请输入著作权', trigger: 'blur'}],
  other: [{required: true, message: '请输入其他', trigger: 'blur'}],
  economicEstimate: [{required: true, message: '请输入经济概算', trigger: 'blur'}],
  specialFundAmount: [{required: true, message: '请输入专项资金', trigger: 'blur'}],
  serviceDescription: [{required: true, message: '请输入现有业务描述', trigger: 'blur'}],
  contentDescription: [{required: true, message: '请输入研发项目关键内容描述', trigger: 'blur'}]
})
const compositeParam = (item, type) => {
  let tag = ''
  if (router.currentRoute.value.name === 'Summary/add') {
    tag = '需求上报'
  }
  return {
    fileId: item.id,
    size: item.size,
    type: type,
    originalFileName: item.originalFilename,
    fileType: item.fileType,
    url: item.url,
    processNodeTag: null,
    tag: tag
  }
}
const getAttachment = (val) => {
  console.log('上传文件getAttachment', val)
  let fileObj = compositeParam(val, 1)
  fileList.value.push(fileObj)
}
const getOtherFile = (val) => {
  console.log('上传文件getOtherFile', val)
  showTable.value = false
  let fileObj = compositeParam(val, 2)
  otherFileList.value.push(fileObj)
  nextTick(() => {
    showTable.value = true
  })
}
const getFileParam = (item) => {
  return {
    fileId: item.fileId,
    type: item.type
  }
}
const handleSubmit = async (instance) => {
  let params = {
    "specialFund": "2",
    "projectName": "3",
    "startTime": "2024-05-24 00:00:00",
    "endTime": "2024-05-29 00:00:00",
    "projectType": "3",
    "investmentType": "3",
    "businessSegment": "2",
    "technicalStandard": "2",
    "governmentDeclaration": "2",
    "inventionPatent": "3",
    "newPatent": "3",
    "economicEstimate": "3",
    "other": "3",
    "copyright": "3",
    "softwareCopyright": "3",
    "industryUniversityResearch": "2",
    "intellectualProperty": "00",
    "resultForm": "3",
    "projectImpact": "3",
    "rdSubject": "3",
    "affiliatedCompanyId": 93,
    "serviceDescription": "3",
    "contentDescription": "3",
    "specialFundAmount": "3",
    "deploymentId": "e75b2040-15b4-11ef-8cdf-f268fc747b04",
    "files": [
      {
        "fileId": 207,
        "type": 1
      },
    ],
    "requirementId": 102
  }
  // if (!instance) return
  // instance.validate(async (valid, fields) => {
  //   if (fileList.value.length === 0) {
  //     attachment.value.validate()
  //   } else {
  //     attachment.value.clearValidate()
  //   }
  //   if (!valid) return
    let files = []
    fileList.value.forEach(item => {
      files.push(getFileParam(item))
    })
    otherFileList.value.forEach(item => {
      files.push(getFileParam(item))
    })
    // ...formData.value,
    //   deploymentId: deploymentId.value,
    //   files: files,
    //   requirementId:102
    // }
    let res = await requirementReported(params)
    ElNotification({
      title: '提示',
      message: res.msg,
      type: res.code === 1000 ? 'success' : 'error'
    })
    if (res.code === 1000) {
      tagsViewStore.delVisitedViews(router.currentRoute.value.path)
      await router.push({
        name: 'Summary'
      })
    }
  // })
}
const init = async () => {
  const res = await getSubCompOpt()
  companyOption.value = res.data
  getProcessInfo().then(res => {
    ElNotification({
      title: '提示',
      message: res.msg,
      type: res.code === 1000 ? 'success' : 'error'
    })
    if (res.code === 1000) {
      let data = res.data
      deploymentId.value = data.deploymentId
    }
  })
}
init()
const staging = async () => {
}
</script>
<style lang="scss" scoped>

.detail-block {
  overflow: hidden;
  padding-bottom: 30px;
}

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

</style>
