<template>
  <!-- 代理商页面 -->
  <view v-if="!isFirstload" class="container" :style="appThemeStyle">
    <!-- 头部卡片 -->
    <view class="cont_header">
      <!-- 我的服务 判断营业执照 -->
      <view v-if="dlsInfoData.agentstaxnumber && dlsInfoData.agentsshzt === '审核通过'" class="my-service">
        <view class="service-title" @click="getCreateOpenid">
          <text>企业识别码</text>
          <text style="font-weight: 900">{{ dlsInfoData.agentsqym }}</text>
        </view>
        <view class="uqrCodeText" style="border: none" @click="getCreateOpenid"> 扫一扫下面的二维码图案 </view>
        <view class="service-content uqrCodeBox">
          <uqrcode
            ref="uqrcode"
            class="uqrcode"
            canvas-id="qrcode"
            :size="320"
            :value="distribution"
            :options="{margin: 10, areaColor: '', foregroundColor: '', foregroundColor: '#156bb4'}"
          ></uqrcode>
        </view>
      </view>
      <!-- 未认证 -->
      <view v-if="!dlsInfoData.agentstaxnumber" class="my-service my-service-flase" @click="handleLogin">
        <view class="service-title">
          <text>企业识别码</text>
          <text style="font-weight: 900">不可用</text>
        </view>
        <view class="uqrCodeText" style="border: none"> 企业还没有认证，无法获取企业识别码 </view>
        <view class="service-content uqrCodeBox">
          <view class="getCodeCenter r_flex_wrap">
            <i-pay-code-one theme="outline" size="136" fill="#156bb4" :strokeWidth="2" strokeLinejoin="bevel" strokeLinecap="square" />
            点击立刻认证，获得企业识别码
          </view>
        </view>
      </view>

      <!-- 未审核 -->
      <view v-if="dlsInfoData.agentstaxnumber && dlsInfoData.agentsshzt === '待审核'" class="my-service my-service-flase my-service-sh">
        <view class="service-title">
          <text>企业识别码</text>
          <text style="font-weight: 900">未审核</text>
        </view>
        <view class="uqrCodeText" style="border: none"> 企业已经提交资料 </view>
        <view class="service-content uqrCodeBox">
          <view class="getCodeCenter r_flex_wrap">
            <i-pay-code-one theme="outline" size="136" fill="#156bb4" :strokeWidth="2" strokeLinejoin="bevel" strokeLinecap="square" />
            等待管理员审核，审核通过发放企业识别码
          </view>
        </view>
      </view>

      <!-- 未审核 -->
      <view v-if="dlsInfoData.agentsshzt === '审核不通过'" class="my-service my-service-flase my-service-wtg">
        <view class="service-title"> </view>
        <view class="uqrCodeText" style="border: none"> 审核未通过 </view>
        <view class="service-content uqrCodeBox">
          <view class="getCodeCenter r_flex_wrap">
            <i-caution theme="outline" size="136" fill="#f44336" :strokeWidth="2" strokeLinejoin="bevel" strokeLinecap="square" />
            审核未通过
          </view>
        </view>
      </view>
    </view>

    <view class="cont_content">
      <!-- 公司logo -->
      <view class="cont_logo">
        <img class="gs-logo-bjy" src="/static/background/bjy.png" />
        <img class="gs-logo-z" src="/static/background/gs-logo-z.png" />
      </view>
      <!-- 商户介绍 -->
      <view class="cont_sh">
        <!-- 名称 -->
        <view class="user-content">
          <view class="nick-name">
            {{ dlsInfoData.agentsname ? dlsInfoData.agentsname : '未认证企业名称' }}
          </view>
          <view v-if="dlsInfoData.agentstaxnumber && dlsInfoData.agentsshzt === '审核通过'" class="my-mobile-true">
            <i-shield-add theme="outline" style="margin-right: 10rpx" size="16" fill="#60d08a" :strokeWidth="3" strokeLinejoin="bevel" strokeLinecap="square" />
            <view class="info">企业已认证</view>
          </view>
          <view class="login-tips">UID: {{ dlsInfoData.agentscustomerid }}</view>
          <view v-if="dlsInfoData.agentstaxnumber && dlsInfoData.agentsshzt === '审核通过'" class="login-tips">企业余额：{{ dlsInfoData.agentsamount }}</view>
        </view>

        <view v-if="dlsInfoData.agentstaxnumber && dlsInfoData.agentsshzt === '审核通过'" class="r_flex_center fenxiao_order" @click="onTargetOrder()">
          <view class="item-name">查看我的分销订单</view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import store from '@/store'
import {inArray} from '@/utils/util'
import AvatarImage from '@/components/avatar-image'
import CustomerBtn from '@/components/customer-btn'
import {setCartTabBadge} from '@/core/app'
import SettingKeyEnum from '@/common/enum/setting/Key'
import SettingModel from '@/common/model/Setting' // 网址设置接口
import * as UserApi from '@/api/user'
import * as OrderApi from '@/api/order'
import {checkLogin, filterModules} from '@/core/app'
import storage from '@/utils/storage'
import * as AppApi from '@/api/app'
import StoreModel from '@/common/model/Store'

// 订单操作
const orderNavbar = [
  {
    id: 'all',
    name: '分销订单',
    icon: 'qpdingdan'
  }
  // {
  //   id: 'payment',
  //   name: '待支付',
  //   icon: 'daifukuan',
  //   count: 0
  // },
  // {
  //   id: 'delivery',
  //   name: '待发货',
  //   icon: 'daifahuo',
  //   count: 0
  // },
  // {
  //   id: 'comment',
  //   name: '已经完成',
  //   icon: 'daishouhuo',
  //   count: 0
  // }
]

/**
 * 我的服务
 * id: 标识; name: 标题名称; icon: 图标; type 类型(link和button); url: 跳转的链接
 */
const service = [
  {
    id: 'address',
    name: '人脸核实',
    icon: 'shouhuodizhi',
    type: 'link',
    url: 'pages/address/index'
  },
  {
    id: 'coupon',
    name: '销户',
    icon: 'lingquan',
    type: 'link',
    url: 'pages/coupon/index',
    moduleKey: 'market-coupon'
  },
  {
    id: 'myCoupon',
    name: '变更套餐',
    icon: 'youhuiquan',
    type: 'link',
    url: 'pages/my-coupon/index',
    moduleKey: 'market-coupon'
  },
  {
    id: 'refund',
    name: '退换/售后',
    icon: 'shouhou',
    type: 'link',
    url: 'pages/refund/index',
    count: 0
  },
  {
    id: 'contact',
    name: '在线客服',
    icon: 'kefu',
    type: 'contact'
  },
  {
    id: 'points',
    name: '成为代理商',
    icon: 'jifen',
    type: 'link',
    url: 'pages/login/index',
    moduleKey: 'market-points'
  },
  {
    id: 'orderCenter',
    name: '订单中心',
    icon: 'order-c',
    type: 'link',
    url: 'pages/order/center'
  },
  {
    id: 'help',
    name: '我的帮助',
    icon: 'bangzhu',
    type: 'link',
    url: 'pages/help/index',
    moduleKey: 'content-help'
  }
]

export default {
  components: {
    AvatarImage,
    CustomerBtn
  },
  // 计算属性
  computed: {
    // 计算属性的
    userInfoComputd: () => {
      return storage.get('wx_userinfo')
    }
  },
  data() {
    return {
      inArray,
      // 枚举类
      SettingKeyEnum,
      // 正在加载
      isLoading: true,
      // 首次加载
      isFirstload: true,
      // 是否已登录
      isLogin: false,
      // 系统设置
      setting: {},
      // 当前用户信息
      userInfo: {},
      // 账户资产
      assets: {
        balance: '--',
        points: '--',
        coupon: '--'
      },
      // 我的服务
      service,
      // 订单操作
      orderNavbar,
      // 当前用户待处理的订单数量
      todoCounts: {
        payment: 0,
        deliver: 0,
        received: 0
      },
      wx_qyid: storage.get('wx_qyid'),
      userInfoData: {},
      distribution: ''
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onLoad(options) {
    uni.stopPullDownRefresh()
    this.onRefreshPage()
    this.userInfoData = storage.get('wx_userinfo')
    if (!storage.get('wx_dlsinfo')) {
      this.getPageData()
    } else {
      this.dlsInfoData = storage.get('wx_dlsinfo') || {agentsname: '加载中', agentscustomerid: '加载中'}
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  created() {
    // 获取图形验证码
    // this.getCaptcha()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow(options) {
    // 获取图形验证码
    // this.getCaptcha()
  },

  methods: {
    // 刷新页面
    onRefreshPage() {
      // 更新购物车角标
      // setCartTabBadge()
      // 判断是否已登录
      // this.isLogin = checkLogin()
      // 获取页面数据
      this.getPageData()
    },

    // 二维码链接
    async getCreateOpenid() {
      this.distribution = ''
      const resAgents = await AppApi.createOpenid({openid: storage.get('wx_openid')})
      this.distribution = resAgents.Data
      console.log('二维码连接', this.distribution)
    },

    // 复制文本
    fuzhi(value) {
      const oInput = document.createElement('input')
      oInput.value = value
      document.body.appendChild(oInput)
      oInput.select()
      document.execCommand('Copy')
      uni.showToast({
        title: '复制成功',
        icon: 'success',
        duration: 2000
      })
    },

    // 获取页面数据
    async getPageData(callback) {
      console.log('下拉刷新user')
      const app = this
      app.isFirstload = true
      // 更新user信息 更新客户信息
      await StoreModel.getCustomerData()
      // 不是代理商跳转代理注册页面
      if (storage.get('wx_userinfo').customertype !== '代理商') {
        // 如果已经是代理商则直接登录
        this.$navTo('pages/firm/index')
      }
      // 更新二维码
      app.distribution = storage.get('wx_fxm')
      // await this.getCreateOpenid()
      this.dlsInfoData = storage.get('wx_dlsinfo')
      app.isFirstload = false
      uni.stopPullDownRefresh()
    },

    // 初始化我的服务数据
    async initService() {},

    // 初始化订单操作数据
    initOrderTabbar() {},

    // 获取当前用户信息
    getUserInfo() {
      const app = this
      return new Promise((resolve, reject) => {
        resolve(true)
      })
    },

    // 获取账户资产
    getUserAssets() {
      const app = this
      return new Promise((resolve, reject) => {
        resolve(true)
      })
    },

    // 获取当前用户待处理的订单数量
    getTodoCounts() {
      const app = this
      return new Promise((resolve, reject) => {
        resolve(true)
      })
    },

    // 跳转到认证
    handleLogin() {
      this.$navTo('pages/firm/authentication/index')
    },

    // 跳转到绑定手机号页面
    handleBindMobile() {
      // this.$navTo('pages/user/bind/index')
      this.$navTo('pages/user/certificate/index')
    },

    // 跳转到修改个人信息页
    handlePersonal() {
      this.$navTo('pages/user/personal/index')
    },

    // 退出登录
    handleLogout() {
      const app = this
      uni.showModal({
        title: '友情提示',
        content: '您确定要退出登录吗?',
        success(res) {
          if (res.confirm) {
            store.dispatch('Logout', {}).then((result) => app.onRefreshPage())
          }
        }
      })
    },

    // 跳转到钱包页面
    onTargetWallet() {
      this.$toast('开发中,敬请期待')
      return
      this.$navTo('pages/wallet/index')
    },

    // 跳转到订单页
    onTargetOrder(item) {
      this.$navTo('pages/order/index', {
        dataType: 'all'
      })
    },

    // 跳转到我的积分页面
    onTargetPoints() {
      this.$toast('开发中,敬请期待')
      return
      this.$navTo('pages/points/log')
    },

    // 跳转到我的优惠券页
    onTargetMyCoupon() {
      this.$toast('开发中,敬请期待')
      return
      this.$navTo('pages/my-coupon/index')
    },

    // 跳转到服务页面
    handleService({url}) {
      console.log(url, 'url')
      this.$navTo(url)
    }
  },

  /**
   * 下拉刷新
   */
  onPullDownRefresh() {
    // 获取首页数据
    this.getPageData(() => {
      uni.stopPullDownRefresh()
    })
  }
}
</script>

<style lang="scss" scoped>
.container {
  padding-bottom: 60rpx;
  height: 100vh;
  background: #fff;
  padding: 20rpx;
  .cont_header {
    border-radius: 20rpx;
    width: 100%;
    height: 800rpx;
  }
  .cont_content {
    position: relative;
    min-height: 400rpx;
    padding-top: 100rpx;
    .cont_logo {
      width: 200rpx;
      height: 200rpx;
      border-radius: 100%;
      position: absolute;
      left: 20rpx;
      top: -86rpx;
      // background: #a1c4fd;
      .gs-logo-bjy {
        position: absolute;
        left: 0rpx;
        top: 0rpx;
        display: block;
        width: 311rpx;
        height: 180rpx;
      }
      .gs-logo-z {
        display: block;
        position: absolute;
        left: 82rpx;
        top: 18rpx;
        display: block;
        width: 110rpx;
        height: 110rpx;
        background: #b5dafc;
        border-radius: 100%;
        padding: 20rpx;
      }
    }
    .cont_sh {
      padding: 0 60rpx;
      .user-content {
        .nick-name {
          color: #333;
          font-size: 40rpx;
          font-weight: 900;
        }
        .login-tips {
          margin-top: 10rpx;
        }
      }
    }
  }
}

// 老的css
.user-img {
  border: 2px solid #0081ff;
  padding: 10rpx;
  border-radius: 100%;
  background: #a7e0fb;
}

// 页面头部
.main-header {
  // background-color: #FBF7EF;
  position: relative;
  width: 100%;
  height: 280rpx;
  background-size: 100% 100%;
  overflow: hidden;
  display: flex;
  align-items: center;
  padding-left: 30rpx;

  .bg-image {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 0;
  }

  .bg-dls {
    height: 1200rpx;
  }
}

// 角标组件
.item-badge {
  position: absolute;
  top: 0;
  right: 55rpx;
  // background: $main-bg;
  background: #fa2209;
  color: #fff;
  border-radius: 100%;
  min-width: 38rpx;
  height: 38rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 1rpx;
  font-size: 24rpx;
}

// 我的钱包
.my-asset {
  display: flex;
  background: #fff;
  padding: 40rpx 0;

  .asset-right {
    width: 200rpx;
    border-left: 1rpx solid #eee;
  }

  .asset-right-item {
    text-align: center;
    color: #545454;

    .item-icon {
      font-size: 44rpx;
    }

    .item-name {
      margin-top: 14rpx;
      font-size: 28rpx;
    }
  }

  .asset-left-item {
    max-width: 183rpx;
    text-align: center;
    color: #666;
    padding: 0 16rpx;

    .item-value {
      font-size: 34rpx;
      color: $main-bg;
    }

    .item-name {
      margin-top: 14rpx;
      font-size: 28rpx;
    }
  }
}

// 订单操作
.order-navbar {
  display: flex;
  margin: 20rpx auto 20rpx auto;
  padding: 20rpx 0 26rpx 0;
  width: 94%;
  box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
  font-size: 30rpx;
  border-radius: 5rpx;
  background: #fff;

  &-item {
    position: relative;
    width: 25%;

    .item-icon {
      text-align: center;
      margin: 0 auto;
      padding: 10rpx 0;
      color: #545454;
      font-size: 44rpx;
    }

    .item-name {
      font-size: 28rpx;
      color: #545454;
      text-align: center;
      margin-right: 10rpx;
    }
  }
}

.fenxiao_order {
  position: fixed;
  bottom: 30rpx;
  left: 0rpx;
  width: 100%;
  text-align: center;
  color: #0081ff;
  .item-name {
    border-bottom: 1px solid #0081ff;
  }
}

.uqrCodeText {
  width: 100%;
  display: flex;
  justify-content: center;
  border: 1px solid #0081ff;
  border-radius: 15rpx;
  padding: 10rpx 20rpx;
  font-size: 26rpx;
  text-align: center;
  padding-top: 40rpx;
}

// 我的服务
.my-service {
  position: relative;
  margin: auto;
  width: 100%;
  height: 100%;
  border-radius: 35rpx;
  background: transparent;
  color: #156bb4;
  background-image: linear-gradient(120deg, #a1c4fd 0%, #c2e9fb 100%);
  .uqrCodeBox {
    padding: 0rpx 0rpx 20rpx 0rpx;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    .uqrcode {
      overflow: hidden;
    }
  }

  .service-title {
    position: absolute;
    width: 400rpx;
    bottom: 30rpx;
    right: 30rpx;
    padding: 0 10rpx;
    display: flex;
    justify-content: space-between;
    font-size: 30rpx;
  }

  .service-content {
    margin-bottom: -20rpx;

    .service-item {
      position: relative;
      width: 25%;
      float: left;
      margin-bottom: 30rpx;

      .item-icon {
        text-align: center;
        margin: 0 auto;
        padding: 14rpx 0;
        color: $main-bg;
        font-size: 44rpx;
      }

      .item-name {
        font-size: 28rpx;
        color: #545454;
        text-align: center;
      }
    }
  }
}

.my-service-flase {
  background-image: linear-gradient(135deg, #f5f7fa 0%, #d6d6d6 100%);
  color: #333;
  .getCodeCenter {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    margin-top: 130rpx;
  }
}

.my-service-sh {
  background-image: linear-gradient(120deg, #a1c4fd 0%, #c2e9fb 100%);
}

.my-service-wtg {
  color: red;
  background-image: linear-gradient(120deg, #ffd7de 0%, #f78c8c9a 100%);
}

// 退出登录
.my-logout {
  display: flex;
  justify-content: center;
  margin-top: 50rpx;

  .logout-btn {
    width: 60%;
    margin: 0 auto;
    font-size: 28rpx;
    color: #616161;
    border-radius: 20rpx;
    border: 1px solid #dcdcdc;
    padding: 16rpx 0;
    text-align: center;
  }
}

// 绑定手机号 样式1
.my-mobile {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16rpx 40rpx;
  background: #fcebd1;

  .info {
    color: #cd8c0c;
    font-size: 28rpx;
  }

  .btn-bind {
    padding: 8rpx 24rpx;
    background-color: #eab766;
    color: #fff;
    border-radius: 30rpx;
    font-size: 26rpx;
    text-align: center;
  }
}

.my-mobile-true {
  display: flex;
  align-items: center;
  border: 1px solid #60d08a;
  padding: 12rpx 10rpx;
  font-size: 18rpx;
  height: 40rpx;
  line-height: 40rpx;
  color: #60d08a;
  border-radius: 25rpx;
  background: #fff;
  margin-top: 10rpx;
  width: 170rpx;
}

// 绑定手机号 样式2
.my-mobile2 {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 20rpx auto 20rpx auto;
  padding: 12rpx 40rpx;
  width: 94%;
  box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
  font-size: 30rpx;
  border-radius: 5rpx;
  color: red;
  background: #ffe7e7;
  border: 1px solid red;

  .info {
    // color: #cd8c0c;
    font-size: 26rpx;
  }

  .btn-bind {
    padding: 8rpx 24rpx;
    background-color: #fa3534;
    color: #fff;
    border-radius: 30rpx;
    font-size: 26rpx;
    text-align: center;
  }
}
</style>
