const util = require('../../../utils/util.js');
const app = getApp();
Page({
  data: {
    reasonData: [],
    current: 1,
    imgPath: util.config.imgPath,
    datas: {},
    reason: '',
    mask: false,
    refundBack: '',
    refundpwd: '',
    orderid: '',
    status: '',
    groupId: ''
  },
  onLoad(options) {
    this.setData({
      refundpwd: options.refundpwd,
      orderid: options.orderid,
      status: options.refundStatus,
      groupId: options.groupid || ''
    });
    let reasonData = [],
      current = 1;
    if (this.data.status == 15) {
      current = 4;
    }
    this.setData({
      current
    })
    this.getData();
  },
  getData() {
    let that = this;
    util.ajax({
      func: "v2/order/detail",
      data: {
        "orderid": that.data.orderid,
        "otype": 0,
        "password": that.data.refundpwd || "",
      }
    }, function (res) {
      if (res.code == 0) {
        let reasonData = [];
        if (res.data.atype == '1') { //电商
          reasonData = [{
              type: 5,
              txt: '不喜欢/不想要'
            },
            {
              type: 6,
              txt: '质量问题'
            },
            {
              type: 0,
              txt: '其他原因'
            }
          ];
        } else {
          if (that.data.status == 15) {
            reasonData = [{
              type: 4,
              txt: '候补退款'
            }], current = 4;
          } else {
            reasonData = [{
                type: 1,
                txt: '行程有变'
              },
              {
                type: 2,
                txt: '买多了/买错了'
              },
              {
                type: 3,
                txt: '孩子突发身体原因'
              },
              {
                type: 0,
                txt: '其他原因'
              }
            ];
          }
        }
        that.setData({
          datas: res.data,
          reasonData,
          current: reasonData[0].type
        });
      }
    })
  },
  backBtn() {
    wx.redirectTo({
      url: '/pages/order/index/order?otype=2'
    })
  },
  confirm(e) {
    this.setData({
      reason: e.detail.value
    });
  },
  bindreason(e) {
    this.setData({
      current: e.currentTarget.dataset.type
    });
  },
  img() {
    this.data.datas.logo = '/images/noimg.png';
    this.setData({
      datas: this.data.datas
    });
  },
  refundRequest() {
    console.log(this.data.orderInfo);
    let that = this,
      reason = this.data.reason,
      current = this.data.current;
    if (current == 0 && reason == '') {
      util.showTips('请输入退款原因。');
      return false;
    }
    util.ajax({
      func: "order/commit_refund",
      data: {
        "reasonType": current,
        "reason": reason,
        "orderid": that.data.orderid,
        "password": that.data.refundpwd || "",
        "type": '0',
      },
      method: 'POST',
    }, function (res) {
      if (res.code == 0) {
        wx.setStorageSync('_REFUND_', 'REFUND');
        that.setData({
          refundBack: res.reason,
          mask: true
        })
      }  else {
        let pages = getCurrentPages();
        wx.showModal({
          title: '提交失败',
          content: res.reason,
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              wx.navigateBack({
                detail: 2
              });
            }
          }
        });
      }
    });
  }
})