// pages/order/serviceDetails/serviceDetails.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goods: {},
        carNum: 1,
        carPrice: 0,
        title: "",
        address: "",
        worke_id: "",
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var goods_id = options.goods_id;
        var title = options.title;
        var address = options.address;
        var worke_id = options.worke_id;
        this.setData({
            title: title,
            address: address,
            worke_id: worke_id,
            goods_id: goods_id
        })
        var that = this;
        app.api.useApi(app.globalData.baseAppUrl + "api/goods_info", {
            network_id: wx.getStorageSync('nid'),
            good_id: goods_id
        }, "get").then(function (res) {
            console.log(res);
            if (res.code != 200) {
                console.log(res.code);
                wx.showLoading({
                    title: res.message.msg,
                })
            } else {
                that.setData({
                    goods: res.message.data.good,
                    carPrice: res.message.data.good.price.start_time!=0?res.message.data.good.price.sale_price:res.message.data.good.price.price,
                })

            }

            wx.hideLoading();

        }).catch(function (err) {
            console.log(222);
        })
    },

    minus() {
        if (this.data.carNum > 1) {
            var carnum = this.data.carNum - 1
            this.setData({
                carNum: carnum
            })

        }
        console.log(this.data.goods.price.start_time);
        if (this.data.goods.price.end_time == 0 && this.data.goods.price.start_time == 0) {
            var price = this.data.goods.price.price;
        } else {
            var price = this.data.goods.price.sale_price;
        }
        console.log(price);
        this.setData({

            carPrice: carnum * price
        })


    },

    plus() {
        console.log(this.data.carNum)
        var carnum = this.data.carNum + 1
        //  if(this.data.carNum>=1){
        this.setData({
            carNum: carnum
        })
        // }
  
        if (this.data.goods.price.end_time == 0 && this.data.goods.price.start_time == 0) {
            var price = this.data.goods.price.price;
        } else {
            var price = this.data.goods.price.sale_price;
        }

        this.setData({
            carPrice: carnum * price
        })
    },

    getOrder() {
        var nid = this.data.worke_id;
        app.api.useApi(app.globalData.baseAppUrl + "api/CreateOrder", {
            network_id: nid,
            good_id: this.data.goods_id,
            num: this.data.carNum
        }, "post").then(function (res) {
            console.log(res);
            if (res.code != 200) {
                wx.showLoading({
                    title: res.message.msg,
                })
                wx.setTimeout(() => {
                    wx.hideLoading();
                }, 1000);
            } else {
                var order_no = res.message.data.order_no;
                wx.navigateTo({
                    url: '/pages/order/confirmOreder/confirmOreder?order_no=' + order_no,
                })
            }



        }).catch(function (err) {
            console.log(222);
        })




    },


})