<template>
  <div class="page-contain list-contain">
    <top-nav title="项目文档" :showQuery="false" ></top-nav>
    <div v-if="dataList.length"  class="data-list">
      <div v-for="(item, index) in dataList"
           :key="index"
           class="collapse-box">
        <div class="top">
          <div class="row title">
            <span @click="openPDF(item.fileId)"
                  class="fb"
                  style="text-decoration: underline;">{{item.fileName}}</span>
            <div @click="handleExtend(index)"
                 class="fc8CEE">
              <span v-if="item.isExtend">收起</span>
              <span v-else>更多</span>
              <van-icon :name="item.isExtend?'arrow-up':'arrow-down' " />
            </div>
          </div>
          <div class="row ptb20">
            <span>文件版本：{{item.fileVersion}}</span>
            <span>文件类型：{{item.fileTypeKey}}</span>
          </div>
          <div class="row">
            <span>签订主体：{{item.signMainPartKey}}</span>
            <span>状态：<span class="fc8CEE">{{item.status===1?'有效':'无效'}}</span> </span>
          </div>
        </div>
        <div v-show="item.isExtend"
             class="info-box">
          <div class="info-item">
            <span class="left">文件有效期</span>
            <span class="right">{{format(item.fileIndate, item.isFlag)}}</span>
          </div>
          <div class="info-item">
            <span class="left">附件格式</span>
            <span class="right">{{item.fileSuffix}}</span>
          </div>
          <div class="info-item">
            <span class="left">文件大小</span>
            <span class="right ">{{(item.fileSize / 1024).toFixed(2)}} KB</span>
          </div>
          <div class="info-item">
            <span class="left">上传时间</span>
            <span class="right">{{item.createTime}}</span>
          </div>
          <div class="info-item">
            <span class="left">上传人员</span>
            <span class="right">{{item.updater}}</span>
          </div>
          <div class="info-item">
            <span class="left">备注说明</span>
            <span class="right">{{item.remark}}</span>
          </div>
        </div>
      </div>
    </div>
    <van-empty v-if="!dataList.length && showNoData"  description="暂无数据！" />

  </div>
</template>

<script>

export default {
  name: 'project-document',

  created(){
    this.projectId = this.$route.params.projectId;
    this.uid = this.$common.getUrlParams('uid') || 'admin';
    this.getProjectFile();
  },
  methods: {
    getProjectFile () {
      let param = {
        busId: this.projectId,
        modelId: 3101
      }
      this.$axios.post({
        domain: 'crmUrl',
        url: 'project.listProjectFile',
        data: param,
        requestType: 'body',
        isShow: true,
        success: function (res) {
          if (res.errorCode === 200) {
            this.dataList = res.data || []
            this.dataList.forEach(item => {
              this.$set(item, 'isExtend', true)
            })
          } else {
            this.$toast({ type: 'fail', message: res.msg })
          }
        }.bind(this),
        error: function (err) {
          this.$toast({ type: 'fail', message: err, duration: 3000 })
        }.bind(this),
        complete: function () {
          this.showNoData = true;
        }.bind(this)
      })
    },
    format (fileIndate, isFlag) {
      if (fileIndate) {
        if (this.$common.compareDate(fileIndate)) {
          return '已失效'
        } else {
          return '生效中'
        }
      } else if (isFlag) {
        if (isFlag === 1) {
          return '生效中'
        }
      }
    },
    getFileUrl (fileId) {
      return new Promise((resolve, reject) => {
        let param = {
          fileId: fileId,
          busSystem: 'cloud-crm-project',
          busModel: 'cloud-crm-project',
          uid: this.uid
        }
        this.$axios.post({
          domain: 'attachUrl',
          url: 'downLoadFile',
          data: param,
          requestType: 'form',
          isShow: true,
          success: function (res) {
            if (res.errorCode === 200) {
              resolve(res.data)
            } else {
              this.$toast({ type: 'fail', message: res.msg })
            }
          }.bind(this),
          error: function (err) {
            reject(err)
            this.$toast({ type: 'fail', message: err, duration: 3000 })
          }.bind(this)
        })
      })
    },
    // 文件预览
    async openPDF (fileId) {
      let url = await this.getFileUrl(fileId)
      let appVersion = longsysWebview.getAppVersion()
      if (!(appVersion >= '1.0.32')) {
        // alert('当前APP版本过低，请将APP升级为最新版');
        // return;
        bsAudit.openWin(link)
        return
      }
      longsysWebview.openReader({
        url: url
      })
    },
    handleExtend (index) {
      this.dataList[index].isExtend = !this.dataList[index].isExtend
    },


  },
  data () {
    return {
      projectId: '',
      uid: '',
      dataList: [],
      showNoData: false,
    }
  }
}
</script>

<style scoped  lang="scss">
</style>
