import {
  request
} from "../../request/index.js"
const a = getApp()
Page({
  data: {
    imagesUrl: a.globalData.baseUrl,
    array: ['美国', '中国', '巴西', '日本'],
    city_id: '',
    pin_id: '',
    S4SList: '',
    S4SName: '',
    S4_ID: '',
    msg: '',
    bind: true,
    id: '',
    s4_id: '',


  },
  onShow: function () {
    //  console.log(this.data.s4_id);
  },
  onLoad: function (options) {
    //  console.log(options);
    this.setData({
      pin_id: options.pin_id
    })
    this.get4ShasList()
  },
  bindPickerChange: function (e) {
    let that = this
    that.setData({
      index: e.detail.value
    })
    that.data.S4SList.forEach((value, index) => {
      if (index == e.detail.value) {
        that.setData({
          S4_ID: value.id
        })
      }
    });
    //  console.log(that.data.S4_ID, "that.data.S4_ID");
  },
  async noBinding4S() {
    let that = this
    if (that.data.s4_id != '') {
      try {
        const {
          data
        } = await request({
          url: 'api/index/bang4s',
          data: {
            id: that.data.s4_id
          }
        })
        //  console.log(data);
        if (data.code == 1) {
          that.setData({
            msg: data.msg
          })
          that.popSuccessTest()
          let pages = getCurrentPages(); //获取当前页面pages里的所有信息。
          let prevPage = pages[pages.length - 2];
          prevPage.setData({
            s4_id: that.data.s4_id,
          })
          wx.navigateBack({
            delta: 1, // 返回上一级页面。
            success: function () {
              //  console.log('成功!')
            }
          })
          // setTimeout(() => {
          //   wx.navigateBack()
          // }, 1500);
        } else {
          that.setData({
            msg: data.msg
          })
          that.popTest()
        }
      } catch (err) {
        that.setData({
          msg: err.msg
        })
        that.popTest()
      }
    } else {
      that.setData({
        msg: '未绑定4S店'
      })
      that.popTest()
    }
  },
  async get4SList() {
    let that = this
    try {
      const {
        data: {
          data
        }
      } = await request({
        url: 'api/index/s4List',
        method: 'GET',
        data: {
          city_id: that.data.city_id,
          pin_id: that.data.pin_id
        }
      })
      that.setData({
        S4SList: data
      })
    } catch (err) {
      this.setData({
        msg: err.msg
      })
      this.popTest()
    }

  },
  async get4ShasList() {
    let that = this
    try {
      const {
        data: {
          data
        }
      } = await request({
        url: 'api/shop4s/s4Info',
        method: 'GET',
      })
      //  console.log(data, "4ddsaadfasdfasfasddf");
      that.setData({
        S4SList: data,
        S4SName: data.name,
        bind: false,
      })
    } catch (err) {
      if (err.code == 401) {
        that.goLogin()

      } else {
        that.setData({
          msg: err.msg,
          bind: true
        })
        that.popTest()
      }

    }

  },
  go4Ssearch() {
    let that = this
    if (that.data.bind == true) {
      wx.navigateTo({
        url: '/pages/4Ssearch/4Ssearch?pin_id=' + that.data.pin_id + '&city_id=' + that.data.city_id
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '若更换4S店,则原4S店积分将被清零!',
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/4Ssearch/4Ssearch?pin_id=' + that.data.pin_id + '&city_id=' + that.data.city_id

            })
          } else {}
        }
      })
    }
  },
  popSuccessTest() {
    wx.showToast({
      title: this.data.msg,
      icon: '', //默认值是success,就算没有icon这个值,就算有其他值最终也显示success
      duration: 1300, //停留时间
    })
  },
  popTest() {
    wx.showToast({
      title: this.data.msg,
      icon: 'none', //如果要纯文本,不要icon,将值设为'none'
      duration: 1300
    })
  },
  goLogin() {
    wx.showModal({
      title: '提示',
      content: '您尚未登录,前往登录',
      success: function (res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '/pages/authorization/authorization?num=1'
          })

        } else {
          wx.switchTab({
            url: '/pages/home/home'
          })
        }
      }
    })
  },



})