import { baseUrl } from "../../../request";

// pages/personCenter/subCommissionSetting/subCommissionSetting.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showCommissionRatePop: false,  // 控制弹窗显隐
    id: 0,  // 项目id
    subCommissionList: [],  // 下级抽佣列表
    projectId: 0,  // 项目ID
    maxCommissionRate: 0, //  最大抽佣比率
    LodingHidden: true, // 控制加载框取消
    nowMyUnitPrice: 0, // 现在的‘我的单价’，用于传给弹窗
    nowCommissionRate: 0,  // 现在的'抽成比例', 用于传给弹窗
    nowAgentUnitPrice: 0, // 现在的‘代理单价’,用于传给弹窗
    nowProjectDetailName: '', // 现在的‘项目详细名称’，用于传给弹窗 
    isSub: true, // 用于区分是项目抽佣页面还是单个用户抽佣页面
    searchText: '',
    tempSubCommissionList: []
  },
  
    // 输入框内容变化时更新数据
    onInput(e) {
      this.setData({
        searchText: e.detail.value,  // 获取用户输入的关键字
      });
    },
  
    onSearch() {
      let searchValue = this.data.searchText;  // 获取输入框的值
      let subCommissionList = this.data.subCommissionList
       // 筛选出 salespersonName 包含 searchText 的项
       let filtered = subCommissionList.filter(item => 
         item.subUserNickName.includes(searchValue)
      );
      this.setData({
        tempSubCommissionList: filtered
      })
    },

  // 点击“设价”按钮，显示弹窗
  showCommissionRatePop(e) {
    console.log('弹窗传值----->',e);
    this.setData({
      nowCommissionRate: e.currentTarget.dataset.currentcommissionrate,
      nowMyUnitPrice: e.currentTarget.dataset.nowmyunitprice,
      nowAgentUnitPrice: e.currentTarget.dataset.agentunitprice,
      nowProjectDetailName: e.currentTarget.dataset.projectdetailname,
      projectId: e.currentTarget.dataset.detailid,
      maxCommissionRate: e.currentTarget.dataset.maxcommissionrate,
    })
    this.setData({ showCommissionRatePop: true });
  },

  // 关闭弹窗（包括遮罩点击和取消按钮）
  closeCommissionRatePop() {
    this.setData({ showCommissionRatePop: false });
  },

  // 确认按钮回调
  handleCommissionRateConfirm(e) {
    this.getSubCommissionList();
    this.closeCommissionRatePop();
  },

  // 查询下级抽佣情况
  getSubCommissionList() {
    const id = this.data.id
    wx.request({
      url: baseUrl + '/projectCommission/query/sub/commission',
      method: 'POST',
      header: {
        Authorization: wx.getStorageSync('token'),
      }, 
      data: { id: id },
      success: res => {
        console.log('下级抽佣列表----->',res.data.data);
        if (res.data.code === 1) {
          this.setData({
            subCommissionList: res.data.data,
            tempSubCommissionList: res.data.data
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('----->',options);
    this.setData({
      id: options.id
    })
    this.getSubCommissionList()
  },

 /**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
  this.onRefresh();
},
onRefresh:function(){
  //导航条加载动画
  wx.showNavigationBarLoading()
  //loading 提示框
  wx.showLoading({
    title: '加载中...',
  })
  this.getSubCommissionList()
  setTimeout(function () {
    wx.hideLoading();
    wx.hideNavigationBarLoading();
    //停止下拉刷新
    wx.stopPullDownRefresh();
  }, 1000)
},
})