// component/applyRefund/applyRefund.js

const app = getApp()
const wxService = require('../../utils/wxService')
const utils = require('../../utils/util')
const envInfo = require('../../config/index').envInfo

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: { // 显示标识
      type: Boolean,
      value: false
    },
    refundId : {
      type: String,
      value: ''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    isSelect: false,
    reasonList : [],
    reasonIndex : '',
    code : ''
  },

  lifetimes:{
    ready(){
      this.getLogisticCompanyList();
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    selectCoupon() {
      this.setData({
        isSelect: !this.data.isSelect
      })
    },
    hidePopup: function () { // 隐藏弹出框
      this.setData({
        show: false
      })
    },
    //选择改变
    bindSelectRefundReason(event){
      this.setData({
        reasonIndex: event.detail.value
      })
    },
    //inpuChange
    inpuChange(e){
      this.setData({
        code : e.detail.value
      });
    },
    //确认发货
    onTabSure(){
      let param = {};
      param.refundId = this.data.refundId;
      let company = this.data.reasonList[this.data.reasonIndex];
      if(!company){
        wx.showToast({
          title: '请选择物流公司',
          icon: 'none'
        });

        return;
      }

      if (!this.data.code){
        wx.showToast({
          title: '请输入物流单号',
          icon : 'none'
        })
        return ;
      }

      param.refundLogistic = {
        "code": this.data.code,
        "supplierCode": company.companyCode,
        "supplierName": company.company,
      };
      wx.showLoading({
        title: '请求中',
      });

      wxService.post(`/sale/refund/buyer/deliver`, param).then(res => {
        const { result, data } = res.data
        if (result == 0) {
          wx.showToast({
            title: `发货成功`,
            icon: 'none'
          });

          this.setData({
            show : false,
            code : '',
            reasonIndex : '',
          });

          this.triggerEvent('ok', true)
        }
      }).finally(() => {
        wx.hideLoading()
      })
    },
    //获取物流公司信息
    getLogisticCompanyList(){
      wxService.get(`/sale/logistics/findLogisticsCompanyList`).then(res => {
        if (!res) return false
        const { result, data } = res.data
        if (result == 0) {
          this.setData({
            reasonList: data
          })
        }

      }).finally(() => {
      })
    },

  },

})
