import HTTP from "../../../../requestFn/Api";
const app = getApp();
Page({
  data: {
    should_pay_amount: 0,
    plate_no: "",
    vehicle_id: "",
    garden_id: "",
    months: "",
  },
  onLoad() {
    const {
      garden_id
    } = app.globalData;
    if (garden_id) {
      this.setData({
        garden_id
      })
    }
  },
  onShow(options) {
    const that = this;
    const {
      garden_id
    } = that.data;
    if (garden_id) {
      wx.getStorage({
        key: "car_list",
        success(e) {
          e.data.map(item => {
            if (item.selected) {
              that.setData({
                plate_no: item.plate_no,
                vehicle_id: item.id,
                brand_pic: item.brand_pic
              })
              that.getVehicle()
            }
          })
        },
        fail() {
          that.getMyCar()
        }
      })
    }
  },
  getMyCar() {
    const that = this;
    const {
      garden_id
    } = that.data;
    HTTP.GetuserCar({
      garden_id
    }).then(res => {
      const result = res.list.map((item, index) => {
        if (index == 0) {
          that.setData({
            plate_no: item.plate_no,
            vehicle_id: item.id,
            brand_pic: item.brand_pic
          })
          that.getVehicle()
          return {
            ...item,
            selected: true,
            detail: `${item.brand}${item.seat}坐（${item.displacement}${item.displacement_unit}）`
          }
        } else {
          return {
            ...item,
            selected: false,
            detail: `${item.brand}${item.seat}坐（${item.displacement}${item.displacement_unit}）`
          }
        }
      })
      wx.setStorage({
        data: result,
        key: "car_list"
      })
    })
  },
  goMyCar() {
    wx.navigateTo({
      url: '../../../mine/pages/mycar/mycar',
    })
  },
  async getVehicle() {
    const {
      garden_id,
      vehicle_id
    } = this.data;
    const res = await HTTP.GetMonthsVehicle({
      vehicle_id,
      garden_id
    });
    if (res.bind_id) {
      this.setData({
        bind_id: res.bind_id,
        desc: res.desc
      })
    } else {
      this.setData({
        desc: res.message
      })
    }
  },
  getMonth(e) {
    let value = e.detail.value;
    if (value == '') {
      this.setData({
        should_pay_amount: 0,
        months: 0
      })
    } else {
      this.setData({
        months: value,
      })
      if (this.data.bind_id) {
        this.pre_pay()
      }
    }
  },
  pre_pay() {
    const {
      garden_id,
      bind_id,
      months,
    } = this.data;
    HTTP.Getpre_pay_info({
      garden_id,
      bind_id,
      months
    }).then(res => {
      this.setData({
        should_pay_amount: res.should_pay_amount,
        charge_desc: res.charge_desc,
      })
    })
  },
  car_pay() {
    const that = this;
    const {
      garden_id,
      bind_id,
      months,
      plate_no,
      should_pay_amount
    } = this.data;
    if (plate_no == "") {
      wx.showToast({
        title: "请选择车辆",
        duration: 2000,
        icon: "error",
      });
      return false
    }
    if (months == "") {
      wx.showToast({
        title: "请输入充值月数",
        duration: 2000,
        icon: "error",
      });
      return false
    }
    HTTP.Pre_pay({
      garden_id,
      bind_id,
      months: months - 0,
      should_pay_amount,
      pay_amount: should_pay_amount
    }).then(res => {
      console.log(res);
      if (res.code == 0) {
        that.setData({
          order_id: res.data.order_id,
        })
        const data = JSON.parse(res.data.prepay_info)
        wx.requestPayment({
          ...data,
          timeStamp: data.timeStamp + "",
          success(res) {
            console.log(res);
            wx.showToast({
              title: '支付成功',
              icon: "success",
              success() {
                wx.navigateBack({
                  delta: 1
                })
              }
            })
          },
          fail(res) {
            console.log(res);
            const {
              order_id
            } = that.data;
            HTTP.CancleOrder({
              order_id,
              garden_id,
              by_user: false,
            }).then(res => {
              if (res.code == 0) {
                wx.showToast({
                  title: '取消支付',
                  icon: "error"
                })
              }
            })
          }
        })
      }
    })
  }
})