import { confirmReceipt, orderCancel, orderDelete, checkOrderBuyAgain } from '@/services/api/order.js'
import { shoppingAddCartFromOrder } from '@/services/api/cart.js'

const orderList = {
    data() {
        return {
            hanldOrder: {}, // 当前要处理的订单信息
            modal: false,
            modalText: '',
            buyAgainModal: false,
            invalidList: [], // 失效商品列表
        }
    },

    methods: {
        // 操作订单
        handleConfirmClick(e) {
            const { type, order } = (this.hanldOrder = e)
            switch (type) {
            // 其他操作
            case 'again':
                console.log('再次购买')
                this.buyAgain()
                break
            case 'share':
                console.log('分享订单')
                this.handleShare()
                break
            case 'query':
                console.log('订单详情')
                this.$router.navigateTo('order/order-detail?orderId=' + order.orderId)
                break
            case 'fightDetail':
                this.$router.navigateTo('share-buy/share-buy-detail?collageId=' + order.collageId)
                break
            case 'fightShare':
                console.log('分享邀请好友拼团')
                this.$router.navigateTo('share-buy/share-buy-detail?collageId=' + order.collageId)
                break
                // 常规操作
            case 'cancel':
                this.modal = true
                this.modalText = '确认取消该订单吗？'
                break
            case 'delete':
                this.modal = true
                this.modalText = '确认删除该订单吗？'
                break
            case 'confirm':
                this.modal = true
                this.modalText = '是否确认收货？'
                break
            case 'pay':
                this.modal = true
                this.modalText = '确认支付？'
                break
            }
        },
        // 确认收货
        handleConfirm(order) {
            console.log('确认收货')
            return confirmReceipt({ orderId: order.orderId })
        },
        // 取消订单
        handleCancel(order) {
            console.log('取消订单')
            return orderCancel({ orderId: order.orderId })
        },
        // 删除订单
        handleDelete(order) {
            console.log('删除订单')
            return orderDelete({ orderId: order.orderId })
        },
        // 支付订单
        handlePay(order) {
            console.log('支付')
            this.miniWxPayFor(order)
        },
        // 再次购买初始化查询订单商品信息
        handleAgain(order) {
            console.log('再次购买初始化查询订单商品信息')
            return checkOrderBuyAgain({ orderId: order.orderId })
        },
        // 查看物流
        handleQuery(order) {
            console.log('查看物流')
        },
        // 分享
        handleShare(order) {
            console.log('分享')
        },
        // 再次购买
        async buyAgain() {
            const { type, order } = this.hanldOrder
            try {
                await this.handleAgain(order)
                this.joinToCart(order)
            } catch (error) {
                if (error.data && error.data.length > 0) {
                    this.buyAgainModal = true
                    this.invalidList = error.data
                }
            }
        },

        // 确认再次购买
        buyAgainModalClick(e) {
            this.buyAgainModal = false
            if (!e.index) return
            this.joinToCart(this.hanldOrder.order)
        },

        // 再次购买弹窗关闭
        buyAgainModalHide() {
            console.log('再次购买弹窗关闭')
            this.invalidList = []
        },

        // 一键加入购物车
        async joinToCart(order) {
            try {
                await shoppingAddCartFromOrder({ orderId: order.orderId })
                this.$router.switchTab('cart')
            } catch (error) {
                console.log(error.message)
            }
        },

        // 操作订单确认事件
        async handleModalConfirm(e) {
            this.modal = false
            if (!e.index) return

            const { type, order } = this.hanldOrder

            // 操作订单
            const confirmFunc = {
                cancel: this.handleCancel, //取消订单
                delete: this.handleDelete, //删除订单
                pay: this.handlePay, //支付订单
                confirm: this.handleConfirm //确认收货
            }
            try {
                const res = await confirmFunc[type](order)
                // 确认操作订单成功后的回调都在这里执行 在this.$on 中监听 orderAction 事件的执行
                this.$emit('orderAction')
                if (res && res.msg) this.$toast.success(res.msg)
            } catch (error) {
                this.$toast.error(res.msg)
            }
        },
        //订单状态文字
        stateExpFormat(status) {
            const stateTextObject = {
                4: '交易完成',
                5: '订单完成',
                6: '已关闭',
                7: '交易全退',
                77: '交易全退',
                11: '待付款待发货',
                12: '待付款部分发货',
                13: '待付款已发货',
                // 21: '部分付款待发货',
                // 22: '部分付款部分发货',
                // 23: '部分付款已发货',
                31: '已付款待发货',
                32: '已付款部分发货',
                33: '已付款已发货',
                111: '待付款待发货',
                81: '拼团中',
                82: '拼团成功待发货',
                83: '拼团成功已发货'
            }
            return stateTextObject[status] || ''
        },
    }
}

export default orderList
