<template>
  <div class="detail-block" v-loading="loading">
    <el-form :model="localFormData" ref="summaryForm" :rules="rules">
      <baseTitle title="预期知识产权"></baseTitle>
      <el-row gutter="20" style="margin-bottom: -18px;margin-left: 5px">
        <el-col :span="24">
          <el-form-item label="预期成果形式" prop="resultForm">
            <span>{{ filterDict(cacheStore.getDict('result_form'), localFormData.resultForm) }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="知识产权状况" prop="intellectualProperty">
            <span>{{
                filterDict(cacheStore.getDict('intellectual_property'), localFormData.intellectualProperty)
              }}</span>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="发明专利(项)" prop="inventionPatent">
            <span>{{ localFormData.inventionPatent }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="实用性新型专利(项)" prop="newPatent">
            <span>{{ localFormData.newPatent }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="软件著作权(项)" prop="softwareCopyright">
            <span>{{ localFormData.softwareCopyright }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="著作权(项)" prop="copyright">
            <span>{{ localFormData.copyright }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="其他(项)" prop="other">
            <span>{{ localFormData.other }}</span>
          </el-form-item>
        </el-col>

      </el-row>
      <baseTitle title="项目描述"></baseTitle>
      <el-row gutter="20" style="margin-bottom: -18px;margin-left: 5px;">
        <el-col :span="24">
          <el-form-item label="现有业务描述" prop="serviceDescription">
            <span style="white-space: pre-wrap">{{ localFormData.serviceDescription }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="研发项目关键内容描述" prop="contentDescription">
            <span style="white-space: pre-wrap">{{ localFormData.contentDescription }}</span>
          </el-form-item>
        </el-col>
      </el-row>
      <baseTitle title="需求上报申请书" style="margin-bottom: 10px">></baseTitle>
      <el-row gutter="20" style="margin-bottom: -15px;">
        <el-col :span="24">
          <single-file-component tag="需求上报" :fileNameTableWidth="300" :fullscreen="true"
                                 v-model:value="localFormData.singleFile" :processViewer="processViewer"
                                 labelAlign="top"/>
          <!--          <el-form-item label="需求上报申请书" label-position="top" style="display:block;">-->
          <!--            &lt;!&ndash;            style="font-size: 16px"&ndash;&gt;-->
          <!--            <el-button type="primary" link @click="handleDownload(localFormData.singleFile)">-->
          <!--              {{ localFormData.singleFile?.originalFileName }}-->
          <!--            </el-button>-->
          <!--          </el-form-item>-->
        </el-col>
        <el-col :span="24" style="margin-top: -15px;">
          <baseTitle title="附件列表" style="margin-bottom: 0"></baseTitle>
        </el-col>
        <el-col :span="24">
          <file-component
              tag="需求上报" :fileNameTableWidth="300" :fullscreen="true"
              v-model:value="localFormData.fileList"
              :processViewer="processViewer"
              :file-list-show="fileListShow"
              labelAlign="top"
          />
        </el-col>
        <el-col :span="24" style="margin-top: -15px">
          <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>
          <div v-if="data.state==='5'" style="margin-bottom: 15px">
            <baseTitle title="前置流程"></baseTitle>
            <div style="display: flex;align-items: center;flex-wrap: wrap;">
              <div v-for="(item,index) in localFormData.preProcess" :key="item.requestId">
                <a :href="item.baseUrl" target="_blank"
                   style="color: #2a99ff;cursor: pointer">{{ item.requestName }}<span
                    v-if="index != localFormData.preProcess.length -1">，</span>
                </a>
              </div>
            </div>
          </div>
          <div v-perm="['annual:plan:approve']" v-if="data.state==='4'">
            <baseTitle title="前置流程"></baseTitle>
            <select-pre-process :formData="localFormData"/>
            <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">
        <div class="approval-title" style="margin-top: -15px">
          <baseTitle title="审批记录"></baseTitle>
          <div class="diagram">
            <div class="base-title">流程图</div>
            <el-switch
                v-model="changeDiagram"
                style="--el-switch-on-color:#BEA266; --el-switch-off-color:#cecdcd"
            />
          </div>
        </div>
        <div class="process">
          <operation-render v-if="processViewer && data.operationList && data.operationList.length > 0&&!changeDiagram"
                            :isColumn="true"
                            :operation-list="data.operationList"
                            :step="'report'"
                            :state="data.state"/>
          <process-diagram-viewer v-if="processViewer&&changeDiagram" id-name="summaryProcess"/>
        </div>
      </div>
    </el-form>
    <div class="oper-page-btn" v-perm="['annual:plan:approve']" v-if="data.state==='4'">
      <el-button type="danger" @click="handleRejectPlan">驳回年度计划</el-button>
      <el-button color="#DED0B2" @click="handleAgreePlan">通过年度计划</el-button>
    </div>
  </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 "@/components/DetailComponent/FileComponent.vue";
import {ElNotification} from "element-plus";
import {approvePlan} from "@/api/project-demand/summary";

import SelectPreProcess from "@/components/SelectPreProcess.vue";

const emit = defineEmits(['update:value'])
const tagsViewStore = useTagsView()
const cacheStore = useCacheStore()
const props = defineProps({
  formData: {
    type: Object,
    default: {}
  },
  data: {
    type: Object,
    default: {
      state: '1'
    }
  },
  processViewer: {
    type: Boolean,
    default: false
  },
  fileListShow: {
    type: String,
    default: 'READ'
  },
  loading: {
    type: Boolean,
    default: false
  },
  value: {
    type: String,
    default: ''
  }
})
const changeDiagram = ref(false)
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 handleRejectPlan = async () => {
  // const values = form.value.getValues()
  // console.log('route',route.query.projectId)
  if (!_value.value) {
    ElNotification({
      title: '提示',
      message: '请填写审核意见',
      type: 'warning'
    })
    return
  }
  const params = {
    auditOpinion: _value.value,
    projectId: parseInt(route.query.projectId),
    state: false
  }
  console.log('params', params)
  const res = await approvePlan(params)
  ElNotification({
    title: '提示',
    message: res.msg,
    type: res.code === 1000 ? 'success' : 'error'
  })
  tagsViewStore.delVisitedViews(router.currentRoute.value.path)
  router.push({
    name: 'Summary'
  })
}
const handleAgreePlan = async () => {
  const params = {
    auditOpinion: _value.value,
    projectId: parseInt(route.query.projectId),
    state: true
  }
  console.log('params', params)
  const res = await approvePlan(params)
  ElNotification({
    title: '提示',
    message: res.msg,
    type: res.code === 1000 ? 'success' : 'error'
  })
  tagsViewStore.delVisitedViews(router.currentRoute.value.path)
  router.push({
    name: 'Summary'
  })
}
const filterDict = (data, value) => {
  if (data === undefined || value === undefined) return;
  let label = ''
  let result = []
  if (value instanceof Array) {
    value.forEach(item1 => {
      data.find(item => {
        if (item.value == item1) {
          result.push(item.label)
        }
      })
    })
    label = result.map(item => item).join('，')
  } else {
    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(() => {
  props.formData.singleFile = [props.formData.singleFile]
  return Object.keys(props.formData).length && (localFormData.value = props.formData)
})

getFundOptions()
</script>

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

:deep(.el-table__header) {
  .is-leaf:first-child {
    .cell {
      margin-left: -25px !important;
    }
  }
}

:deep(.el-table__body) {
  .el-table__cell:first-child {
    .cell {
      margin-left: -13px !important;
    }
  }
}
</style>
