const _Http = getApp().globalData.http;
const getHeight = require("../../utils/GetRheRemainingHeight"),
    currency = require("../../utils/currency");

Page({
    data: {
        tabShow: true,
        auth: {}, //权限
        listHeight: 0,
        butText: "", //按钮类型
        content: {
            nocache: true,
            "pageNumber": 1,
            "pageSize": 20,
            "pageTotal": 1,
            "where": {
                "condition": ""
            }
        }
    },
    onLoad(options) {
        let auth = getApp().globalData.queryPer.query(wx.getStorageSync('userauth'), ['通用'], ['个人中心'])[0].apps.find(v => v.name == 'teamManagement').meta.auth,
            tabList = [],
            authList = auth.map(v => {
                if (v.option == 'account') {
                    tabList[0] = {
                        title: "账号列表",
                        butText: "新建账号",
                        classname: "sale.team.team",
                        method: "query_teamList",
                        url: '/pages/teams/addUsers'
                    }
                } else if (v.option == 'role') {
                    tabList[1] = {
                        title: "角色列表",
                        butText: "新建角色",
                        classname: "sale.role.role",
                        method: "query_roleList",
                        url: '/pages/teams/addRole'
                    }
                } else if (v.option == 'paidOrder') {
                    tabList[2] = {
                        title: "付费订单",
                        butText: wx.getStorageSync('siteP').sys_payswitch ? "新建付费订单" : "",
                        classname: "system.payorder.payorder",
                        method: "list",
                        url: "/pages/teams/addOrder"
                    }
                }
                return v.option
            });
        tabList = tabList.filter(v => v);
        this.setData({
            authList,
            tabList,
            active: tabList[options.title ? tabList.findIndex(v => v.title == options.title) || 0 : 0]
        })
        console.log(auth)
        console.log(this.data.active)
        /*   if (options.title) setTimeout(() => {
              this.setData({
                  active: this.data.active
              })
          }, 300) */
        this.getList();
    },
    /* tab切换 */
    tabChange(e) {
        this.setData({
            active: this.data.tabList[e.detail.index],
            list: [],
        })
        this.getList(true);
    },
    /* 获取列表 */
    getList(init = false) {
        if (init.detail != undefined) init = init.detail;
        if (init) this.setData({
            ['content.pageNumber']: 1
        })
        if (this.data.content.pageNumber > this.data.content.pageTotal) return;
        let active = this.data.active;
        _Http.basic({
            classname: active.classname || '',
            method: active.method || '',
            id: active.id || '',
            content: this.data.content
        }).then(res => {
            this.selectComponent('#ListBox').RefreshToComplete();
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            })
            if (active.butText == '新建账号') {
                for (let i = 0; i < res.data.length; i++) {
                    res.data[i].attinfos = res.data[i].attinfos.filter(v => v.usetype == 'headportrait');
                }
            } else if (active.butText == '新建付费订单') {
                res.data = res.data.map(v => {
                    switch (v.ispaid) {
                        case 0:
                            v.color = "#FF3B30";
                            v.status = '未付费';
                            break;
                        case 1:
                            v.color = '#52C41A';
                            v.status = '已付费';
                            break;
                        case 2:
                            v.color = '#ddd';
                            v.status = "新建";
                            break;
                        case 3:
                            v.color = '#ddd';
                            v.status = "已取消";
                            break;
                        case 4:
                            v.color = '#ddd';
                            v.status = "已退费";
                            break;
                    };
                    v.amount = currency(v.amount, {
                        symbol: "￥",
                        precision: 2
                    }).format();
                    return v
                })
            }
            this.setData({
                list: (res.pageNumber == 1) ? res.data : this.data.list.concat(res.data),
                ['content.pageNumber']: res.pageNumber + 1,
                ['content.pageTotal']: res.pageTotal
            })
        })
    },
    /* 新建账号/角色 */
    newItem() {
        let url = this.data.active.url;
        console.log(url)
        if (!url) return;
        if (url == '/pages/teams/addOrder') {
            _Http.basic({
                "classname": "system.payorder.payorder",
                "method": "createOrder",
                "content": {},
            }).then(res => {
                console.log("新建订单", res)
                if (res.msg != '成功') return wx.showModal({
                    title: '提示',
                    content: res.msg,
                    showCancel: false,
                })
                wx.navigateTo({
                    url: url + '?sys_payorderid=' + res.data.sys_payorderid
                })
            })
        } else {
            wx.navigateTo({
                url
            })
        }
    },
    /* 修改角色 */
    changeRole(e) {
        let {
            item
        } = e.currentTarget.dataset;
        wx.navigateTo({
            url: `./addRole?item=${JSON.stringify(item)}&update=${this.data.authList.includes("角色修改")}&userDelete=${this.data.authList.includes("角色删除")}`
        })
    },
    /* 修改账号 */
    changeUser(e) {
        let {
            item
        } = e.currentTarget.dataset;
        delete(item.attinfos);
        wx.navigateTo({
            url: `./addUsers?item=${JSON.stringify(item)}&update=${this.data.authList.includes("账号修改")}`
        })
    },
    onReady() {
        getHeight.getHeight('.tabs', this).then(res => {
            this.setData({
                listHeight: res
            })
        });
    },
})