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

  data: {
    pgencyPriceAble: true,  // 代理单价input启用/禁用
    commissionRateAble: false, // 抽成比例input启用/禁用
    LodingHidden: false, // 遮罩禁用启用
  },
  /**
   * 组件的属性列表
   */
  properties: {
    show: {           // 控制显示/隐藏
      type: Boolean,
      value: false
    },
    pgencyPrice: {   // 代理单价
      type: String,
      value: 0
    },
    commissionRate: {  // 抽成比例
      type: String,
      value: 0
    },
    myUnitPrice: {  // 我的单价
      type: Number,
      value: 0
    },
    projectDetailName: {   // 项目详细名称
      type: String,
      value: ''
    },
    detailId: {  // 下级项目明细id
      type: Number,
      value: 0
    },
    maxCommissionRate: {  // 最大抽佣比例
      type: Number,
      value: 0
    },
    isSub: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    close() {
      this.resetStatus();
      this.triggerEvent('close');
    },
    cancel() {
      this.resetStatus();
      this.triggerEvent('cancel');
    },
    confirm() {
      // 触发confirm事件带数据
      const id = this.data.detailId;
      const Rate = this.data.commissionRate;
      const maxRate = this.data.maxCommissionRate;
      const isSub = this.data.isSub;
      console.log('id---->', id);
      console.log('Rate--->',Rate);
      console.log('isSub---->',isSub);
      if (Rate > maxRate || Rate < 0) {
        wx.showModal({
          title: '抽佣比率错误',
          content: '抽佣比率大于最大抽成',
          showCancel: false
        })
        return;
      }
      wx.showLoading({
        title: '加载中',
        mask: true
      })
      // 发送请求
      if (isSub) {
        wx.request({
          url: baseUrl + '/projectCommission/update/sub/rate',
          method: 'POST',
          header: {
            Authorization: wx.getStorageSync('token'),
          },
          data: {
            id: id,
            currentCommissionRate: Rate
          },
          success: res=> {
            console.log('用户抽用户----->',res.data);
            if ( res.data.code === 1 )  {
              wx.hideLoading()  // 加载框关闭
              this.triggerEvent('confirm', {});
            } else {
              wx.showToast({
                title: '服务异常',
                icon: 'error'
              })
            }
          }
        })
      } else {
        wx.request({
          url: baseUrl + '/projectCommission/update/rate',
          method: 'POST',
          header: {
            Authorization: wx.getStorageSync('token'),
          },
          data: {
            id: id,
            currentCommissionRate: Rate
          },
          success: res => {
            console.log('项目抽佣结果---->',res.data);
            if (res.data.code === 1) {
              wx.hideLoading()  // 加载框关闭
              this.triggerEvent('confirm', {});
            } else {
              wx.showToast({
                title: '服务异常',
                icon: 'error'
              })
            }
          }
        })
      }
      
      this.resetStatus();
    },

    // 单选框选择方法
    radioChange(e) {
      const selected = e.detail.value;
      if (selected === '代理价') {
        this.setData({ 
          pgencyPriceAble: false,
          commissionRateAble: true,
        })
      } else {
        this.setData({
          pgencyPriceAble: true,
          commissionRateAble: false,
        })
      }
    },

    // 重置input状态
    resetStatus() {
    this.setData({
      pgencyPriceAble: true,  
      commissionRateAble: false, 
    })
    },

    /**
    * 处理输入框的变化
    */
    handleInputChange(e) {
      const { field } = e.target.dataset;  // 获取字段名
      this.setData({
        [field]: e.detail.value, // 动态更新输入框数据
      });
    },

    // 计算单价——当输入抽成比率失焦时自动计算
    calculateUnitPrice() {  
      const tempCommissionRate = this.data.commissionRate / 100; // 暂存抽成比率
      const tempMyUnitPrice = this.data.myUnitPrice;  // 暂存我的单价
      let res = parseFloat((tempMyUnitPrice * (1 - tempCommissionRate)).toFixed(2)) ;
      console.log(tempCommissionRate, tempMyUnitPrice, res)
      // 计算单价 (避免精度丢失问题)
      this.setData({
        pgencyPrice : res
      })
    },
    
    


    // 计算比率——当输入代理单价失焦时自动计算
    calculateRatio() {  
      const tempPgencyPrice = this.data.pgencyPrice;  // 暂存代理单价
      const tempMyUnitPrice = this.data.myUnitPrice;  // 暂存我的单价
      const res = parseFloat(((1 - tempPgencyPrice / tempMyUnitPrice) * 100).toFixed(2))
        console.log(tempPgencyPrice, tempMyUnitPrice, res)
        this.setData({
          commissionRate: res
        })
        console.log('抽佣比率------>',this.data.commissionRate);
      }
    }
})