import {
    DeliveryStatus,
    OrderType,
} from "@/enums";


export const DeliveryMixin = {

    computed: {
        btnList() {
            if (this.data.deliveryState == DeliveryStatus.UN_SHIP) {
                return ["service"];
            } else if (this.data.deliveryState == DeliveryStatus.SHIP) {
                return ["express", "afterSale", "confirm"];
            } else if (this.data.deliveryState == DeliveryStatus.RECEIVE) {
                return ["afterSale"];
            } else if (this.data.deliveryState == DeliveryStatus.CANCEL) {
                return [];
            } else if (this.data.deliveryState == DeliveryStatus.UN_REFUND) {
                return ["cancelRefund", "afterSale", "service"];
            } else if (this.data.deliveryState == DeliveryStatus.REFUND) {
                return [];
            } else if (this.data.deliveryState == DeliveryStatus.SUCCESS) {
                return ["buy"];
            } else if (this.data.deliveryState == DeliveryStatus.REFUND_FAIL) {
                return ["afterSale"];
            } else if (this.data.deliveryState == DeliveryStatus.REFUND_EXPRESS) {
                return ["afterSale"];
            }
            return [];
        },
    },

    data() {
        return {
            btnMap: {
                express: {
                    label: "查看物流",
                    type: "black",
                    click: this.handleExpress,
                    plain: true
                },
                cancelRefund: {
                    label: "撤销",
                    type: "black",
                    click: this.handleCancelRefund,
                    plain: true
                },
                afterSale: {
                    label: "售后",
                    type: "black",
                    click: this.handleAfterSale,
                    plain: true
                },
                confirm: {
                    label: "确认收货",
                    type: "primary",
                    click: this.handleConfirm,
                    plain: false
                },
                buy: {
                    label: "再次购买",
                    type: "primary",
                    click: this.handleBuy,
                    plain: false
                },
                service: {
                    label: "联系商家",
                    type: "primary",
                    click: this.handleService,
                    plain: false
                },
            },
        };
    },
    methods: {

        handleExpress() {
            let query = { oid: this.data.deliveryId, status: this.data.deliveryState }
            this.$router.push({ name: 'delivery-express-detail', query })
        },
        handleCancelRefund() {
            this.$showLoading();
            this.$api({
                apiName: "cancelDeliveryRefund",
                postData: this.data.deliveryId,
                success: () => {
                    this.data.deliveryState = DeliveryStatus.CANCEL;
                },
                complete: () => {
                    this.$hideLoading();
                },
            });
        },
        handleAfterSale() {
            let name = 'apply-refund', query = { oid: this.data.deliveryId, type: OrderType.DELIVERY }
            let { deliveryState } = this.data;
            if (
                deliveryState == DeliveryStatus.UN_REFUND ||
                deliveryState == DeliveryStatus.REFUND_EXPRESS ||
                deliveryState == DeliveryStatus.REFUND ||
                deliveryState == DeliveryStatus.REFUND_FAIL
            ) {
                name = 'refund-goods-detail'
                query = { oid: this.data.deliveryId }
            }

            this.$router.push({ name, query })
        },
        handleConfirm() {
            this.$showLoading();
            this.$api({
                apiName: "confirmDeliveryReceipt",
                postData: this.data.deliveryId,
                success: () => {
                    this.data.deliveryState = DeliveryStatus.RECEIVE;
                },
                complete: () => {
                    this.$hideLoading();
                },
            });
        },
        handleBuy() { },
        handleService() {
            this.contactCustomer(this.data.merchantId)
        },
        contactCustomer(merchantId) {
            this.$api({
                apiName: "listShopCustomer",
                postData: { merchantId: merchantId },
                success: (res) => {
                    if (res && res.length > 0) {
                        let customer = res[0]
                        let query = {customer: JSON.stringify(customer)}
                        this.$router.push({name: "customer-service-chat", query})
                    } else {
                        this.$toast('该商家暂未开通客服！')
                    }
                },
            })
        },
    },
}