// mine/pages/traderList/traderList.js
const { getTraderList, getCapital } = require("../../api/api");
Page({
  data: {
    radio:'',  
    keyword:'',//搜索关键字
    listData:[],//列表
    capitalList:[],//资方列表
    capitalId:'',//资方id
  },
  onLoad(options) {
    this.setData({
      radio: wx.getStorageSync('traderId') || '',
      capitalId: wx.getStorageSync('capitalId') || '',
    });
    this.getCapitalList(()=>{this.getList()});
  },
  onShow() {

  },
  /** 资方列表 **/
  getCapitalList(callback) {
    callback = callback || function(){};
    getCapital({  
    }).then((res) => {
      if(res.code == 200){
        res.data.unshift({
          id:'',
          name:'所有资方'
        })
        this.setData({
          capitalList: res.data
        })
      }else{
        this.setData({
          capitalList: [
            {
              id:'',
              name:'所有资方'
            },
            {
              id:'01',
              name:'越秀'
            },
            {
              id:'03',
              name:'广发'
            },
            {
              id:'04',
              name:'华融'
            }
          ]
        })
      }
      callback();
    })
  },
  /** 选择资方 **/
  cateFn(e){
    this.setData({
      capitalId:e.currentTarget.dataset.obj.id,//资方id
      radio:'',
    })
    this.getList();
  },
  /** 搜索输入事件 **/
  keyWordsChange(e) {
    this.setData({
      keyword: e.detail,
    });
  },
  /** 搜索 **/
  keyWordsSearch(e){
    this.getList();
  },
  /** 列表 **/
  getList(){
    getTraderList({
      userId: wx.getStorageSync('userId'),
      keyword: this.data.keyword,
      partner:this.data.capitalId,
    }).then(res=>{
      if(res.code==200){
        if(res.data){
          var listData = res.data.map(item=> item.columns)
          listData.unshift({
            traderId:'',
            name: '所有代理商'
          })
          this.setData({
            listData
          });
        }else{
          wx.showToast({
            title: '未找到您搜索的代理商',
            icon:'none'
          })
        }     
      }else{
        wx.showToast({
          title: res.msg,
          icon:'none'
        })
      }
    });
  },
  /** 单选 **/
  onChange(e) {
    this.setData({
      radio: e.detail,
    });    
  }, 
  /** 返回 **/
  back(e) {
    wx.navigateBack();
  },
  /** 提交 **/
  submit(){
    // if(!this.data.radio){
    //   wx.showToast({
    //     title: '请先选择一个服务商',
    //     icon:'none'
    //   })
    //   return;
    // }
    wx.setStorageSync('traderId', this.data.radio)
    const traderObj = this.data.listData.find(itm=>itm.traderId==this.data.radio)
    wx.setStorageSync('traderName', traderObj.name)
    wx.setStorageSync('capitalId', this.data.capitalId);
    const Obj = this.data.capitalList.find(itm=>itm.id==this.data.capitalId)
    wx.setStorageSync('capitalName', Obj.name);
    wx.navigateBack();
  },  
})