<template>
  <div>
    <cy-info-title>
      授权书
      <template #right>
        <el-button v-if="!disabled" type="text" @click="handleRefresh">刷新授权状态</el-button>
      </template>
    </cy-info-title>

    <div v-for="(item, index) in deatailList" :key="index" class="sign-link-box">
      <div class="main-title">{{ getContractTypeDesc(item.type) }}</div>
      <div class="sign-item">
        <div class="name">{{ item.name }}</div>
        <div class="title">
          <div>认证链接</div>
          <div>签署链接</div>
        </div>
        <div class="link">
          <div class="two-link">
            <div>
              <div style="overflow: hidden;text-overflow: ellipsis; white-space: no-wrap;">
                {{ item.authShortUrl }}
              </div>
              <el-button v-if="!disabled" v-clipboard:copy="item.authShortUrl" v-clipboard:success="copySuccess" type="text" icon="iconfont icon-a-Group779" />
            </div>
            <div>
              <div>
                认证状态: {{ getAuthStatusDesc(item.authStatus) }}
              </div>
            </div>
          </div>
          <div class="two-link">
            <div>
              <div style="overflow: hidden;text-overflow: ellipsis; white-space: no-wrap;">
                {{ item.creditShortUrl }}
              </div>
              <el-button
                v-if="!disabled"
                v-clipboard:copy="item.creditShortUrl"
                v-clipboard:success="copySuccess"
                type="text"
                icon="iconfont icon-a-Group779"
              />
            </div>
            <div>
              <div>
                签署状态: {{ getSignStatusDesc(item.creditSignStatus) }}
              </div>
            </div>
          </div>
        </div>

      </div>
      <!-- <div class="sign-last">
        <div class="sing-last-desc">人脸识别像</div>
        <div class="sing-last-file">
          <cy-upload
            ref="cardFrontRef"
            :upload-params="{
              fileType: 'smb_electronic_contract_person_face_fie',
              mouldName: 'bocom'
            }"
            :file-data="item.faceFile"
            :upload-from-orange-status="true"
            :limit="1"
            :disabled="disabled"
            class="sign-file"
            placeholder="请上传人脸识别影像"
          />
        </div>
      </div> -->
    </div>
  </div>
</template>

<script>
import { cloneDeep } from '@/utils'
import { refreshAuthStatus } from '@/api/myStoreEnterprise'

export default {
  props: {
    disabled: {
      type: Boolean,
      default: false
    },
    detailsInfo: {
      type: Array,
      default: () => []
    },
    authBookId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      deatailList: []
    }
  },
  watch: {
    detailsInfo: {
      handler(val) {
        if (val && val.length) {
          console.log('auth book ---------------')
          console.log(val)
          this.deatailList = val

          // this.deatailList.forEach((el) => {
          //   const faceTemp = []
          //   faceTemp.push(el.faceFile)
          //   el.faceFile = faceTemp
          // })
        }
      },
      immediate: true
    }
  },
  methods: {
    // 刷新授信状态
    async handleRefresh() {
      const res = await refreshAuthStatus(this.$route.query.zrId)

      if (res.success) {
        this.deatailList = res.data
      }
    },
    copySuccess() {
      this.$message.success('复制成功')
    },
    getContractTypeDesc(type) {
      const map = {
        0: '企业征信查询授权书',
        1: '法人征信查询授权书',
        2: '实控人征信查询授权书'
      }
      return map[type] || ''
    },
    getSignStatusDesc(status) {
      if (status === false) {
        return '未签署'
      } else if (status === true) {
        return '已签署'
      } else {
        return ''
      }
    },
    getAuthStatusDesc(status) {
      if (status === 0) {
        return '未认证'
      } else if (status === 1) {
        return '已认证'
      } else {
        return ''
      }
    },
    getParams(type) {
      return new Promise((resolve) => {
        const cloneDetailList = cloneDeep(this.deatailList)
        // 校验状态 -- 必须是已签署且已认证 才可以提交
        // if (this.deatailList.length) {
        //   cloneDetailList.forEach((el, index) => {
        //     if (el.faceFile.length > 0) {
        //       el.faceFile = el.faceFile[0]
        //     }
        //   })
        // }
        if (type === 'temp') {
          resolve(cloneDetailList)
        } else {
          const list = cloneDetailList.filter(item => item.authStatus === 1 && item.creditSignStatus === true)
          if (list.length === 0) {
            this.$message.error('必须是已签署且已认证的授权书')
            return
          }
          resolve(cloneDetailList)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.sign-link-box {
  border-top: 1px solid #E4E7ED;
  box-sizing: border-box;
  margin: 20px 0px;
  .main-title {
    padding: 0 20px;
    box-sizing: border-box;
    height: 40px;
    line-height: 40px;
    border-left: 1px solid #E4E7ED;
    border-right: 1px solid #E4E7ED;
    border-bottom: 1px solid #E4E7ED;
  }
  .sign-item {
    display: flex;
    .name {
      display: flex;
      align-items: center;
      background: #f5f6f6;
      padding: 0 20px;
      min-width: 200px;
      width: 200px;
      box-sizing: border-box;
      border-bottom: 1px solid #E4E7ED;
      border-right: 1px solid #E4E7ED;
      border-left: 1px solid #E4E7ED;
      &:last-child {
        border-bottom: none;
      }
    }
    .title {
      line-height: 40px;
      height: 40px;
      min-width: 120px;
      width: 120px;
      & > div {
        border-bottom: 1px solid #E4E7ED;
        border-right: 1px solid #E4E7ED;
        box-sizing: border-box;
        padding: 0 20px;
      }

    }
    .link {
      line-height: 40px;
      flex: 1;
      & > div {
        border-bottom: 1px solid #E4E7ED;
        border-right: 1px solid #E4E7ED;
        padding: 0 20px;
      }
      .two-link {
        display: flex;
        & > div {
          height: 40px;
          flex: 1;
          border-right: 1px solid #E4E7ED;
          display: flex;
          justify-content: space-between;
          align-items: center;
          padding-right: 20px;
          &:last-child {
            padding-left: 20px;
            padding-right: 0px;
            border-right: none;
          }
        }
      }
      .one-link {
        display: flex;
        justify-content: space-between;
        align-items: center;
        height: 40px;
      }
    }
  }
  .sign-last {
    display: flex;
    align-items: center;
    .sing-last-desc {
      padding: 0 20px;
      box-sizing: border-box;
      width: 200px;
      height: 40px;
      line-height: 40px;
      background: #f5f6f6;
      box-sizing: border-box;
      border-bottom: 1px solid #E4E7ED;
      border-right: 1px solid #E4E7ED;
      border-left: 1px solid #E4E7ED;
    }
    .sing-last-file {
      display: flex;
      align-items: center;
      flex: 1;
      height: 40px;
      line-height: 40px;
      border-bottom: 1px solid #E4E7ED;
      border-right: 1px solid #E4E7ED;
      .cy-upload.sign-file >>> .upload-from-orange-status {
        border: none !important;
      }
    }
  }
}
</style>
