import {
    ApiModel
} from "../../utils/api";
const _Http = new ApiModel;
Page({
    /**
     * 页面的初始数据
     */
    data: {
        checked: true, //开关
        partnerDetails: {}, //合作详情
        seIndex: null, //合作方式
        throttle: true, //截流
        fisadministrator: null,
        isCancel: false, //是否取消合作
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const data = JSON.parse(options.data)
        this.setData({
            partnerDetails: data,
            seIndex: data.ftype,
            fisadministrator: (wx.getStorageSync('userData').fisadministrator == 1) ? false : true,
        })
    },
    /* 开关 */
    onChange({
        detail
    }) {
        const that = this;
        if (this.data.fisadministrator) return wx.showToast({
            title: '当前账号无权限操作',
            icon: "none"
        });
        if (this.data.checked) {
            wx.showModal({
                title: "提示",
                content: '是否确定取消与“' + this.data.partnerDetails.fbrand + '”的合作关系',
                success: function (res) {
                    if (res.confirm) {
                        that.setData({
                            checked: detail,
                            isCancel: true
                        });
                    }
                }
            })
        } else {
            that.setData({
                checked: detail,
                isCancel: false
            });
        }
    },
    /* 预览合作商logo */
    previewImg() {
        const urls = [this.data.partnerDetails.attinfos[0].fobsurl]
        wx.previewImage({
            current: 1, // 当前显示图片的http链接
            urls: urls
        })
    },
    /* 提交 */
    submit() {
        /* 截流 */
        if (!this.data.throttle) return;
        this.setData({
            throttle: false
        });
        /* 取消合作 */
        if (this.data.isCancel) return _Http.basic({
            "accesstoken": wx.getStorageSync('userData').token,
            "classname": "customer.tagents.tagents",
            "method": "delete_cooperation",
            "content": {
                "tcooperationagentsid": this.data.partnerDetails.tcooperationagentsid
            }
        }).then(res => {
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "error"
            })
            setTimeout(() => {
                wx.navigateBack({
                    delta: 1,
                })
            }, 500);
        })
        /* 发送修改请求 */
        if (this.data.seIndex != this.data.partnerDetails.ftype) {
            _Http.basic({
                "accesstoken": wx.getStorageSync('userData').token,
                "classname": "customer.tagents.tagents",
                "method": "update_cooperation",
                "content": {
                    "tcooperationagentsid": this.data.partnerDetails.tcooperationagentsid,
                    "ftype": this.data.seIndex
                }
            }).then(res => {
                if (res.msg != '成功') return wx.showToast({
                    title: res.data,
                    icon: "none"
                });
                wx.showToast({
                    title: '修改成功'
                });
                setTimeout(() => {
                    wx.navigateBack({
                        delta: 1,
                    })
                }, 500);
            });
        } else {
            wx.showToast({
                title: '保存成功'
            });
            setTimeout(() => {
                wx.navigateBack({
                    delta: 1,
                })
            }, 500);
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})