<template>
  <view class="content">
    <kevyloading v-if="loading" type="bsm-loader" color="#618af8" transparent></kevyloading>
    <!-- <view class="w-full">
      <wd-navbar fixed safeAreaInsetTop :leftText="user_info?.info?.name || '商城首页'">
        <template #right>
          <wd-icon @click="utils.toUrl('/store/setup/index')" name="setting" size="22px"></wd-icon>
        </template>
      </wd-navbar>
    </view> -->

    <div class="grid p-2">
      <div v-for="shop of shopList" class="rounded-md overflow-hidden">
        <wd-card class="rounded-md overflow-hidden" type="rectangle">
          <template #title>
            <view class="title">
              <view>ID：{{ shop.id }}</view>
              <view class="title-tip">
                {{ shop.status ? "启用" : "停用" }}
              </view>
            </view>
          </template>
          <view style="height: 40px;" class="shopBox">
            <image mode="aspectFill" :src="shop.logo_url" :alt="shop.name"
              style="width: 60px;height: 60px;border-radius: 4px; margin-right: 12px;" />
            <view>
              <view style="color: rgba(0,0,0,0.85); font-size: 16px;">{{ shop.name }}</view>
              <view style="color: rgba(0,0,0,0.25); font-size: 12px;">{{ shop.detail }}</view>
            </view>
          </view>

          <template #footer>
            <view class="flex">
              <wd-button style="margin-right: auto;margin-left: 0;" @click="syncGoods(shop)" size="small">同步商品</wd-button>
              <view>
                <wd-button @click="changeS(shop)" v-if="shop.status == 1" size="small" plain
                  style="margin-right: 8px;">关闭</wd-button>
                <wd-button @click="changeS(shop)" v-if="shop.status !== 1" size="small" plain style="margin-right: 8px;">
                  开启</wd-button>
                <wd-button @click="utils.toUrl('/store/shop/edit?edit=' + JSON.stringify(shop))" size="small"
                  style="margin-right: 0px;">编辑</wd-button>
              </view>

            </view>
          </template>
        </wd-card>
      </div>

    </div>

    <view class="h-[64px]"></view>
    <view class="fixed left-2 right-2 bottom-4 z-50">
      <myTabbar tab="shop"></myTabbar>
    </view>

    <wd-toast />
    <wd-message-box></wd-message-box>
  </view>
</template>

<script setup>
import { ref } from 'vue';
import utils from '@/utils/utils.js';
import shop from '@/api/store/shop.js';
import myTabbar from "../components/myTabbar/index.vue";
import kevyloading from "@/components/kevy-loading/kevy-loading";
import { useMessage } from '@/uni_modules/wot-design-uni';
import { useToast } from '@/uni_modules/wot-design-uni'
const toast = useToast()
const message1 = useMessage();

import {
  onShow,
} from "@dcloudio/uni-app";

/** 
 * @type {Ref<boolean>} 
 * 控制页面加载状态的 Ref
 */
const loading = ref(false);

/**
 * 从本地存储中获取用户信息
 */
const user_info = uni.getStorageSync("user_info");

const shopList = ref({});

const changeS = (row) => {
  shop.editStatus({
    id: row.id,
    status: [1, 0][row.status],
  }).then(res => {

    if (res.code == 0) {
      toast.success('操作成功')
      row.status = [1, 0][row.status]
    } else {
      showNotify({ type: 'error', message: '出错了' })
    }

  })
}

const syncGoods = (row) => {
  message1
    .confirm({
      title: '同步商品',
    })
    .then((resp) => {
      toast.loading('请稍后...')

      shop.synchronousGoods({
        id: row.id,
      }).then(res => {
        if (res.code == 0) {
          toast.success('同步成功');
        } else {
          toast.error('同步失败');
        }
      });
    })
    .catch((error) => {
      console.log(error);
    });
};


/**
 * 获取数据的函数
 */
const getShopList = () => {
  loading.value = true
  shop.list().then(res => {
    shopList.value = res.data;
    shopList.value.ok = true;
    loading.value = false
  });
};

// 初始化调用获取一行数据
onShow(() => {
  getShopList();
})
</script>

<style lang="scss" scoped>
.shopBox,
.title {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
}

.shopBox {
  justify-content: flex-start;
}

.title {
  justify-content: space-between;
}

.title-tip {
  color: rgba(0, 0, 0, 0.25);
  font-size: 12px;
}
</style>
