<template>
  <div class='flex flex-justify-start full-height'>
    <div class="full-height">
      <left-bar :active="type === '0' ? -1 : active " :data="data" :stage="stage" @typeChange="typeChange"/>
    </div>
    <div class="full-height full-width white-bg padding-left padding-top" style="margin-left: 300px">
      <proinfo v-if="type === '0'" :data="data" :issue="list" @refInfo="proInfo" @refresh="issueList"/>
      <folder-info v-else :id="id" :data='data' :projectStageId="projectStageId" />
    </div>
  </div>
</template>

<route>
  {
    name: '项目详情',
    meta: { keepAlive: true }
  }
</route>

<script>
import leftBar from './component/left_bar.vue'
import proinfo from './component/proinfo.vue'
import folderInfo from './component/folder_info.vue'

export default {
  name: '项目详情',
  components: { leftBar, proinfo, folderInfo },
  watch: {
    stage: {
      handler (newValue) {
        if (newValue) {
          this.change(0)
        }
      },
      immediate: true
    }
  },
  data () {
    return {
      type: '0',
      id: '',
      data: {},
      list: [],
      stage: [],
      projectStageId: '',
      active: -1
    }
  },
  created () {
    this.id = this.$route.query.id
    this.type = this.$route.query.type
    this.proInfo()
    this.issueList()
    this.getStage()
    this.$bus.on('change', (index) => {
      this.type = '1'
      this.active = index
      this.projectStageId = this.stage[index] === undefined ? '' : this.stage[index].id
    })
    console.log('created')
  },
  methods: {
    typeChange () {
      this.type = '0'
    },
    /**
     * 项目信息
     */
    proInfo () {
      this.$api.project.projectInfo(this.id).then(res => {
        if (res.code === 200) {
          this.data = res.data
        }
      })
    },
    /**
     * 发行明细
     */
    issueList () {
      this.$api.project.issuanceDetail({ projectId: this.id }).then(res => {
        if (res.code === 200) {
          this.list = res.data.records
        }
      })
    },
    /**
     * 获取项目阶段
     */
    getStage () {
      this.$api.project.includeStage({ projectId: this.id }).then(res => {
        if (res.code === 200) {
          this.stage = res.data.map(e => {
            const newItem = {}
            newItem.title = e.stageName
            newItem.count = e.fileNumber
            newItem.isAccess = e.isAccess
            newItem.id = e.id
            return newItem
          })
        }
      })
    },
    /**
     * 切换项目阶段
     * @param res
     */
    change (res) {
      if (this.$route.query.type !== '0') {
        this.type = '1'
      }
      this.projectStageId = this.stage[res] === undefined ? '' : this.stage[res].id
    }
  }
}
</script>

<style scoped>

</style>
