/**
 * 说明：
 *   用户管理相关 WEB API 都在此文件中定义。
 */

import { AxiosRequestData } from '@/data/axios_request'
import cache from '@/utils/cache'

/**
 * 根据企业税号分页查询该企业下用户信息
 * @param {String} entUserMobile 企业用户的手机号，选填
 * @param {Number} pageNum 当前页码，默认为1
 * @param {Number} pageSize 每页数量，默认为10
 */
function getEntUserRoleInfo(entUserMobile = '', pageNum = 1, pageSize = 10) {
    const url = '/auth/ent/user/findEntUserRoleInfo'
    let token = cache.getToken()
    let mobile = JSON.parse(cache.getSession('user')).account
    let entTaxId = JSON.parse(cache.getSession('company')).entTaxId
    let args = {
        mobile: mobile,
        entTaxId: entTaxId,
        pageNum: pageNum,
        pageSize: pageSize
    }
    if (entUserMobile != '') {
        args.entUserMobile = entUserMobile
    }
    console.log('根据企业税号分页查询该企业下用户信息 参数：')
    console.log(args)
    return new Promise(function (resolve, reject) {
        AxiosRequestData(url, token, args, false, 2)
            .then(val => {
                console.log('根据企业税号分页查询该企业下用户信息 结果：')
                console.log(val)
                if (val.code === 0) {
                    resolve(val)
                } else {
                    reject(val.msg)
                }
            })
    })
}

/**
 * 删除企业用户
 * @param {String} userMobile 要删除的用户手机号
 */
function entUserDelete(userMobile) {
    const url = '/auth/ent/user/delete'
    let token = cache.getToken()
    let mobile = JSON.parse(cache.getSession('user')).account
    let args = {
        entUserMobile: userMobile,
        mobile: mobile,
        reqChannel: 4,
    }
    console.log('删除企业用户 参数：')
    console.log(args)
    return new Promise(function (resolve, reject) {
        AxiosRequestData(url, token, args, false, 1)
            .then(val => {
                console.log('删除企业用户 结果：')
                console.log(val)
                if (val.code === 0) {
                    resolve(val)
                } else {
                    reject(val.msg)
                }
            })
    })
}

/**
 * 企业人员保存开票相关业务人员
 * @param {String} entUserMobile 用户手机号
 * @param {String} entUserName 用户名 （用户真实姓名）
 * @param {String} entUserEmail 用户邮箱
 */
function entUserSave({ entUserMobile, entUserName, entUserEmail, entUserRoleVOs }) {
    const url = '/auth/ent/user/save'
    let token = cache.getToken()
    let mobile = JSON.parse(cache.getSession('user')).account
    let args = {
        entUserMobile: entUserMobile,
        entUserName: entUserName,
        entUserRoleVOs: entUserRoleVOs,
        mobile: mobile,
        reqChannel: 4,
    }
    if (entUserEmail != '') {
        args.entUserEmail = entUserEmail
    }
    console.log('企业人员保存开票相关业务人员 参数：')
    console.log(args)
    return new Promise(function (resolve, reject) {
        AxiosRequestData(url, token, args, false, 1)
            .then(v => {
                console.log('企业人员保存开票相关业务人员 结果：')
                console.log(args)
                if (v.code === 0) {
                    resolve(v)
                } else {
                    reject(v.msg)
                }
            })
    })
}

/**
 * 用户企业绑定信息。
 * 根据企业用户手机号获取对应管理相关信息
 * @param {String} userMobile 企业用户的手机号
 * @param {String} entTaxId 企业税号
 * @param {String} entName 企业名称
 * @param {Number} pageNum 当前页码，默认为1
 * @param {Number} pageSize 每页数量，默认为10
 */
function userEntBindInfo({ userMobile, entTaxId = "", entName = "", pageNum = 1, pageSize = 10 }) {
    const url = '/auth/ent/user/findManageInfoByMobile'
    let token = cache.getToken()
    let args = {
        entUserMobile: userMobile,
        pageNum: pageNum,
        pageSize: pageSize
    }
    if (entTaxId != '') {
        args.entTaxId = entTaxId
    }
    if (entName != '') {
        args.entName = entName
    }
    console.log('根据企业用户手机号获取对应管理相关信息 参数：')
    console.log(args)
    return new Promise(function (resolve, reject) {
        AxiosRequestData(url, token, args, false, 2)
            .then(val => {
                console.log('根据企业用户手机号获取对应管理相关信息 结果：')
                console.log(val)
                if (val.code === 0) {
                    resolve(val)
                } else {
                    reject(val.msg)
                }
            })
    })
}

/**
 * 获取企业用户角色
 */
function getEntCategoryRoles() {
    const url = '/auth/ent/role/findCategoryRoles'
    let token = cache.getToken()
    let args = {
    }
    return new Promise(function (resolve, reject) {
        AxiosRequestData(url, token, args, false, 2)
            .then(val => {
                console.log('获取企业用户角色 结果：')
                console.log(val)
                if (val.code === 0) {
                    resolve(val)
                } else {
                    reject(val.msg)
                }
            })
    })
}

/**
 * 获取用户个人信息
 * @param {String} mobile 用户手机号
 */
function getUserPersonalInfo(mobile) {
    const url = '/auth/comm/user/personalInfo/find'
    let token = cache.getToken()
    let args = {
        mobile: mobile
    }
    console.log('获取用户个人信息 参数：');
    console.log(args);
    return new Promise(function (resolve, reject) {
        AxiosRequestData(url, token, args, false, 2)
            .then(val => {
                console.log('获取用户个人信息 结果：')
                console.log(val)
                if (val.code === 0) {
                    resolve(val)
                } else {
                    reject(val.msg)
                }
            })
    })
}

/**
 * 保存或修改用户个人信息
 * @param {String} mobile 用户手机号
 * @param {String} name 用户名，非昵称
 * @param {String} sex 性别 M:男  F:女
 * @param {String} address 地址
 * @param {String} personalProfile 个人简介
 */
function saveUserPersonalInfo({ mobile, name, sex, address, personalProfile }) {
    const url = '/auth/comm/user/personalInfo/save'
    let token = cache.getToken()
    let args = {
        mobile: mobile,
        name: name,
        sex: sex,
        address: address,
        personalProfile: personalProfile,
        reqChannel: 4,
    }
    console.log('保存或修改用户个人信息 参数：');
    console.log(args);
    return new Promise(function (resolve, reject) {
        AxiosRequestData(url, token, args, false, 1)
            .then(val => {
                console.log('保存或修改用户个人信息 结果：')
                console.log(val)
                if (val.code === 0) {
                    resolve(val)
                } else {
                    reject(val.msg)
                }
            })
    })
}

/**
 * 忘记/找回密码
 * @param {String} mobile 用户手机号
 * @param {String} password 密码
 * @param {String} confirmPassword 确认密码
 * @param {String} smsCode 短信验证码
 */
function forgotUserPassword({ mobile, password, confirmPassword, smsCode }) {
    const url = '/auth/comm/user/forgotpwd'
    let token = cache.getToken()
    let args = {
        mobile: mobile,
        password: password,
        confirmPassword: confirmPassword,
        smsCode: smsCode,
        reqChannel: 4,
    }
    console.log('忘记/找回密码 参数：');
    console.log(args);
    return new Promise(function (resolve, reject) {
        AxiosRequestData(url, token, args, false, 1)
            .then(val => {
                console.log('忘记/找回密码 结果：')
                console.log(val)
                if (val.code === 0) {
                    resolve(val)
                } else {
                    reject(val.msg)
                }
            })
    })
}

/**
 * 用户修改手机号
 * @param {String} oriMobile 原来的手机号
 * @param {String} mobile 修改之后手机号
 * @param {String} password 密码
 * @param {String} smsCode 短信验证码
 */
function modifyUserMobile({ oriMobile, mobile, password, smsCode }) {
    const url = '/auth/comm/user/modifyMobile'
    let token = cache.getToken()
    let args = {
        oriMobile: oriMobile,
        mobile: mobile,
        password: password,
        smsCode: smsCode,
        reqChannel: 4,
    }
    console.log('用户修改手机号 参数：');
    console.log(args);
    return new Promise(function (resolve, reject) {
        AxiosRequestData(url, token, args, false, 1)
            .then(val => {
                console.log('用户修改手机号 结果：')
                console.log(val)
                if (val.code === 0) {
                    resolve(val)
                } else {
                    reject(val.msg)
                }
            })
    })
}

/**
 * 分页查询用户消息
 * @param {Boolean} hasRead 消息是否已读
 * @param {String} pageNum 当前页码  默认为1
 * @param {Number} pageSize 每页数量  默认为10
 */
function getUserMessages({ hasRead = '', pageNum = 1, pageSize = 10 }) {
    const url = '/auth/user/msg/findUserMsg'
    let token = cache.getToken()
    let mobile = JSON.parse(cache.getSession('user')).account
    let args = {
        mobile: mobile,
        pageNum: pageNum,
        pageSize: pageSize
    }
    if (typeof hasRead === 'boolean') {
        args.hasRead = hasRead;
    }
    console.log('分页查询用户消息 参数：');
    console.log(args);
    return new Promise(function (resolve, reject) {
        AxiosRequestData(url, token, args, false, 2)
            .then(val => {
                console.log('分页查询用户消息 结果：')
                console.log(val)
                if (val.code === 0) {
                    resolve(val)
                } else {
                    reject(val.msg)
                }
            })
    })
}

/**
 * 获取消息详情读取消息
 * @param {String} msgId 消息id
 */
function getUserMessageDetail(msgId) {
    const url = '/auth/user/msg/findMsgDetail'
    let token = cache.getToken()
    let mobile = JSON.parse(cache.getSession('user')).account
    let args = {
        msgId: msgId,
        mobile: mobile,
        reqChannel: 4
    }
    console.log('获取消息详情读取消息 参数：');
    console.log(args);
    return new Promise(function (resolve, reject) {
        AxiosRequestData(url, token, args, false, 2)
            .then(val => {
                console.log('获取消息详情读取消息 结果：')
                console.log(val)
                if (val.code === 0) {
                    resolve(val)
                } else {
                    reject(val.msg)
                }
            })
    })
}

/**
 * 分页查询系统全局公告信息
 * @param {Boolean} hasRead 消息是否已读
 * @param {String} pageNum 当前页码  默认为1
 * @param {Number} pageSize 每页数量  默认为10
 */
 function findGlobalNotice({ hasRead = '', pageNum = 1, pageSize = 10 }) {
    const url = '/auth/user/msg/findGlobalNotice'
    let token = cache.getToken()
    let mobile = JSON.parse(cache.getSession('user')).account
    let args = {
        mobile: mobile,
        pageNum: pageNum,
        pageSize: pageSize
    }
    if (typeof hasRead === 'boolean') {
        args.hasRead = hasRead;
    }
    console.log('分页查询系统全局公告信息 参数：');
    console.log(args);
    return new Promise(function (resolve, reject) {
        AxiosRequestData(url, token, args, false, 2)
            .then(val => {
                console.log('分页查询系统全局公告信息 结果：')
                console.log(val)
                if (val.code === 0) {
                    resolve(val)
                } else {
                    reject(val.msg)
                }
            })
    })
}

const USER_MANAGEMENT = {
    getEntUserRoleInfo,
    entUserDelete,
    entUserSave,
    userEntBindInfo,
    getEntCategoryRoles,
    getUserPersonalInfo,
    saveUserPersonalInfo,
    forgotUserPassword,
    modifyUserMobile,
    getUserMessages,
    getUserMessageDetail,
    findGlobalNotice
}

export default USER_MANAGEMENT