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

Page({

    /**
     * 页面的初始数据
     */
    data: {
        height: 0,
        roleid: -1, //角色id
        appList: [], //应用列表
        fromList: [{
            label: "角色名称",
            error: false,
            errMsg: "",
            type: "text",
            value: "",
            placeholder: "请填写",
            valueName: "rolename", //绑定的字段名称
            required: true, //必填
        }, {
            label: "角色描述",
            error: false,
            errMsg: "",
            type: "text",
            value: "",
            placeholder: "请填写",
            valueName: "remarks", //绑定的字段名称
            required: true, //必填
        }],
        disabled: true, //按钮禁用
        loading: false, //按钮加载
    },
    onLoad(options) {
        console.log(options)
        if (options.item) {
            let item = JSON.parse(options.item)
            this.setData({
                roleid: item.roleid,
                ['fromList[0].value']: item.rolename,
                ['fromList[1].value']: item.remarks,
                disabled: false,
            });
        };
        this.query_appselect();
        /*    */
    },
    /* 查询应用列表 */
    query_appselect() {
        _Http.basic({
            "classname": "sale.role.role",
            "method": "query_appselect",
            "content": {
                "roleid": this.data.roleid
            }
        }).then(res => {
            console.log("应用列表", res)
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            })
            this.setData({
                roleid: this.data.roleid == -1 ? 0 : this.data.roleid,
                appList: res.data
            })
            /*  this.setData({
                 roleid: this.data.roleid == -1 ? 0 : this.data.roleid,
                 appList: wx.getStorageSync('userauth')
             }) */
        })
    },
    /* 提交数据 */
    submitRole() {
        if (this.data.disabled || this.data.loading) return;
        this.setData({
            loading: true
        })
        let {
            isReturn,
            returnData
        } = this.selectComponent("#form").getData();

        _Http.basic({
            "classname": "sale.role.role",
            "method": "insertormodify_role",
            "content": {
                "roleid": this.data.roleid,
                ...returnData
            }
        }).then(res => {
            console.log("新建角色", res)
            this.setData({
                loading: false
            })
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            })
            this.setData({
                disabled: true
            })
            let systemapps = this.selectComponent("#myCateg").backData();

            if (systemapps.length > 0) _Http.basic({
                "classname": "sale.role.role",
                "method": "add_appauth",
                "content": {
                    "roleid": res.data.roleid,
                    systemapps
                }
            }).then(res => {
                console.log("绑定授权", res)
            })
            this.navBack();
        })
    },
    deleteRole() {
        const that = this;
        wx.showModal({
            title: "提示",
            content: "是否确认删除该角色?",
            success: (s) => {
                if (s.confirm) {
                    console.log('删除')
                    _Http.basic({
                        "classname": "sale.role.role",
                        "method": "delete_role",
                        "content": {
                            "roleid": this.data.roleid
                        }
                    }).then(res => {
                        if (res.msg != '成功') return wx.showToast({
                            title: res.msg,
                        });
                        that.navBack('删除成功');
                    })
                }
            }
        })
    },
    navBack(tips = '保存成功') {
        let pages = getCurrentPages(),
            prevPage = pages[pages.length - 2];
        prevPage.getList(true);
        setTimeout(() => {
            wx.navigateBack({
                delta: 0
            })
            wx.showToast({
                title: tips,
            })
        }, 300)
    },
    onReady() {
        getHeight.getHeight('.module-navigation', this).then(res => {
            this.setData({
                height: res - 130
            })
        })
    },

    /* 表单是否完成 */
    formCompletedOrNot({
        detail
    }) {
        this.setData({
            disabled: !detail
        })
    },
})