import {
    OrderStatus,
    OrderType,
    CommentStatus
} from "@/enums"
import bus from "@/utils/bus"
export const OrderMixin = {
    computed: {
        btnList() {
            let state = this.data.orderState
            if (state == OrderStatus.SUCCESS) {
                return this.getBtnListWithComm([ "buy"])
            } else if (state == OrderStatus.CLOSE) {
                return ["buy"];
            } else if (state == OrderStatus.UN_REFUND) {
                return this.getBtnListWithComm(["cancelRefund", "service"])
            } else if (state == OrderStatus.REFUND) {
                return this.getBtnListWithComm(["buy"])
            } else if (state == OrderStatus.PAYING) {
                return [];
            } else if (state == OrderStatus.UN_PAY) {
                return ["cancelOrder", "pay", "service"];
            } else if (state == OrderStatus.REFUND_FAIL) {
                return ["buy"]
            }
            return []
        },
        commBtn() {
            if (!this.data.orders) {
                return null;
            }
            let yesNum = 0,
                noNum = 0;
            this.data.orders.forEach((item) => {
                if (item.buyerState == CommentStatus.YES) {
                    yesNum++;
                } else if (item.buyerState == CommentStatus.NO) {
                    noNum++;
                }
            });
            if (noNum > 0) {
                return "comm";
            }
            if (yesNum > 0) {
                return "addComm";
            }
            return null;
        }
    },
    data() {
        return {
            btnMap: {
                cancelOrder: {
                    label: "取消",
                    type: "black",
                    click: this.handleCancelOrder,
                    plain: true,
                },
                cancelRefund: {
                    label: "撤销",
                    type: "black",
                    click: this.handleCancelRefund,
                    plain: true
                },
                comm: {
                    label: "评价",
                    type: "black",
                    click: this.handleComm,
                    plain: true
                },
                addComm: {
                    label: "追评",
                    type: "black",
                    click: this.handleComm,
                    plain: true
                },
                afterSale: {
                    label: "售后",
                    type: "black",
                    click: this.handleAfterSale,
                    plain: true
                },
                service: { label: "联系商家", click: this.handleService, type: "black", plain: true },
                pay: { label: "去付款", click: this.handlePay, type: "primary", plain: true },
                buy: { label: "再次购买", click: this.handleBuy, type: "primary", plain: true },
                
            },
        }
    },
    methods: {
        getBtnListWithComm(arr) {
            // return this.commBtn ? [this.commBtn].concat(arr) : arr
            return arr
        },
        async handleCancelOrder() {
            let apiToken=await this.getApiToken()
            if(apiToken){
                this.$api({
                    apiName: "cancelOrder",
                    header:{
                        "apiToken":apiToken
                    },
                    postData: this.data.payId,
                    success: () => {
                        this.data.orderState = OrderStatus.CLOSE;
                        bus.$emit("changeState", this.data);
                    },
                    complete: () => {
                        this.$hideLoading();
                    },
                });
            }else{
                this.$hideLoading();
            }
            
        },
        getApiToken(){
            this.$showLoading();
            return new Promise(reslove=>{
                this.$api({
                    apiName:"getCreatApiToken",
                    success:(res)=>{
                        reslove(res)
                    }
                })
            })
            
        },
        handleCancelRefund() {
            this.$showLoading();
            this.$api({
                apiName: "cancelOrderRefund",
                postData: this.data.oid,
                success: () => {
                    this.data.orderState = OrderStatus.SUCCESS;
                    bus.$emit("changeState", this.data);
                },
                complete: () => {
                    this.$hideLoading();
                },
            });
        },
        handleComm() {
            if (this.data.orders.length == 1) {
                let { oid } = this.data;
                let { skuId, buyerState: status, commentId } = this.detail.order1st;

                let query = { oid, skuId, status, commentId }
                return this.$router.push({ name: "release-comment", query })
            }

            this.setUnCommentOrders(this.data.orders);
            let { oid, enterpriseName: merchantName } = this.data;
            let query = { oid, merchantName }
            this.$router.push({ name: "un-comment-list", query })
        },
        handleAfterSale() {
            let name = 'apply-refund', query = { oid: this.data.oid, type: OrderType.ORDER }
            let { orderState } = this.detail;
            if (
                orderState == OrderStatus.UN_REFUND ||
                orderState == OrderStatus.REFUND ||
                orderState == OrderStatus.REFUND_FAIL
            ) {
                query = { oid: this.data.oid }
                name = 'refund-money-detail'
            }

            this.$router.push({ name, query })
        },
        handlePay() {
            let query = { pid: this.data.payId }
            this.$router.push({ name: "payment", query })
        },
        handleBuy() {
            if (this.data.orders.length == 1) {
                this.goGoods(this.data.orders[0].goodsId,this.data.orders[0].type,this.data.orders[0].salesType)
            } else {
                this.goHome()
            }
        },
        handleService() {
            this.contactCustomer(this.data.merchantId)
        },
        contactCustomer(merchantId) {
            this.$showLoading();
            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('该商家暂未开通客服！')
                    }
                },
                complete: () => {
                    this.$hideLoading();
                },
            })
        },
    }
}