<template>
<view id="retreat">
   <view class="company">
       <view class="detail">物流公司</view>
       <picker :value="selectedIndex" :range="list" @change="change">
           <input type="text" placeholder="选择物流公司" disabled :value="list[selectedIndex]"></input>
       </picker>
   </view>
    <view class="code">
       <view class="air_way">
           运单号
       </view>
        <input type="number" placeholder="填写运单号的条形码数字" @input="changeCodeNumber"></input>
    </view>
    <view class="submit">
        <button type="primary" @tap="submit">完成</button>
    </view>
</view>

</template>
<script>
import {pageLogin, getUrl,config,is} from '@/common/js/utils.js';

export default {
  data() {
    return {
      list: "",
      codeList: "",
      selectedIndex: "",
      codeNumber: "",
      refundNo: ""
    };
  },

  onShow() {// let app =getApp();
    // app.isBirthday().then(()=>{
    //     if(this.$cookieStorage.get("birthday_gift")){
    //         var giftData=this.$cookieStorage.get("birthday_gift").data;
    //         new app.ToastPannel().__page.showText(giftData);
    //     }
    // });
  },

  onLoad(e) {
    this.setData({
      refundNo: e.no
    });
    pageLogin(getUrl(), () => {
      this.showlogistics();
    });
  },

  components: {},
  props: {},
  methods: {
    change: function (e) {
      // console.log(e);
      // 修改选中项文案
      this.setData({
        selectedIndex: e.detail.value
      });
    },

    showlogistics() {
      this.$http.get({
        api: "api/shipping/methods",
        header: {
          Authorization: this.$cookieStorage.get('user_token')
        }
      }).then(res => {
        var arr = [];
        res.data.data.forEach(val => {
          arr.push(val.name);
        });
        this.setData({
          list: arr,
          codeList: res.data.data
        });
      });
    },

    changeCodeNumber(e) {
      this.setData({
        codeNumber: e.detail.value
      });
    },

    submit() {
      var message = null;

      if (!this.selectedIndex) {
        message = "请选择物流公司";
      } else if (!this.codeNumber) {
        message = "请填写运单号";
      }

      if (message) {
        wx.showModal({
          title: "提示",
          content: message
        });
        return;
      }

      var applyItem = {
        refund_no: this.refundNo,
        shipping_name: this.codeList[this.selectedIndex].name,
        shipping_tracking: this.codeNumber,
        shipping_code: this.codeList[this.selectedIndex].code // console.log(applyItem);

      };
      this.returnData(applyItem);
    },

    returnData(data) {
      this.$http.post({
        api: "api/refund/user/return",
        header: {
          Authorization: this.$cookieStorage.get('user_token')
        },
        data: data
      }).then(res => {
        if (res.data.status) {
          wx.showToast({
            title: "提交申请成功",
            duration: 1500,
            success: () => {
              setTimeout(() => {
                wx.redirectTo({
                  url: '/pages/afterSales/index/index'
                });
              }, 1500);
            }
          });
        } else {
          wx.showToast({
            title: "提交申请失败",
            duration: 1500
          });
        }
      });
    },

    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  },
  computed: {},
  watch: {}
};
</script>
<style rel="stylesheet/less" lang="less">
    @import "retreat";
</style>
