<template>
  <div class="ShopPlatAuthorization">
    <div class="authBox">
      <Icon
        v-if="status == &quot;loading&quot;"
        type="ios-loading"
        style="animation: ani-demo-spin 1s linear infinite"
      />
      <Icon
        v-if="status == &quot;true&quot;"
        type="md-checkmark-circle"
        style="color: green;"
      />
      <Icon
        v-if="status == &quot;false&quot;"
        type="md-close-circle"
        style="color: red"
      />
      {{ massage }}
    </div>
    <div class="bottomBtnBox">
      <Button
        type="primary"
        size="small"
        class="sureBtn"
        ghost
        @click="cancel"
      >
        关闭
      </Button>
    </div>
  </div>
</template>

<script>
  // import R3 from '@syman/burgeon-r3';
  const R3 = window.R3;

  const { network } = R3;
  export default {
    components: {},
    props: {
      selectRowData: {// 获取ID用于多选,获取勾选的明细对应的row内的数据
        type: [Array, Object],
        default: () => {}
      },
      externalOptions: {
        type: Object,
        default: ()=>{}
      },
    },
    data() {
      return {
        massage: '店铺授权中...',
        status: 'loading',
      };
    },
    created() {
      if (this.externalOptions) {
        if (this.externalOptions.ID) {
          this.getStoreMsgTable();
        }
      } else if (this.$route.params.itemId) {
        this.getStoreMsg();
      }
    },
    mounted() {
      document.getElementsByClassName('ark-modal-close')[0].addEventListener('click', () => {
        if (this.timer) {
          clearInterval(this.timer);
        }
      });
    },
    methods: {
      // 从标准列表获取所选项信息
      getStoreMsgTable() {
        network.post('/p/cs/getObject', this.$urlSearchParams({ table: 'T_IESSHOP', objid: this.externalOptions.ID })).then(res => {
          if (res.data.code == 0) {
            let params = {
              nickName: res.data.data.addcolums[0].childs[3].valuedata,
              plantNo: res.data.data.addcolums[0].childs[0].refobjid,
              shopId: this.externalOptions.ID
            };
            this.getAuthorizationAddressFromTable(params);
          }
        });
      },
      // 从平台店铺详情获取信息
      getStoreMsg() {
        network.post('/p/cs/getObject', this.$urlSearchParams({ table: 'T_IESSHOP', objid: this.$route.params.itemId })).then(res => {
          if (res.data.code == 0) {
            let params = {
              nickName: res.data.data.addcolums[0].childs[3].valuedata,
              plantNo: res.data.data.addcolums[0].childs[0].refobjid,
              shopId: this.$route.params.itemId
            };
            this.getAuthorizationAddressDetail(params);
          }
        });
      },
      // 从标准列表获取跳转地址
      getAuthorizationAddressFromTable(params) {
        network.post('/p/cs/getAuthorizationAddress', params).then(res => {
          if (res.data.code == 0) {
            window.open(res.data.data.authorizationUrl);
            this.getAuthStatus(params);
          }
        });
      },
      // 从平台店铺详情获取跳转地址
      getAuthorizationAddressDetail(params) {
        network.post('/p/cs/getAuthorizationAddress', params).then(res => {
          if (res.data.code == 0) {
            window.open(res.data.data.authorizationUrl);
            this.getAuthStatusDetail(params);
          }
        });
      },
      cancel() {
        clearInterval(this.timer);
        this.$parent.close();
      },
      // 从标准列表获取回调结果
      getAuthStatus(params) {
        return new Promise((resolve, reject) => {
          network.post('/p/cs/shop/pollIsAuthorize', params).then(res => {
            if (res.data.code == 0) {
              if (res.data.data == null) {
                this.timer = setTimeout(() => {
                  resolve(this.getAuthStatus(params));
                }, 2000);
              } else if (res.data.data.ERRORCODE == null) {
                this.status = 'true';
                this.massage = '您已成功完成授权';
                resolve(res);
              } else if (res.data.data.ERRORCODE == -1) {
                this.status = 'false';
                this.massage = res.data.data.ERRORMESSAGE;
                // this.massage = res.data.message;
                resolve(res);
              }
            } else if (res.data.code == -1) {
              // this.status = 'false';
              // this.massage = res.data.message;
              // resolve(res);
            }
          });
        });
      },
      // 从平台店铺详情获取回调结果
      getAuthStatusDetail(params) {
        return new Promise((resolve, reject) => {
          network.post('/p/cs/shop/pollIsAuthorize', params).then(res => {
            if (res.data.code == 0) {
              if (res.data.data == null) {
                this.timer = setTimeout(() => {
                  resolve(this.getAuthStatusDetail(params));
                }, 2000);
              } else if (res.data.data.ERRORCODE == null) {
                this.status = 'true';
                this.massage = '您已成功完成授权';
                resolve(res);
              } else if (res.data.data.ERRORCODE == -1) {
                this.status = 'false';
                this.massage = res.data.message;
                resolve(res);
              }
            } else if (res.data.code == -1) {
              // this.status = 'false';
              // this.massage = res.data.message;
              // resolve(res);
            }
          });
        });
      },
    },
  };
</script>
<style lang='less' scoped>
  .ShopPlatAuthorization {
    width: 350px;
  }
  .bottomBtnBox {
    width: calc(100% - 48px);
    margin: 10px auto 0px;
    text-align: right;
    .cancelBtn,.sureBtn {
      width: 56px;
      height: 24px;
      font-size: 12px;
    }
    .cancelBtn {
      color: #909399;
      border: 1px solid #DADADA;
      margin-right: 8px;
    }
  }
</style>
