import {
    ApiModel
} from "../../utils/api";
const _Http = new ApiModel;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        requestList: [], //请求列表
        showBtn: -1, //选中下标
        scrolltolowerThrottle: true, //下拉触底截流
        pageNumber: 1, //请求分页
        pageTotal: 1, //总页数
        tabsList: ['建立新合作', '合作请求'], //tabs
        tabsIndex: 0, //tabs 选中下标
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        /* 判断主副账号 */
        if (wx.getStorageSync('userData').fisadministrator == 0) {
            this.setData({
                tabsList: ['建立新合作']
            })
        }
        this.getList();
    },
    /* tabs切换页面 */
    setIndex({
        detail
    }) {
        this.setData({
            tabsIndex: detail
        })
    },
    /* 下拉触底 */
    scrolltolower() {
        if (!this.data.scrolltolowerThrottle) return;
        if (this.data.pageTotal < this.data.pageNumber) return;
        this.setData({
            scrolltolowerThrottle: false
        })
        this.getList();
    },
    /* 获得列表 */
    getList() {
        _Http.basic({
            "accesstoken": wx.getStorageSync('userData').token,
            "classname": "customer.tagents.tagents",
            "method": "query_cooperation",
            "content": {
                "getdatafromdbanyway": true,
                "pageNumber": this.data.pageNumber,
                "pageSize": 20,
                "where": {
                    "condition": "",
                    "fstatus": "申请"
                }
            }
        }).then(res => {
            if (res.msg != "成功") return wx.showToast({
                title: res.data,
                icon: 'none'
            })
            let requestList = [];
            if (res.pageNumber == 1) {
                requestList = res.data
            } else {
                requestList = this.data.requestList.concat(res.data)
            }
            this.setData({
                requestList,
                scrolltolowerThrottle: true,
                pageTotal: res.pageTotal,
                pageNumber: this.data.pageNumber + 1
            })
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
    /* 选择 */
    showBtnIndex(e) {
        let {
            index
        } = e.currentTarget.dataset;
        if (index == this.data.showBtn) index = -1;
        this.setData({
            showBtn: index,
        })
    },
    /* 同意 */
    ratify() {
        const i = this.data.showBtn,
            that = this;
        wx.showModal({
            title: '提示',
            content: "是否同意“" + that.data.requestList[i].fbrand + '”的合作申请',
            success: function (res) {
                if (res.confirm) {
                    _Http.basic({
                        "accesstoken": wx.getStorageSync('userData').token,
                        "classname": "customer.tagents.tagents",
                        "method": "cooperation",
                        "content": {
                            "tcooperationagentsid": that.data.requestList[i].tcooperationagentsid
                        }
                    }).then(res => {
                        console.log(res)
                        if (res.msg != '成功') return wx.showToast({
                            title: res.data,
                            icon: "none"
                        });
                        wx.showToast({
                            title: '合作成功!',
                        })
                        let requestList = that.data.requestList;
                        requestList.splice(i, 1);
                        that.setData({
                            requestList,
                            showBtn: -1
                        })
                    })
                }
            }
        })
    },
    /* 拒绝 */
    refuse(e) {
        const {
            index
        } = e.currentTarget.dataset,
            that = this;
        wx.showModal({
            title: "提示",
            content: '是否确定拒绝“' + this.data.requestList[index].fbrand + '”的合作请求',
            success(res) {
                if (res.confirm) {
                    _Http.basic({
                        "accesstoken": wx.getStorageSync('userData').token,
                        "classname": "customer.tagents.tagents",
                        "method": "delete_cooperation",
                        "content": {
                            "tcooperationagentsid": that.data.requestList[index].tcooperationagentsid
                        }
                    }).then(res => {
                        console.log(res)
                        if (res.msg != '成功') return wx.showToast({
                            title: res.data,
                            icon: "none"
                        });
                        let requestList = that.data.requestList;
                        requestList.splice(index, 1)
                        that.setData({
                            requestList
                        })
                    })
                }
            }
        })
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})