<template>
  <div v-loading="loading" style="padding: 0 30px">
    <baseTitle title="专项资金详情"></baseTitle>
    <el-form :model="formData" ref="form" >
      <el-row style="margin-left: 15px;margin-bottom: -18px">
        <el-col :span="6">
          <el-form-item label="专项名称">
            <span>{{ formData.name }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="金额(元)">
            <span>{{ toThousands(formData.fundAmount) }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="剩余金额(元)">
            <span>{{ toThousands(formData.residualAmount) }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="专项资金情况说明" >
              <div style="white-space: pre-wrap">{{formData.introduce}}
              </div>
          </el-form-item>
        </el-col>
        </el-row>
      <el-row style="margin-bottom: -18px" class="projects">
        <baseTitle title="关联项目" v-if="!data.taskId"></baseTitle>
        <el-col :span="24" v-if="!data.taskId" >
          <el-form-item >
            <fvTable style="width: 100%;max-height:160px" height="160" v-if="showTable" :tableConfig="projectTable"
                     :data="formData.projects" :isSettingCol="false" :pagination="false">
              <template #empty>
                <el-empty :image-size="55" description="暂无数据" style="padding: 0"/>
              </template>
            </fvTable>
          </el-form-item>
        </el-col>
        <baseTitle title="附件文件"></baseTitle>
        <el-col :span="24">
          <el-form-item>
            <fvTable style="width: 100%;max-height: 160px;"  height="160"  v-if="showTable" :tableConfig="fileTable"
                     :data="formData.files" :isSettingCol="false" :pagination="false">
              <template #empty>
                <el-empty :image-size="55" description="暂无数据" style="padding: 0"/>
              </template>
            </fvTable>
          </el-form-item>
        </el-col>
        <el-col :span="24" style="margin-top: -18px">
          <div v-if="data.taskId">
            <baseTitle title="审核意见"></baseTitle>
            <el-form-item prop="auditOpinion">
              <el-input
                  v-model="formData.auditOpinion"
                  :rows="3"
                  type="textarea"
                  placeholder="请输入审核意见"
              />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <div class="approval-record">
        <div class="approval-title">
          <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"
                            :operation-list="data.operationList"
                            :state="data.state"/>
          <process-diagram-viewer v-if="processViewer&&changeDiagram" id-name="fundProcess"/>
        </div>
      </div>
    </el-form>
    <opinion v-if="data.taskId" :formData="data.formData" :taskId="data.taskId"
             :taskUserOptionList="data.taskUserOptionList"
             v-model:value="formData.auditOpinion"></opinion>
    <file-preview ref="filePreviewRef"  v-if="filePreviewShow" :fileName="filePreviewParam.fileName" :fileUrl="filePreviewParam.fileUrl"
                  :fileType="filePreviewParam.fileType"/>
  </div>
</template>

<script setup lang="jsx">
import {toThousands} from '@/utils/changePrice.js'
import OperationRender from '@/views/workflow/common/OperationRender.vue'
import ProcessDiagramViewer from '@/views/workflow/common/ProcessDiagramViewer.vue'
import {downloadFile} from "@/api/project-demand";

const changeDiagram = ref(false)
const emit = defineEmits(['getInfo', "update:formData"])
const form = ref()
const router = useRouter()

const props = defineProps({
  formData: {
    type: Array,
    default: []
  },
  data: {
    type: Array,
    default: []
  },
  processViewer: {
    type: Boolean,
    default: false
  }, showTable: {
    type: Boolean,
    default: false
  },
  loading: {
    type: Boolean,
    default: false
  }
})
const projectTable = reactive({
  columns: [
    {
      prop: 'index',
      type: 'index',
      label: '序号',
      align: 'center',
      width: '80',
    },
    {
      prop: 'projectName',
      label: '项目名称',
      align: 'center',
      width: 400
    },
    {
      prop: 'specialFundAmount',
      label: '项目金额',
      align: 'center',
      currentRender:({row})=>{
        return <span>{toThousands(row.specialFundAmount)}</span>
      }
    },
    {
      prop: 'startTime',
      label: '项目时间',
      align: 'center'
    },
    {
      prop: 'oper',
      label: '操作',
      align: 'center',
      currentRender: ({row, index}) => {
        return (
            <el-button type="primary" link onClick={() => handleView(row)}>查看</el-button>
        )
      }
    }
  ]
})
const fileTable = reactive({
  columns: [
    {
      prop: 'index',
      type: 'index',
      label: '序号',
      align: 'center',
      width: '80',
    },
    {
      prop: 'originalFileName',
      label: '文件名',
      align: 'center',
      width: 400,
      showOverflowTooltip: false,
      currentRender: ({row, index}) => (<div style="color: #2a99ff;cursor: pointer;" onClick={()=>clickToPreview(row)}>{row.originalFileName}</div>)
    },
    {
      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 filePreviewParam = ref({
  fileUrl: '',
  fileName: '',
  fileType: 'pdf'
})
const filePreviewShow = ref(false)
const clickToPreview=(row)=>{
  filePreviewShow.value = false
  filePreviewParam.value = {
    fileUrl: row.url,
    fileName: row.originalFileName,
    fileType: row.fileType
  }
  nextTick(()=>{
    filePreviewShow.value = true
  })
}
const handleView=(row)=>{
  router.push({
    name: 'Implementation/detail',
    query: {
      id: row.requirementId,
      projectId: row.projectId,
      step: '10'
    }
  })
}
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.loading, (newVal) => {
  props.loading = newVal
}, {deep: true})

watch(() => props.processViewer, (newVal) => {
  props.processViewer = newVal
}, {deep: true})
watch(() => props.showTable, (newVal) => {
  props.showTable = newVal
}, {deep: true})
</script>

<style scoped lang="scss">
.projects{
  :deep(.el-table--fit ) {
    height: 300px !important;
  }
}

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

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