<template>
  <view v-if="storeMaps.length != 0">
    <template v-for="item in storeMaps" :key="item.vendorStoreID">
      <view class="cell" @tap="platformManager(item)">
        <view class="left">
          <view class="vendor">
            <text :class="`icon icon-${item.vendorID}`"></text>
            <view class="vendor-name">
              <view>{{
                vendorName[item.vendorID]
                  ? vendorName[item.vendorID]
                  : '未知平台'
              }}</view>
              <view>{{ item.isSync ? '(自动同步)' : '(未同步)' }}</view>
            </view>
          </view>

          <view class="original-name name-active">
            {{ item.vendorStoreName }}
          </view>
          <text class="packname">{{ item.pricePercentagePack }}</text>
          
          <!-- 营业状态 -->
          <view style="width:100px" v-if="(item.vendorID === 1 || item.vendorID === 3) && store.getters['storeInfo/storeStatus'] !== item.status" >
            <view  class="do-business-root business-active">
              <text class="text">{{item.status === 1?'营业中':item.status === 0?'临时休息':item.status === -1?'休息':item.status === -2?'禁用':'未知'}}</text>
            </view>
          </view>
        
        </view>

        <view class="right">
          <button
            v-if="!earningType"
            class="btn2"
            size="mini"
            @tap.stop="openPricePack(item)"
          >
            加价比例
          </button>
        </view>
      </view>
    </template>
  </view>
  <jx-empty title="您还没有开通任何店铺哟" v-else />

  <!-- 公共组件 -->
  <jx-loading />
</template>

<script lang="ts" setup>
import { onLoad } from '@dcloudio/uni-app'
import merchant from '@/api/https/merchant'
import { getStorage } from '@/utils/storage'
import { computed, ref } from 'vue'
import { store } from '@/store'
import jxEmpty from '@/components/jx-empty/jx-empty.vue'
import configCms from '@/utils/configCms'

onLoad(async () => {
  await getStoreInfo()
})

/**
 * 获取店铺数据
 */
const storeMaps = ref<Array<AnyObject>>([])
async function getStoreInfo() {
  let data = {
    storeID: getStorage('storeID'),
  }
  let storeInfoRes = await merchant.get_stores(data)
  storeMaps.value = storeInfoRes.data.stores[0].StoreMaps
  // 修改三方平台 营业状态
  store.getters['storeInfo/platformInfo'].forEach((element:AnyObject) => {
    let findIndex = storeMaps.value.findIndex((item:AnyObject) => item.vendorID === element.vendorID)
    if(findIndex!==-1) storeMaps.value[findIndex].status = element.status
  });
}

/**
 * 加价比例
 */
function openPricePack(obj: AnyObject) {
  uni.navigateTo({
    url: `/subPages/merchantChild/modifyPrice/modifyPrice?vendorID=${obj.vendorID}&key=${obj.pricePercentagePack}`,
  })
}

/**
 * 平台名字
 */
const vendorName = computed(() => {
  let storeInfo:AnyObject = configCms.serveInfo
  return storeInfo.vendorName
})

/**
 * 门店折扣 结算方式 (1报价，2扣点)
 */
const earningType = computed(() => {
  return store.getters['storeInfo/geterPointStore']
})

/**
 * 跳转到三方平台管理
 */
function platformManager(item:AnyObject) {
  if((item.vendorID === 1 || item.vendorID === 3) && store.getters['storeInfo/storeStatus'] !== item.status){
    uni.navigateTo({
      url: `/subPages/merchantChild/platformDetail/index?vendorInfo=${JSON.stringify(item)}`,
    })
  }
}
</script>

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