const { getShopDetail } = require('../../api/common')
const { getProductDetail, postProductCollect } = require('./api/index')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchForm: {
      product_id: '',
      type: -1
    },
    nav: [
      {
        name: '描述',
        value: '1'
      },
      {
        name: '供应商',
        value: '2'
      }
    ],
    active: '1',
    objProductDetail: {
      product_rotation_img_list: [],
      product_detail_img_list: []
    },
    shopDetail: { shop_address: {} },
    booLock: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const { product_id } = options
    this.setData({
      'searchForm.product_id': product_id
    }, () => {
      this.fetchProductDetail()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  bindCallBack() {
    this.fetchProductDetail()
  },
  async fetchProductDetail() {
    try {
      const { status, data, msg } = await getProductDetail(this.data.searchForm.product_id)
      if (status) {
        const { shop_id, product_rotation_img_list, product_detail_img_list, collect_status } = data

        this.setData({
          objProductDetail: {
            ...data,
            product_rotation_img_list: product_rotation_img_list ?  JSON.parse(product_rotation_img_list) : [],
            product_detail_img_list: product_detail_img_list ? JSON.parse(product_detail_img_list) : []
          },
          'searchForm.shop_id': shop_id,
          'searchForm.type': collect_status === 0 ? 1 : 2 // 是否收藏(0否1是)
        }, () => {
          this.fetchShopDetail()
        })
      } else {
        wx.showToast({
          title: msg,
          icon: 'none'
        })
      }
    } catch (err) {}
  },
  async fetchShopDetail() {
    try {
      const { status, data, msg } = await getShopDetail(this.data.searchForm.shop_id)

      if (status) {
        this.setData({
          shopDetail: {
            ...data,
            shop_address: data.shop_address ? JSON.parse(data.shop_address) : {}
          }
        })
      } else {
        wx.showToast({
          title: msg,
          icon: 'none'
        })
      }
    } catch (err) {}
  },
  async productCollect() {
    const { type, product_id } = this.data.searchForm

    this.setData({
      booLock: true
    })
    try {
      const { status, msg } = await postProductCollect(product_id, type)
      if (status) {
        this.setData({
          'searchForm.type': type === 1 ? 2 : 1
        })
      } else {
        wx.showToast({
          title: msg,
          icon: 'none'
        })
      }
    } catch (err) {}

    this.setData({
      booLock: false
    })
  },
  handleNav(e) {
    const { value } = e.detail

    this.setData({
      active: value
    })
  },
  handlePreviewImage(e) {
    const { imgs, index } = e.currentTarget.dataset

    if (Array.isArray(imgs)) {
      wx.previewImage({
        urls: imgs,
        current: imgs[index]
      })
    }
  }
})