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

Page({
    data: {
        tabShow: true,
        auth: {}, //权限
        listHeight: 0,
        butText: "", //按钮类型
        content: {
            "pageNumber": 1,
            "pageSize": 20,
            "pageTotal": 1,
            "where": {
                "condition": ""
            }
        }
    },
    onLoad(options) {
        let auth = options.auth ? JSON.parse(options.auth) : getApp().globalData.queryPer.query(wx.getStorageSync('userauth'), ['通用'], ['个人中心'])[0].apps[0].meta.auth,
            authList = auth.map(v => v.optionname);
        let tabShow = false,
            butText = '账号';
        if (authList.includes('查看账号') && authList.includes('查看角色')) {
            tabShow = true;
        } else {
            butText = authList.includes('查看角色') ? '角色' : '账号';
        }
        this.setData({
            tabShow,
            butText,
            authList
        })
        this.getList();
    },
    /* 获取列表 */
    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;
        _Http.basic({
            "id": this.data.butText == '账号' ? "20221107094704" : "20221109091404",
            "content": this.data.content
        }).then(res => {
            this.selectComponent('#ListBox').RefreshToComplete();
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            })
            if (this.data.butText == '账号') {
                for (let i = 0; i < res.data.length; i++) {
                    res.data[i].attinfos = res.data[i].attinfos.filter(v => v.usetype == 'headportrait');
                }
            }
            this.setData({
                list: (res.pageNumber == 1) ? res.data : this.data.list.concat(res.data),
                ['content.pageNumber']: res.pageNumber + 1,
                ['content.pageTotal']: res.pageTotal
            })
        })
    },
    /* 新建账号/角色 */
    newItem() {
        if (this.data.butText == '账号') {
            wx.navigateTo({
                url: '/pages/teams/addUsers'
            })
        } else {
            wx.navigateTo({
                url: '/pages/teams/addRole'
            })
        }
    },
    /* tab切换 */
    tabChange(e) {
        this.setData({
            butText: e.detail.title.slice(0, 2),
            list: [],
        })
        this.getList(true);
    },
    /* 修改角色 */
    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
            })
        });
    },
    onShareAppMessage(res) {}
})