<template>
  <div class="detail-block">
    <el-form :model="formData" label-width="auto">
      <baseTitle title="需求征集详情"></baseTitle>
      <div class="left-info">
        <el-row>
          <el-col :span="12">
            <el-form-item label="名称">
              <span>{{ formData.requirementName }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属公司">
              <span>{{ formData.companyIds }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="征集类型">
              <span>{{ formData.collectType }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="截止时间">
              <span>{{ formData.deadline }}</span>
            </el-form-item>
          </el-col>
          <baseTitle title="征集说明"></baseTitle>
          <el-col :span="24">
            <el-form-item>
              <el-card style="width: 100%">
                <div v-html="formData.collectExplain">
                </div>
              </el-card>
            </el-form-item>
          </el-col>
          <baseTitle title="附件列表"></baseTitle>
          <el-col :span="24">
            <el-form-item>
              <el-table :data="formData.fileList" style="width: 100%">
                <el-table-column label="序号" type="index" align="center" width="80"/>
                <el-table-column prop="fileName" label="文件名" align="center"/>
                <el-table-column prop="tag" label="标签" align="center"/>
                <el-table-column prop="size" label="文件大小" align="center">
                  <template #default="scope">
                    {{ parseInt(scope.row.size / 1024) }}KB
                  </template>
                </el-table-column>
                <el-table-column align="center" label="操作">
                  <template #default="scope">
                    <a :href="scope.row.url">
                      下载
                    </a>
                  </template>
                </el-table-column>
              </el-table>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <div v-if="processInstanceData.taskId">
              <baseTitle title="审核意见"></baseTitle>
              <el-form-item>
                <el-input
                    v-model="auditOpinion"
                    :rows="3"
                    type="textarea"
                    placeholder="请输入审核意见"
                />
              </el-form-item>
            </div>
          </el-col>
        </el-row>
      </div>
      <div class="approval-record">
        <baseTitle title="审批记录"></baseTitle>
        <div class="process">
          <operation-render v-if="processDiagramViewer" :operation-list="processInstanceData.operationList"
                            :state="processInstanceData.state"/>
          <process-diagram-viewer v-if="processDiagramViewer"/>
        </div>
      </div>
    </el-form>
    <div class="oper-page-btn" v-if="processInstanceData.state === '1' && processInstanceData.taskId">
      <el-button @click="handleReject">驳回</el-button>
      <el-button color="#DED0B2" @click="handleSubmit">同意</el-button>
    </div>
  </div>
</template>

<script setup lang="jsx">
import OperationRender from '@/views/workflow/common/OperationRender.vue'
import ProcessDiagramViewer from '@/views/workflow/common/ProcessDiagramViewer.vue'
import {useProcessStore} from '@/stores/processStore.js';
import {getInfo, agreeTask, rejectTask} from "@/api/project-demand/index.js";
import {getSubCompOpt} from '@/api/user/user.js'

const route = useRoute()
const form = ref();
const processStore = useProcessStore()
const companyOption = ref([])
const processInstanceData = ref({})
const processDiagramViewer = ref(false)
const processTree = ref()
const companyNameArray = ref([])
const formData = ref({})
const auditOpinion = ref('')
const handleSubmit = () => {
  let approve = {
    taskId: processInstanceData.value.taskId,
    auditOpinion: auditOpinion.value,
    formData: formData.value
  }
  agreeTask(approve).then(res => {
    console.log(res)
  })
}
const handleReject = () => {
  let approve = {
    taskId: processInstanceData.value.taskId,
    auditOpinion: auditOpinion.value,
  }
  rejectTask(approve).then(res => {
    console.log(res)
  })
}
const getCompanyOption = async () => {
  const res = await getSubCompOpt()
  companyOption.value = res.data
}

const matterTree = (data, id) => {
  if(id){
    for (let i = 0; i < data.length; i++) {
      if (data[i].value == id) {
        companyNameArray.value.push(data[i].label);
      }
      if (data[i].children && data[i].children.length > 0) {
        matterTree(data[i].children)
      }
    }
    return companyNameArray.value;
  }
}

const getDataSourceOptionItem = (val) => {
  if (val !== undefined) {
    val.forEach(item => {
      matterTree(companyOption.value,item)
    })
  }
  return companyNameArray.value.join('，');
}
const init = async () => {
  await getCompanyOption()
  getInfo(route.query.id).then(res => {
    let data = res.data
    formData.value = data.formData;
    data.formData.companyIds= getDataSourceOptionItem(data.formData.companyIds)
    processInstanceData.value = data
    processStore.setDesign(data)
    processStore.runningList.value = data.runningList;
    processStore.endList.value = data.endList;
    processStore.noTakeList.value = data.noTakeList;
    processStore.refuseList.value = data.refuseList;
    processStore.passList.value = data.passList;
    nextTick(() => {
      processDiagramViewer.value = true
    })
  })
}
init()
</script>

<style lang="scss" scoped>
a {
  cursor: pointer;
  font-size: 14px;
  color: #2a99ff;
}

.detail-block {
  overflow: hidden;
  padding-right: 10px;

  .left-info {
    .info {
      display: flex;
      flex-wrap: wrap;

      > div {
        width: 350px;
        margin-bottom: 15px;
        margin-right: 10px;

        > span:first-child {
          color: black;
          font-size: 16px;
          font-weight: bold;
        }
      }
    }
  }

  .approval-record {
    padding-bottom: 30px;
    .process {
      position: relative;
    }
  }
}

</style>
