import { getShopList } from '../../api/products'
export default {
  name: 'ProductList',
  data() {
    return {
        products: [
            // {
            //   id: 1,
            //   name: "Nexa",
            //   price: `10000~20000`,
            //   image: "https://img.yzcdn.cn/vant/apple-1.jpg",
            //   desc: "NexaPow",
            // },
            // {
            //   id: 2,
            //   name: "grs",
            //   price: `10000~20000`,
            //   image: "https://img.yzcdn.cn/vant/apple-1.jpg",
            //   desc: "groestl",
            // },
            // {
            //   id: 3,
            //   name: "mona",
            //   price: `10000~20000`,
            //   image: "https://img.yzcdn.cn/vant/apple-1.jpg",
            //   desc: "Lyra2REv2",
            // },
            // {
            //   id: 4,
            //   name: "dgb",
            //   price: `10000~20000`,
            //   image: "https://img.yzcdn.cn/vant/apple-1.jpg",
            //   desc: "DigiByte(Skein)",
            // },
          ],
      loading: false,
      powerList: [
        // {
        //   value: 1,
        //   label: "NexaPow",
        //   children: [
        //     {
        //       value: 1 - 1,
        //       label: "挖矿账户1",
        //     },
        //     {
        //       value: 1 - 2,
        //       label: "挖矿账户2",
        //     },
        //   ],
        // },
        // {
        //   value: 2,
        //   label: "Grepow",
        //   children: [
        //     {
        //       value: 2 - 1,
        //       label: "挖矿账户1",
        //     },
        //     {
        //       value: 2 - 2,
        //       label: "挖矿账户2",
        //     },
        //   ],
        // },
        // {
        //   value: 3,
        //   label: "mofang",
        //   children: [
        //     {
        //       value: 3 - 1,
        //       label: "挖矿账户1",
        //     },
        //   ],
        // },
      ], 
      currencyList: [
        {
          path: "nexaAccess",
          value: "nexa",
          label: "nexa",

          imgUrl: `https://m2pool.com/img/nexa.png`,
          name: "course.NEXAcourse",
          show: true,
          amount: 10000,
        },
        {
          path: "grsAccess",
          value: "grs",
          label: "grs",

          imgUrl: `https://m2pool.com/img/grs.svg`,
          name: "course.GRScourse",
          show: true,
          amount: 1,
        },
        {
          path: "monaAccess",
          value: "mona",
          label: "mona",

          imgUrl: `https://m2pool.com/img/mona.svg`,
          name: "course.MONAcourse",
          show: true,
          amount: 1,
        },
        {
          path: "dgbsAccess",
          value: "dgbs",
          // label: "dgb-skein-pool1",
          label: "dgb(skein)",

          imgUrl: `https://m2pool.com/img/dgb.svg`,
          name: "course.dgbsCourse",
          show: true,
          amount: 1,
        },
        {
          path: "dgbqAccess",
          value: "dgbq",
          // label: "dgb(qubit-pool1)",
          label: "dgb(qubit)",

          imgUrl: `https://m2pool.com/img/dgb.svg`,
          name: "course.dgbqCourse",
          show: true,
          amount: 1,
        },
        {
          path: "dgboAccess",
          value: "dgbo",
          // label: "dgb-odocrypt-pool1",
          label: "dgb(odocrypt)",

          imgUrl: `https://m2pool.com/img/dgb.svg`,
          name: "course.dgboCourse",
          show: true,
          amount: 1,
        },
        {
          path: "rxdAccess",
          value: "rxd",
          label: "radiant(rxd)",

          imgUrl: `https://m2pool.com/img/rxd.png`,
          name: "course.RXDcourse",
          show: true,
          amount: 100,
        },
        {
          path: "enxAccess",
          value: "enx",
          label: "Entropyx(enx)",

          imgUrl: `https://m2pool.com/img/enx.svg`,
          name: "course.ENXcourse",
          show: true,
          amount: 5000,
        },
        {
          path: "alphminingPool",
          value: "alph",
          label: "alephium",

          imgUrl: `https://m2pool.com/img/alph.svg`,
          name: "course.alphCourse",
          show: true,
          amount: 1,
        },
      ],
      screenCurrency: "",
      searchAlgorithm: "",
      params:{
        pageNum: "1",
        pageSize: "10",
        keyword:""
      },
      productListLoading:false,
    }
  },
  mounted() {
    this.fetchShopList()
  },
  methods: {
    // 获取商场页面的店铺列表
    async fetchShopList(params) {
      this.productListLoading = true
      try{
        // 仅允许 pageNum、pageSize、keyword 三个参数
        const payload = {
          pageNum: this.params.pageNum,
          pageSize: this.params.pageSize
        }
        const kw = params && typeof params === 'object' ? params.keyword : this.params.keyword
        if (kw) payload.keyword = kw
        const res = await getShopList(payload)
        if (res && (res.code === 0 || res.code === 200)) {
          // 这里直接将店铺列表赋值到 products，用于页面渲染
          this.products = Array.isArray(res.rows) ? res.rows : []
        } else {
          this.products = []
        }
      }catch(e){
        console.error('获取店铺列表失败:', e)
        this.products = []
      }
      this.productListLoading = false
    },
 
    // 算法搜索（使用同一接口，传入 algorithm 参数）
    handleAlgorithmSearch() {
      const keyword = (this.searchAlgorithm || '').trim()
      this.params.keyword = keyword
      this.fetchShopList(keyword ? { keyword } : undefined)
     
    },
    // 清空搜索关键字
    handleAlgorithmClear() {
      this.searchAlgorithm = ""
      this.params.keyword = ""
      this.fetchShopList()
    },
    handleProductClick(product) {

      const id = (product && (product.shopId != null ? product.shopId : product.id)) 
      if (id !== undefined && id !== null) {
        this.$router.push(`/product/${id}`);  
      }  

     
    },



  }
}