export default {
    data() {
        return {
            actionList: [],
            showReportSheet: false,
            order: {},
        };
    },
    onShow() {
        let selectEditAddr = uni.getStorageSync("selectEditAddr");
        if (selectEditAddr.id) {
            this.handleAddressSubmit(selectEditAddr.id);
        }
    },
    methods: {
        //操作成功之后的回调
        successFeedback() {
            uni.$emit("btnActionSuccess");
        },

        //修改地址
        handleAddressSubmit(addressId) {
            const params = {
                orderId: this.order.orderId,
                addressId,
            };
            uni.$u.http
                .post("/token/order/modifyAddress", params)
                .then((res) => {
                    if (res.code === 200) {
                        uni.showToast({
                            title: "地址修改成功",
                            icon: "none",
                        });
                        this.$emit("success");
                        this.successFeedback();
                        uni.removeStorageSync("selectEditAddr");
                    } else {
                        uni.showToast({
                            title: res.msg,
                            icon: "none",
                        });
                    }
                })
                .finally(() => {
                    uni.removeStorageSync("selectEditAddr");
                    uni.hideLoading();
                });
        },
        //根据code获取字典 /token/common/getDictOptions
        getDict(code) {
            return uni.$u.http.get("/token/common/getDictOptions?type=" + code);
        },
        //获取一键上报的选项 code user_report_options
        getReportOptions() {
            this.getDict("user_report_options").then((res) => {
                if (res.code === 200) {
                    this.actionList = res.data.map((item) => ({
                        text: item.dictLabel,
                        value: item.dictValue,
                    }));
                }
            });
        },
        //一键上报相关操作
        handleReportSelect(index) {
            this.showReportSheet = false;
            let reason = this.actionList[index].value;
            if (this.actionList[index]?.isCancel) {
                this.submitCancel(reason);
            } else {
                this.submitReport(reason);
            }
        },
        submitReport(reason) {
            const params = {
                orderId: this.order.orderId,
                reason: reason,
            };
            uni.$u.http.post("/token/order/userReport", params).then((res) => {
                if (res.code === 200) {
                    uni.showToast({
                        title: "一键上报已上报给管理员",
                        icon: "none",
                    });
                    this.$emit("success");
                    this.successFeedback();
                }
            });
        },

        //订单取消字典 order_cancel_reason_user
        getCancelReason() {
            this.getDict("order_cancel_reason_user").then((res) => {
                if (res.code === 200) {
                    this.actionList = res.data.map((item) => ({
                        text: item.dictLabel,
                        value: item.dictValue,
                        isCancel: true,
                    }));
                }
            });
        },

        // 订单操作方法
        handleAction({ type, order }) {
            console.log("handleAction", type, order);
            this.order = order;
            switch (type) {
                case "submit":
                    uni.navigateTo({
                        url: "/pages-home/pages/book-order",
                    });
                    uni.setStorageSync("orderId", this.order.orderId);
                    break;
                case "editAddress":
                    uni.navigateTo({
                        url: `/pages-mine/pages/address/list?id=${this.order.addressId}&isSelect=1&editAddress=1`,
                    });
                    break;
                case "cancel":
                    uni.showModal({
                        title: "提示",
                        content: "确定取消订单吗？",
                        success: (res) => {
                            if (res.confirm) {
                                this.showReportSheet = true;
                                this.getCancelReason();
                            }
                        },
                    });
                    break;
                case "report":
                    this.showReportSheet = true;
                    this.getReportOptions();
                    break;
                case "remindAudit":
                    this.submitRemindAudit();
                    break;
                case "complaint":
                    uni.navigateTo({
                        url: `/pages-mine/pages/complaint?orderId=${this.order.orderId}`,
                    });
                    break;
                case "compensation":
                    this.$emit("compensation", this.order);
                    break;
                case "review":
                    this.$refs.reviewDialog.openPopup();
                    break;
                case "reviewImage":
                    uni.navigateTo({
                        url: `/pages-mine/pages/review-image?orderId=${this.order.orderId}`,
                    });
                    break;
            }
        },

        submitCancel(reason) {
            const params = {
                orderId: this.order.orderId,
                reason: reason,
            };
            uni.$u.http.post("/token/order/userCancel", params).then((res) => {
                if (res.code === 200) {
                    uni.showToast({
                        title: "您的订单已取消",
                        icon: "none",
                    });
                    this.$emit("success");
                    this.successFeedback();
                }
            });
        },

        //提醒审核
        submitRemindAudit() {
            uni.$u.http
                .post("/api/token/order/urgeOrder", {
                    orderId: this.order.orderId,
                })
                .then((res) => {
                    if (res.code === 200) {
                        uni.showToast({
                            title: "已提醒给管理员进行加急审核",
                            icon: "none",
                        });
                    } else {
                        uni.showToast({
                            title: res.msg,
                            icon: "none",
                        });
                    }
                });
        },
    },
};
