// pages/shop/product/affirmorder.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        totalPrice: 0,
        goodsList: [], //商品列表
        shopId: '',
        shopDetailBean: null, //店铺详情
        baseImg: app.urls.baseImgUrl,
        remark: '', //备注
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            shopId: options.shopId
        })
        let shopCart = app.globalData.shopCart
        let price = 0
        shopCart.forEach(it => {
            if (it.shopId == this.data.shopId) {
                it.totalPrice = it.goodsUnitPrice * it.buyNum
                this.data.goodsList = this.data.goodsList.concat(it)
                price += it.totalPrice
            }
        })
        //计算价格
        this.setData({
            totalPrice: price *= 100,
            goodsList: this.data.goodsList
        })
        this.getShopDetailById(this.data.shopId)
    },
    //提交订单
    onSubmit() {
        let _self = this
        wx.showLoading({
            title: '预定中...',
        })
        var params = _self.buildSubmitParams()
        console.log(params)
        app.restAjax.post(app.restAjax.path(app.restAjax.apis.doSaveOrder, [app.restAjax.baseUrl.tradeUrl]), params, {
            headers: {
                token: app.globalData.token
            }
        }, (code, data) => {
            wx.hideLoading({})
            if (code == 200) {
                //提交成功
                app.globalData.shopCart = []
                wx.redirectTo({
                    url: '../../mine/order/ordersuccess?price=' + _self.data.totalPrice / 100 + '&shopId=' + _self.data.shopId + '&orderId=' + data.data,
                })
            }
        }, (code, err) => {
            wx.hideLoading({})
            if (err.msg) {
                app.dialog.msg(err.msg)
            } else {
                wx.showToast({
                    title: '网络错误',
                    icon: 'error'
                })
            }
        })
    },
    //构建订单信息
    buildSubmitParams() {
        let _self = this
        var array = []
        _self.data.goodsList.forEach(it => {
            var item = {
                "goodsCount": it.buyNum,
                "goodsId": it.goodsId,
                "goodsLogo": it.goodsIcon,
                "goodsName": it.goodsName,
                "goodsPrice": it.goodsUnitPrice,
                "shopId": it.shopId,
                "totalPrice": it.totalPrice,
                "words": _self.data.remark
            }
            array = array.concat(item)
        })
        return array
    },
    textareaBInput(e) {
        this.setData({
            remark: e.detail.value
        })
    },
    //获取店铺详情
    getShopDetailById(id) {
        wx.showLoading({
            title: '加载中...',
        })
        app.restAjax.get(app.restAjax.path(app.restAjax.apis.getShopDetail, [app.restAjax.baseUrl.tradeUrl, id]), {}, {
            headers: {
                token: app.globalData.token
            }
        }, (code, data) => {
            wx.hideLoading({})
            if (code == 200) {
                this.setData({
                    shopDetailBean: data
                })
            }
        }, (code, err) => {
            wx.hideLoading({})
            if (err.msg) {
                app.dialog.msg(err.msg)
            } else {
                wx.showToast({
                    title: '网络错误',
                    icon: 'error'
                })
            }
        })
    },
    //商品数量修改
    onChange(e) {
        let buyNum = e.detail
        let goods = e.currentTarget.dataset.goods
        let _self = this
        let price = 0
        _self.data.goodsList.forEach(it => {
            if (it.goodsId == goods.goodsId) {
                it.buyNum = buyNum
                it.totalPrice = it.goodsUnitPrice * it.buyNum
            }
            price += it.totalPrice
        })
        _self.setData({
            totalPrice: price * 100,
            goodsList: _self.data.goodsList
        })
        app.globalData.shopCart = _self.data.goodsList
    },
    // 删除商品
    delGoods(e) {
        let _self = this
        var index = e.currentTarget.dataset.index
        wx.showModal({
            title: '提示',
            content: '确定要删除该商品吗?',
            success(res) {
                if (res.confirm) {
                    _self.data.goodsList.splice(index, 1)
                    _self.setData({
                        goodsList: _self.data.goodsList
                    })
                    var price = 0
                    _self.data.goodsList.forEach(it => {
                        it.totalPrice = it.goodsUnitPrice * it.buyNum
                        price += it.totalPrice
                    })
                    _self.setData({
                        totalPrice: price * 100
                    })
                    app.globalData.shopCart = _self.data.goodsList
                }
            }
        })
    }
})