<template>
  <div class="progressDetail" v-if="pageShow">
    <div class="status" v-if="info.approvalStatus === '1'">
      <h2>审批通过：</h2>
      <div class="status-content">
        <span>[{{ info.processDefName }}]已签署完成，</span>
        <i @click="$linkTo('./progressAnnex?img=' + info.pdfPicFile.url + '&name=' + info.processDefName)">点击查看和保存</i>
      </div>
    </div>
    <div class="info">
      <h2 :style="{maxWidth: info.approvalStatus !== '1' ? '100%' : '550rpx'}">{{ info.processDefName }}</h2>
      <AiIcon class="info-status" v-if="info.approvalStatus === '1'" size="96" icon="iconsp-pass"></AiIcon>
      <div class="info-user">
        <image v-if="info.createUserAvatar" :src="info.createUserAvatar"/>
        <span v-else>{{ nameAvatar(info.createUserName) }}</span>
        <div class="inf-user__right flex1">
          <h2>{{ info.createUserName }}</h2>
          <p>{{ info.createTime }}</p>
        </div>
        <i :style="{color: mapStatusColor(info.approvalStatus)}"
           v-if="info.approvalStatus !== '1'">{{ $dict.getLabel('approvalStatus', info.approvalStatus) }}</i>
      </div>
      <div class="info-list">
        <div class="info-item">
          <span>所属类型：</span>
          <div>{{ info.classificationName || '-' }}</div>
        </div>
        <div class="info-item">
          <span>所属部门：</span>
          <div>{{ $dict.getLabel('hbDepartment', info.department) || '-' }}</div>
        </div>
        <div class="info-item">
          <span>审批编号：</span>
          <div>{{ info.serialNumber }}</div>
        </div>
        <div class="info-item">
          <span>附件资料：</span>
          <div v-if="!!info.annexs && info.annexs.length">
            <div class="imgs" v-for="(item, index) in info.annexs" :key="index">
              <img :src="item.annexFile.url" mode="aspectFill" @click="preview(info.annexs, item.annexFile.url)">
              <span>{{ item.annexName }}</span>
            </div>
          </div>
          <div v-else>暂无附件</div>
        </div>
      </div>
    </div>
    <div class="detail">
      <div class="detail-tab">
        <div @click="currIndex = 0" :class="[currIndex === 0 ? 'active' : '']">申请表单</div>
        <div @click="currIndex = 1" :class="[currIndex === 1 ? 'active' : '']">流程信息</div>
      </div>
      <!--申请表单-->
      <div class="detail-form" v-if="currIndex==0">
        <div class="detail-form__item" v-for="(item, index) in tableForm" :key="index">
          <div class="detail-form__item--title" @click="showMore(index)">
            <div class="left">
              <span></span>
              <h2>{{ item[0].groupName }}</h2>
            </div>
            <i class="iconfont" :class="[index === formIndex ? 'title-active' : '']">&#xe696;</i>
          </div>
          <div class="detail-form__item--list" v-show="index === formIndex">
            <div class="detail-form__item--info" v-for="(field, i) in item" :key="i">
              <h2>{{ field.fieldName }}{{ field.fieldNameSuffix || '' }}</h2>
              <span v-if="!field.dictionaryCode">{{ field.fieldValue || '-' }}</span>
              <span v-if="field.dictionaryCode && field.fieldDataType !== '5'">{{
                  $dict.getLabel(field.dictionaryCode, field.fieldValue) || '-'
                }}</span>
              <span v-if="field.dictionaryCode && field.fieldDataType === '5'">{{
                  getDictValue(field.dictionaryCode, field.fieldValue)
                }}</span>
            </div>
          </div>
        </div>
      </div>
      <!--流程信息-->
      <div class="detail-list" v-if="currIndex==1">
        <div class="step-item" v-for="(item, index) in stepList" :key="index">
          <div class="line"></div>
          <div class="detail-left">
            <image class="avatar" v-if="item.stepAvatar && item.userName" :src="item.stepAvatar"/>
            <span v-if="!item.stepAvatar && item.userName">{{ item.userName.substr(item.userName.length - 2) }}</span>
            <image v-if="index !== 0 && item.candidates && item.candidates.length > 1" class="detail-left__statusicon"
                   src="/static/img/notice.png"/>
            <image class="avatar"
                   v-if="index !== 0 && item.candidates && item.candidates.length === 1 && item.candidates[0].avatar"
                   :src="item.candidates[0].avatar"/>
            <span v-if="index !== 0 && item.candidates && item.candidates.length === 1 && !item.candidates[0].avatar">{{
                item.candidates[0].name.substr(item.candidates[0].name - 2)
              }}</span>
            <image class="detail-left__icon" src="/static/img/gou.png"
                   v-if="index === 0 || ['0', '1', '6', '5'].indexOf(item.stepType) > -1"/>
            <image class="detail-left__icon" src="/static/img/point.png" v-else-if="item.stepType !== '2'"/>
          </div>
          <div class="detail-right">
            <div class="detail-right__title">
              <h2>{{ item.title }}</h2>
              <span v-if="item.approvalTime">{{ item.approvalTime }}</span>
            </div>
            <div class="detail-right__subtitle" v-if="item.stepType === '0'">
              <span>{{ item.userName || '-' }}</span>
              <i>({{ stepTypeList[item.stepType] }})</i>
            </div>
            <div class="detail-right__subtitle" v-else-if="item.stepType !== '2'">
              <span>{{ item.title2 }}</span>
              <i v-if="item.title2Desc" :style="{color: mapColor(item.stepType)}">{{ item.title2Desc }}</i>
            </div>
            <div class="detail-right__subtitle" v-else-if="item.stepType === '2'">
              <span>{{ item.title2 }} </span>
              <i style="color: #F14242"> {{ item.title2Desc }}</i>
            </div>
            <div class="detail-right__subtitle" v-if="item.stepType === '2'">
              <span>审批意见</span>
              <i style="color: #F14242;">{{ item.opinion || '-' }}</i>
            </div>
            <div class="detail-right__subtitle detail-right__text" style="display: block"
                 v-if="item.candidateFieldInfos && item.candidateFieldInfos.length"
                 v-for="(candidateField, z) in item.candidateFieldInfos" :key="z">
              <span>{{ candidateField.fieldName }}</span>
              <i style="color: #343D65;display: contents;">{{
                  candidateField.dictionaryCode ? $dict.getLabel(candidateField.dictionaryCode, candidateField.fieldValue) || '-' : candidateField.fieldValue || '-'
                }}</i>
            </div>
            <div class="detail-right__imgs" v-if="item.pictureFiles && item.pictureFiles.length">
              <image :src="img.url" mode="aspectFill" v-for="(img, i) in item.pictureFiles" :key="i"
                     @click="previewPic(item.pictureFiles, img.url)"/>
            </div>
            <div class="detail-right__doc" v-if="item.annexFiles && item.annexFiles.length">
              <div class="doc-item" v-for="(file, j) in item.annexFiles" :key="j" @click="saveFile(file)">
                <h2>{{ file.name }}</h2>
                <span>{{ (file.size / 1024).toFixed(2) }}KB</span>
              </div>
            </div>
            <div class="detail-right__userlist" v-if="item.candidates && item.candidates.length">
              <div class="detail-right__userinfo" v-for="(user, z) in item.candidates" :key="z">
                <image v-if="user.avatar" :src="user.avatar"/>
                <span v-else>{{ user.name.substr(user.name.length - 2) }}</span>
                <h2>{{ user.name }}</h2>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "progressDetail",
  appName: "进度详情",
  data() {
    return {
      title: '',
      currIndex: 0,
      id: '',
      info: {},
      formIndex: 0,
      tableForm: [],
      pageShow: false,
      nodeApprovalStatusList: {
        '0': '待审批',
        '1': '审批通过',
        '2': '审批驳回'
      },
      stepTypeList: {
        '0': '发起',
        '1': '审批通过',
        '2': '审批拒绝',
        '3': '抄送',
        '4': '转办审批中',
        '5': '转办审批同意',
        '6': '转办审批拒绝',
        '7': '发起人撤回',
        '8': '待审批',
        '9': '未到达该节点'
      },
      stepList: []
    }
  },

  onLoad(query) {
    this.id = query.id
    this.getInfo(query.id)
  },
  methods: {
    showMore(index) {
      if (index === this.formIndex) {
        this.formIndex = -1
      } else {
        this.formIndex = index
      }
    },

    mapStatusColor(status) {
      return {
        '0': '#FF9B2B',
        '1': '#3078E1',
        '2': '#F14242'
      }[status]
    },

    mapColor(status) {
      if (status === '1') {
        return '#2EA222'
      }

      if (status === '2' || status === '6' || status === '7') {
        return '#F14242'
      }

      return '#FF8822'
    },

    saveFile(file) {
      if (['.png', '.jpg', '.jpeg'].indexOf(file.postfix) > -1) {
        uni.previewImage({
          urls: [file.url],
          current: file.url
        })

        return false
      }

      uni.downloadFile({
        url: file.url,
        success: res => {
          const filePath = res.tempFilePath
          this.$hideLoading()
          wx.openDocument({
            filePath: filePath,
            success: function (res) {
              console.log('打开文档成功')
            }
          })
        },
        fail: () => {
          this.$hideLoading()
          this.$toast('保存失败')
        }
      })
    },

    previewPic(imgList, img) {
      uni.previewImage({
        urls: imgList.map(v => v.url),
        current: img
      })
    },

    preview(imgList, img) {
      uni.previewImage({
        urls: imgList.map(v => v.annexFile.url),
        current: img
      })
    },

    getDictValue(dictKey, value) {
      return value.split(',').map(item => {
        return this.$dict.getLabel(dictKey, item)
      }).join(',')
    },

    getInfo(id) {
      this.$loading()
      this.$instance.post(`/app/approv-alapply-info/info-id-table?id=${id}`).then(res => {
        if (res.code === 0) {
          const groupFormIds = this.unique(res.data.tableInfo.tableFieldInfos.filter(v => v.fieldType !== '1').map(v => v.groupIndex))
          const dictKeys = res.data.tableInfo.tableFieldInfos.filter(v => !!v.dictionaryCode).map(v => v.dictionaryCode)
          this.$dict.load([...dictKeys, 'hbDepartment', 'nodeApprovalStatus', 'nodeType']).then(() => {
            this.info = res.data
            this.$instance.post(`/app/approv-alapply-info/processinfo-id2?id=${id}`).then(res => {
              if (res.code === 0) {
                let dictKeys = []
                res.data.forEach(item => {
                  if (item.candidateFieldInfos && item.candidateFieldInfos.length) {
                    item.candidateFieldInfos.forEach(v => {
                      v.dictionaryCode && dictKeys.push(v.dictionaryCode)
                    })
                  }
                })

                if (dictKeys.length) {
                  this.$dict.load([...dictKeys]).then(() => {
                    this.stepList = res.data
                  })
                } else {
                  this.stepList = res.data
                }
              }
            })
            this.pageShow = true
          })
          this.tableForm = groupFormIds.map(index => {
            return res.data.tableInfo.tableFieldInfos.filter(v => v.fieldType !== '1' && v.groupIndex === index)
          })
        }
      })
    },

    /**
     * 数组去重
     */
    unique(arr) {
      return arr.filter((item, index) => {
        return arr.indexOf(item, 0) === index
      })
    },
    nameAvatar(name) {
      return name?.split("")?.slice(-2) || "游客"
    }
  }
}
</script>

<style scoped lang="scss">
div, label, i, view {
  box-sizing: border-box;
}

.progressDetail {
  padding-bottom: 40px;
}

.ellipsis {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.detail-list {
  padding: 40px 32px;
}

.step-item {
  display: flex;
  position: relative;
  padding-bottom: 88px;

  &:last-child {
    padding-bottom: 0;

    .line {
      display: none;
    }
  }

  .detail-right__doc {
    padding-top: 8px;

    .doc-item {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 16px;
      padding: 16px;
      background: #F7F7F7;
      border-radius: 4px;
      border: 1px solid #DDDDDD;

      &:last-child {
        margin-bottom: 16px;
      }

      span {
        color: #999999;
        font-size: 24px;
      }

      h2 {
        max-width: 444px;
        line-height: 32px;
        color: #333333;
        font-size: 24px;
        word-break: break-all;
      }
    }
  }

  .detail-right {
    flex: 1;

    .detail-right__title {
      display: flex;
      justify-content: space-between;
      margin-bottom: 6px;

      span {
        color: #999999;
        font-size: 28px;
      }

      & > h2 {
        color: #333333;
        font-size: 32px;
        font-weight: 600;
      }
    }

    .detail-right__userlist {
      margin-top: 16px;
      font-size: 0;

      .detail-right__userinfo {
        display: inline-block;
        margin-right: 32px;
        margin-bottom: 32px;
        text-align: center;

        image, span {
          display: inline-block;
          width: 64px;
          height: 64px;
          line-height: 64px;
          border-radius: 50%;
          margin-bottom: 8px;
          text-align: center;
          color: #FFFFFF;
          font-size: 26px;
          border-radius: 50%;
          background: #2266FF;
        }

        h2 {
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
          color: #666666;
          font-size: 24px;
        }
      }
    }

    .detail-right__subtitle {
      display: flex;
      margin-bottom: 6px span {
        margin-right: 10px;
        color: #666666;
        font-size: 28px;
      }

      i {
        flex: 1;
        text-align: justify;
        word-break: break-all;
        color: #2EA222;
        font-size: 28px;
      }
    }

    .detail-right__text {
      // color: #343D65;
      // font-size: 28px;
      // margin: 6px 0;
    }

    .detail-right__imgs {
      font-size: 0;

      image {
        display: inline-block;
        width: 136px;
        height: 136px;
        margin-right: 8px;
        margin-bottom: 8px;
        margin-top: 8px;
        border-radius: 4px;
      }
    }
  }

  .line {
    position: absolute;
    left: 38px;
    top: 80px;
    width: 4px;
    height: 100%;
    background: #EEEEEE;
  }

  .detail-left {
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    width: 80px;
    height: 80px;
    margin-right: 40px;
    flex-shrink: 1;
    border-radius: 50%;
    background: #2266FF;

    .detail-left__statusicon {
      width: 48px;
      height: 48px;
    }

    .detail-left__icon {
      position: absolute;
      top: 48px;
      right: -4px;
      width: 36px;
      height: 36px;
    }

    span, .avatar {
      display: block;
      width: 80px;
      height: 80px;
      line-height: 80px;
      color: #fff;
      font-size: 28px;
      text-align: center;
      border-radius: 50%;
      background: #2266FF;
    }
  }
}

.status {
  width: 100%;
  padding: 20px 32 px20px;
  font-weight: 600;
  background: #EFF4FF;

  h2 {
    margin-bottom: 10px;
    color: #3A7EE2;
    font-size: 28px;
  }

  .status-content {
    span {
      display: inline;
      font-size: 28px;
      color: #3A7EE2;
    }

    i {
      display: inline;
      font-size: 28px;
      color: #3A7EE2;
      text-decoration: underline;
    }
  }
}

.info {
  position: relative;
  padding: 32px 32px 20px;
  background: #fff;

  & > h2 {
    max-width: 550px;
    margin-bottom: 8px;
    font-size: 32px;
    font-weight: 600;
    color: #333333;
  }

  .info-status {
    position: absolute;
    right: 0;
    top: 0;
    font-size: 96px;;
  }

  .info-user {
    display: flex;
    align-items: center;
    height: 112px;
    border-bottom: 1px solid #D8DDE6;

    i {
      font-size: 28px;
      color: #FF8822;
    }

    span, image {
      width: 80px;
      height: 80px;
      line-height: 80px;
      margin-right: 16px;
      text-align: center;
      color: #FFFFFF;
      font-size: 28px;
      border-radius: 50%;
      background: #2266FF;
    }

    p {
      margin-top: 4px;
      font-size: 24px;
      color: #999999;
    }

    h2 {
      color: #333333;
      font-size: 32px;
    }
  }

  .info-list {
    padding-top: 32px;

    .info-item {
      display: flex;
      margin-bottom: 16px;

      &:first-child {
        div {
          color: #1365DD;
        }
      }

      .imgs {
        display: inline-block;
        position: relative;
        margin-right: 8px;
        margin-bottom: 8px;
        font-size: 0;

        image {
          width: 170px;
          height: 170px;
        }

        span {
          position: absolute;
          bottom: 0;
          left: 0;
          width: 100%;
          height: 40px;
          line-height: 40px;
          text-align: center;
          color: #fff;
          font-size: 20px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          background: rgba(0, 0, 0, 0.4);
        }
      }

      span {
        flex-shrink: 1;
        color: #999999;
        font-size: 30px;
      }

      & > div {
        flex: 1;
        color: #343D65;
        font-size: 30px;
      }
    }
  }
}

.detail {
  margin-top: 16px;
  background: #fff;

  .detail-tab {
    display: flex;
    align-items: center;
    height: 96px;
    border-bottom: 1px solid #D8DDE6;

    div {
      flex: 1;
      height: 100%;
      line-height: 96px;
      text-align: center;
      color: #999999;
      font-size: 34px;
      font-weight: 600;
      border-bottom: 4px solid transparent;

      &.active {
        color: #1365DD;
        border-bottom: 4px solid #1365DD;
      }
    }
  }

  .detail-form {
    .detail-form__item {
      border-bottom: 1px solid #D8DDE6;

      &:last-child {
        border: none;
      }
    }

    .detail-form__item--title {
      display: flex;
      align-items: center;
      justify-content: space-between;
      height: 96px;
      padding: 0 32px;

      .left {
        display: flex;
        align-items: center;

        span {
          width: 4px;
          height: 32px;
          margin-right: 12px;
          background: #1365DD;
        }

        h2 {
          color: #333333;
          font-size: 32px;
        }
      }

      i {
        position: relative;
        font-size: 32px;
        color: #cfcfcf;
        transform: rotate(180deg);
        transition: all 0.3s ease;

        &.title-active {
          transform: rotate(-180deg);
        }
      }
    }

    .detail-form__item--list {
      background: #FAFAFA;
      padding: 0 32px;

      .detail-form__item--info {
        display: flex;
        justify-content: space-between;
        padding: 10px 0;

        span {
          text-align: right;
          max-width: 500px;
          padding-left: 10px;
          word-break: break-all;
        }
      }
    }
  }
}
</style>
