// pages/getMerchant/index.js
const config = require('../../config/config.js')
const Http = require('../../utils/HttpBasics.js')
const util = require('../../utils/util.js')
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectList:[],
    list: [],
    selectMerchant:null,
    businessId:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      paramData: options
    })
    this.getMerchent()
  },
  getMerchent() {
    let _this = this;
    Http.get({
      url: config.api.getMerchentList
    })
      .then(res => {
        _this.setData({
          list: res.data,
          selectList:res.data
        })
      })
      .catch(err => {
        tt.showToast({
          title: err.message,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  checkMerchant(e){
    console.log(e)
    let id=e.target.id;
    this.data.selectList.map((item,index)=>{
      if(id==item.id){
        console.log(item,item.businessId)
        this.setData({
          selectMerchant:item,
          businessId: item.businessId
        })
      }
    })
  },
  submitForm(){
    if (this.data.selectMerchant!=null){
      tt.redirectTo({
        url: `/pages/operation/index?address=${this.data.paramData.address}&id=${this.data.paramData.id}&levelName=${this.data.paramData.levelName}&name=${this.data.paramData.name}&nickName=${this.data.paramData.nickName}&phone=${this.data.paramData.phone}&score=${this.data.paramData.score}&credit=${this.data.paramData.credit}&type=1&merchantName=${this.data.selectMerchant.merchantName}&merchant_id=${this.data.selectMerchant.id}&isAdmin=1&merchantType=1&businessId=${this.data.businessId}`,
      })
    }else{
      tt.showToast({
        title: '抱歉，您未选择门店',
        icon: 'none',
        duration: 2000,
        mask: false
      });
    }
  },
  getList(e){
    console.log(e.detail.value)
    let selectList = [];
    var reg = new RegExp(e.detail.value.toUpperCase());
    this.data.list.map((item,index)=>{
      if (item.merchantName.toUpperCase().match(reg) || e.detail.value==''){
        selectList.push(item)
      }
    })
    let haveIf=false;
    selectList.map((item,index)=>{
      console.log(item,this.data.selectMerchant)
      if (this.data.selectMerchant!=null&&item.id == this.data.selectMerchant.id){
        haveIf=true
      }
    })
    if (!haveIf){
      this.setData({
        selectMerchant:null
      })
    }
    // debugger
    this.setData({
      selectList
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})