var util = require('../../utils/util.js'); // 确保你有这样一个文件

var e = getApp();

Page({
    data: {
        text: "",
        type: "",
        wxid: "",
        alias: "",
        uuid: "",
        openid: ""
    },
    onLoad: function(e) {
        wx.showShareMenu({
            withShareTicket: !0,
            menus: [ "shareAppMessage", "shareTimeline" ]
        }), this.setData({
            text: decodeURIComponent(e.text),
            type: decodeURIComponent(e.type) || "",
            wxid: decodeURIComponent(e.wxid) || "",
            alias: decodeURIComponent(e.alias) || ""
        });
    },
    copyText: function() {
        wx.setClipboardData({
            success: function() {
                wx.showToast({
                    title: "复制成功",
                    duration: 1200
                });
            },
            data: this.data.text
        });
    },
    onAdClick: function() {
        e.广告();
    },
    rewriteText: function() {
        if (console.log(wx.getStorageSync("defaultDailyFreeParseNum")), wx.getStorageSync("defaultDailyFreeParseNum") > 0 || wx.getStorageSync("isMember")) {
            var t = this;
            wx.showLoading({
                title: "ai改写中..."
            }), e.apiRequest({
                url: "/myapp/rewrite_text/",
                method: "POST",
                data: {
                    text: this.data.text,
                    uuid: wx.getStorageSync("uuid"),
                    openid: wx.getStorageSync("openid"),
                    type: this.data.type || "",
                    wxid: this.data.wxid || "",
                    alias: this.data.alias || ""
                },
                success: function(e) {
                    wx.hideLoading(), "success" === e.data.status ? (
                      t.setData({
                        text: e.data.rewritten_text
                    }), 
                    wx.showToast({
                        title: "改写完成",
                        icon: "success",
                        duration: 2e3
                    })
                    ) : "次数不足或不是会员" == e.data.error ? wx.showModal({
                        title: "提示",
                        content: "已无体验次数,前往个人中心领取或开通会员",
                        confirmColor: "#00B269",
                        cancelColor: "#858585",
                        success: function(e) {
                            e.confirm ? (console.log("确定"), wx.navigateTo({
                                url: "../vip_recharge/vip_recharge?show=true"
                            })) : e.cancel && console.log("取消");
                        }
                    }) : wx.showToast({
                        title: e.data.error,
                        icon: "none",
                        duration: 2e3
                    });
                },
                fail: function(e) {
                    console.error("改写请求失败", e), wx.hideLoading(), wx.showToast({
                        title: "请求出错",
                        icon: "none",
                        duration: 2e3
                    });
                }
            });
        } else wx.showToast({
            title: "免费次数已用完！",
            icon: "none"
        }), wx.showModal({
            title: "提示",
            content: "已无体验次数,前往个人中心领取或开通会员",
            confirmColor: "#00B269",
            cancelColor: "#858585",
            success: function(e) {
                e.confirm ? (console.log("确定"), wx.navigateTo({
                    url: "../vip_recharge/vip_recharge?show=true"
                })) : e.cancel && console.log("取消");
            }
        });
    },
    saveCopywriting: function() {
        e.apiRequest({
            url: "/myapp/create_copywriting/",
            method: "POST",
            data: {
                text_content: this.data.text,
                uuid: wx.getStorageSync("uuid") || "",
                openid: wx.getStorageSync("openid") || "",
                source: this.data.type || "",
                tag: this.data.wxid || "",
                type: this.data.type || "",
                wxid: this.data.wxid || "",
                alias: this.data.alias || ""
            },
            success: function(e) {
                console.log(e);
            },
            fail: function(e) {
                console.error("请求失败:", e);
            }
        });
    },
    onReady: function() {},
    onShow: function() { 
        e.checkUpdateVersion(), wx.showLoading(), e.getUserInfo().then(function() {
            console.log("获取用户开始"), console.log("获取用户结束");
        }).catch(function(e) {
            console.error("获取用户信息失败:", e);
        }).finally(function() {
            console.log("getUserInfo调用完成");
        });
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {
        return {
            title: "推荐一款免费又超好用的视频文案创作工具，分享给大家一起使用",
            path: "/pages/index/index?uuid=" + wx.getStorageSync("uuid"),
            imageUrl: "/images/share.jpg",
            success: function(e) {
                wx.showToast({
                    title: "分享成功",
                    icon: "success",
                    duration: 2e3
                });
            },
            fail: function(e) {
                wx.showToast({
                    title: "分享失败",
                    icon: "none",
                    duration: 2e3
                });
            }
        };
    },
    onShareTimeline: function() {
        return {
            title: "推荐一款免费又超好用的视频文案创作工具，分享给大家一起使用",
            path: "/pages/index/index?uuid=" + wx.getStorageSync("uuid"),
            imageUrl: "/images/share.jpg",
            success: function(e) {
                wx.showToast({
                    title: "分享成功",
                    icon: "success",
                    duration: 2e3
                });
            },
            fail: function(e) {
                wx.showToast({
                    title: "分享失败",
                    icon: "none",
                    duration: 2e3
                });
            }
        };
    }
});