<template>
  <view class="my-shop-wrapper page-index">
    <view class="shop-data">
      <view class="bg">
        <image src="/static/images/icon/bg-home.png" />
      </view>
      <!-- 自提点信息 -->
      <view class="data-top">
        <view class="shop-info">
          <view class="shop-icon">
            <image
              :src="imgUrl"
              @error="onErrorImg"
            />
          </view>
          <view class="shop-name">
            {{ stationName }}
          </view>
          <view
            class="switch-num"
            @tap="onSwitchNum"
          >
            <image src="/static/images/icon/handoff.png" />
            <view>切换账号</view>
          </view>
        </view>
      </view>
      <!-- 日/月业绩信息 -->
      <view class="achievement">
        <!-- <view class="a-title">
          <view class="tit">本店业绩</view>
        </view> -->
        <!-- <view class="data-table"> -->
        <view class="data-line">
          <view class="data-block">
            <view class="data-cont">
              {{ dayTurnover.payOrderNumber || 0 }}
            </view>
            <view class="data-tit">
              今日订单
            </view>
          </view>
          <view class="data-block dividing">
            <view class="data-cont">
              {{ dealNum(dayTurnover.payAmount).res }}<text v-if="dealNum(dayTurnover.payAmount).isW">
                w
              </text>
            </view>
            <view class="data-tit">
              今日交易额
            </view>
          </view>
          <view class="data-block">
            <view class="data-cont">
              {{ dealNum(dayTurnover.customerUnitPrice).res }}<text v-if="dealNum(dayTurnover.customerUnitPrice).isW">
                w
              </text>
            </view>
            <view class="data-tit">
              今日客单价
            </view>
          </view>
        </view>
        <view class="data-line">
          <view class="data-block">
            <view class="data-cont">
              {{ monthTurnover.payOrderNumber || 0 }}
            </view>
            <view class="data-tit">
              本月订单
            </view>
          </view>
          <view class="data-block dividing">
            <view class="data-cont">
              {{ dealNum(monthTurnover.payAmount).res }}<text v-if="dealNum(monthTurnover.payAmount).isW">
                w
              </text>
            </view>
            <view class="data-tit">
              本月交易额
            </view>
          </view>
          <view class="data-block">
            <view class="data-cont">
              {{ dealNum(monthTurnover.customerUnitPrice).res }}<text v-if="dealNum(monthTurnover.customerUnitPrice).isW">
                w
              </text>
            </view>
            <view class="data-tit">
              本月客单价
            </view>
          </view>
        </view>
        <!-- </view> -->
      </view>
    </view>
    <!-- 常见应用 -->
    <view class="commom-app">
      <view class="commom-tit">
        常见应用
      </view>
      <view class="app-list">
        <view
          class="app-item"
          @tap="onToShopAdministration"
        >
          <image src="/static/images/icon/shop-manage.png" />
          <view class="app-name">
            店铺管理
          </view>
        </view>
        <view
          class="app-item"
          @tap="onToOrderAdministration"
        >
          <image src="/static/images/icon/orders.png" />
          <view class="app-name">
            订单管理
          </view>
        </view>
        <view
          class="app-item"
          @tap="scanCode"
        >
          <image src="/static/images/icon/verification.png" />
          <view class="app-name">
            验证核销
          </view>
        </view>
        <view
          class="app-item"
          @tap="onToBusinessCode"
        >
          <image src="/static/images/icon/merchant-code.png" />
          <view class="app-name">
            商家码
          </view>
        </view>
        <view
          class="app-item"
          @tap="onToPinkCode"
        >
          <image src="/static/images/icon/verification-code.png" />
          <view class="app-name">
            核销码
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import Wechat from '../../utils/wechat.js'
import util from '@/utils/util.js'
const stationName = ref('') // 自提点名称
const imgUrl = ref('') // 自提点logo
const dayTurnover = ref([]) // 日营业额
const monthTurnover = ref([]) // 月营业额
/**
 * 生命周期函数--监听页面加载
 */
onShow(() => {
  onGetIndexData()
})
/* 自提点首页数据 */
const onGetIndexData = () => {
  uni.showLoading()
  http.request({
    url: '/mall4cloud_order/station/get_station_index_info',
    method: 'GET',
    data: {}
  }).then(({ data }) => {
    // 自提点信息赋值
    stationName.value = data.stationName
    imgUrl.value = data.imgUrl ? import.meta.env.VITE_APP_RESOURCES_URL + data.imgUrl : '/static/images/icon/normal-bg.png'
    for (let i = 0; i < data.stationSalesDTOList.length; i++) {
      const salesType = data.stationSalesDTOList[i].salesType
      if (salesType === 1) {
        dayTurnover.value = data.stationSalesDTOList[i]
      } else if (salesType === 2) {
        monthTurnover.value = data.stationSalesDTOList[i]
      }
    }
    if (!uni.getStorageSync('cloudStationId')) {
      uni.setStorageSync('cloudStationId', data.stationId)
    }
    uni.hideLoading()
  })
}
/* 切换账号 */
const onSwitchNum = () => {
  http.request({
    url: '/mall4cloud_auth/ma/login_out',
    method: 'post'
  }).then(() => {
    // 清除登录缓存
    uni.removeStorageSync('cloudStationLoginResult')
    uni.removeStorageSync('cloudStationToken')
    uni.removeStorageSync('cloudStationHadLogin')
    uni.removeStorageSync('cloudStationId')
    uni.redirectTo({
      url: '/pages/account-login/account-login'
    })
  })
}
/* 跳转店铺管理页 */
const onToShopAdministration = () => {
  uni.navigateTo({
    url: '/pages/shop-adminsistration/shop-adminsistration'
  })
}
/* 跳转订单管理页 */
const onToOrderAdministration = () => {
  uni.navigateTo({
    url: '/pages/order-list/order-list'
  })
}
/* 跳转商家码页 */
const onToBusinessCode = () => {
  uni.navigateTo({
    url: '/pages/business-code/business-code'
    // url: '/pages/scan-pick/scan-pick'
  })
}
/* 跳转提货码取件页 */
const onToPinkCode = () => {
  uni.navigateTo({
    url: '/pages/pick-code/pick-code'
  })
}
/* 唤醒扫码功能 */
let code = ''
const scanCode = util.debounce(function () {
  // #ifdef H5
  if (!Wechat.isWechat()) {
    uni.showToast({
      title: '请在微信浏览器打开!',
      icon: 'none'
    })
    return
  }

  Wechat.scanQRCode((codeStr) => {
    // 处理扫码结果
    if (codeStr.indexOf(',') > -1) {
      code = codeStr.split(',')[1]
    } else {
      code = codeStr
    }
    // 扫码后跳转核销页面
    uni.navigateTo({
      url: '/pages/scan-pick/scan-pick?pickCode=' + code + '&pageType=1'
    })
  }, () => {
    uni.showToast({
      title: '唤起扫码失败，请稍后重试',
      icon: 'none'
    })
  })
  // #endif
}, 1000)

// 单价交易额显示
const dealNum = (val) => {
  if (!val) return { res: 0, isW: false }
  let res = val / 100
  let isW = false
  if (res >= 100000) {
    isW = true
    res = res / 10000
  }
  res = String(res)
  if (res && res.indexOf('.') > -1 && res.indexOf('.') <= res.length - 3) {
    res = res.slice(0, res.indexOf('.') + 2)
  }
  return { res, isW }
}

const onErrorImg = () => {
  imgUrl.value = '/static/images/icon/normal-bg.png'
}
</script>

<style lang="scss" scoped>
@import "./index.scss";
</style>
