<template>
  <div class="wrapper">
    <h2>上传资料</h2>
    <div class="content">
      <div class="subtitle">
        <p>
          <span>*</span>
          <span>主贷人征信报告</span>
          <span>(近10天，详版)</span>
        </p>
        <a class="btn-help" href="javascript:;" style="visibility: hidden;">如何获取？</a>
      </div>
      <MyUploader :maxCount="2"
                  :uploadType="uploadType0"
                  @funSetUploadData="funSetUploadData"/>
    </div>
    <div class="content">
      <div class="subtitle">
        <p>
          <span>*</span>
          <span>上传房产信息凭证照片证件</span>
        </p>
      </div>
      <MyUploader :maxCount="2"
                  :uploadType="uploadType1"
                  @funSetUploadData="funSetUploadData"/>
    </div>
    <button class="submit" :class="creditReport.length && houseProperty.length ? 'active' : 'disabled'"
            @click="funSubmit">提交
    </button>
  </div>
</template>

<script>
  import MyUploader from './components/myUploader'
  import { submitData } from './api'
  import { Toast } from 'vant'

  export default {
    name: 'attest',
    components: {
      MyUploader
    },
    props: {
      id: {
        type: String,
        default: ''
      }
    },
    data () {
      return {
        uploadType0: 'credit',
        uploadType1: 'house',
        creditReport: [],
        houseProperty: [],
        arrErrorList: []
      }
    },
    async mounted () {
      await this.$nextTick()
      this.$refreshTitle('进度查询')
    },
    methods: {
      funSetUploadData (type, value) {
        if (type === this.uploadType0) {
          this.creditReport = value
        } else if (type === this.uploadType1) {
          this.houseProperty = value
        }
      },
      verifyData () {
        const { creditReport, houseProperty } = this
        this.arrErrorList = []
        if (!creditReport.length) {
          this.arrErrorList.push('请上传主贷人征信报告')
        }
        if (!houseProperty.length) {
          this.arrErrorList.push('请上传房产信息凭证照片证件')
        }
        return this.arrErrorList.length <= 0
      },
      funSubmit () {
        const postData = {
          id: this.id,
          checkContent1: this.creditReport.map(item => item.url),
          checkContent2: this.houseProperty.map(item => item.url)
        }
        if (!this.verifyData()) {
          Toast({
            message: this.arrErrorList[0],
            forbidClick: true
          })
          return
        }
        const myToast = Toast.loading({
          message: '提交中...',
          duration: 1000 * 100,
          forbidClick: true
        })
        submitData(postData).then(res => {
          myToast.clear()
          if (res.status) {
            Toast({
              type: 'success',
              message: '提交成功',
              forbidClick: true,
              onClose: () => {
                this.$router.replace({ path: `/loan/detail/${this.id}` })
              }
            })
            return
          }
          Toast(res.msg)
        }).catch(err => {
          myToast.clear()
          Toast(err.msg)
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    min-height: 100vh;
    padding-top: 24px;
    background: #fff;
  }

  h2 {
    width: 327px;
    margin-bottom: 38px;
    line-height: 33px;
    font-size: 24px;
    font-weight: 500;
    color: #333;
  }

  .content {
    width: 327px;
    padding-bottom: 41px;

    .subtitle,
    p {
      display: flex;
      align-items: center;
    }

    .subtitle {
      justify-content: space-between;
      margin-bottom: 6px;
    }

    span:nth-of-type(1) {
      line-height: 33px;
      font-size: 24px;
      font-weight: 500;
      color: #FF3B30;
    }

    span:nth-of-type(2) {
      margin-left: 3px;
      margin-bottom: 4px;
      line-height: 22px;
      font-size: 16px;
      font-weight: 500;
      color: #333;
    }

    span:nth-of-type(3) {
      margin-left: 5px;
      line-height: 17px;
      font-size: 12px;
      color: #333;
    }

    .btn-help {
      line-height: 20px;
      font-size: 15px;
      font-weight: 500;
      text-decoration: underline;
      color: #C7AC8B;
    }
  }

  .submit {
    width: 327px;
    height: 45px;
    margin-top: 18px;
    border-radius: 4px;
    line-height: 20px;
    font-size: 14px;
    font-weight: 500;

    &.disabled {
      color: #999;
      background: #F2F2F2;
    }

    &.active {
      color: #fff;
      background: linear-gradient(90deg, #E5C7A5 0%, #CFAA7F 100%);
      box-shadow: 0 14px 9px -10px rgba(219, 208, 194, 1);
    }
  }
</style>
