import APP from '@/services';
import { setAllEnum } from '@/services/bus/allEnum';
import { getLongTypeLoginID } from '@/services/bus/login';
import { localStorageUtil } from '@/utils/storage';
import moment from 'moment';
import { commonSearch_go } from '../index';
import * as type from './interface';

/**
 * 获取服务器时间
 */
export function getServerTime(): Promise<string> {
    return commonSearch_go('/Common/GetServerTime', {})
        .then((res) => {
            const time = moment(res)
                .utc()
                .format('YYYY-MM-DD HH:mm:ss');
            APP.set('systemDate', time);
            return 'ok';
        })
        .catch((err: Error) => {
            throw new Error('获取服务器时间数据错误：' + err);
        });
}

/**
 * 获取所有枚举信息
 * @param autoid autoid传入后则返回这个ID之后的数据；如不传则返回所有
 */
export function getAllEnums(autoid?: number): Promise<string> {
    const param = autoid ? { autoid } : {};
    return commonSearch_go('/Common/GetAllEnums', param)
        .then((res) => {
            console.log('所有枚举信息', res);
            setAllEnum(res)
            return 'ok';
        })
        .catch((err: Error) => {
            throw new Error('查询获取所有枚举信息：' + err);
        });
}

/**
 * 获取数据库错误信息
 * @param rowNumber 如果传入rowNumber，则返回此rowNumber后的数据
 */
export function queryErrorInfos(rowNumber?: string): Promise<string> {
    const rowNum = localStorageUtil.getItem('rowNumber')
    const param = rowNum ? { rowNumber: rowNum } : {};
    return commonSearch_go('/Common/QueryErrorInfos', param)
        .then((res) => {
            if (res && res.length) {
                const rowNumber = res[res.length - 1].rownumber
                if (rowNumber) {
                    localStorageUtil.setItem('rowNumber', rowNumber);
                }
                const list = localStorageUtil.getItem('errorCodeInfos') || []
                localStorageUtil.setItem('errorCodeInfos', [...list, ...res]);
            }
            return 'ok';
        })
        .catch((err: Error) => {
            throw new Error('查询获取所有枚举信息：' + err);
        });
}

/**
 * 通知公告设置已读请求
 * @param loginID 登录账号
 * @param noticeID 通知公告ID
 */
export function queryNoticeReaded(noticeID: number): Promise<string> {
    const param = { noticeID, loginID: Number(getLongTypeLoginID()) };
    return commonSearch_go('/Common/NoticeReaded', param, 'post')
        .then(() => 'ok')
        .catch((err: Error) => {
            throw new Error(`通知公告设置已读请求失败:${err}`);
        });
}

/**
 * 通知公告系统消息查询
 */
export function queryNotice(): Promise<type.QueryNoticeRsp[]> {
    const param = { loginID: Number(getLongTypeLoginID()) };
    return commonSearch_go('/Common/QueryNotice', param).catch((err: Error) => {
        throw new Error(`通知公告系统消息查询失败:${err}`);
    });
}
/**
 * 获取PC交易端菜单
 */
export function GetPCMenus(): Promise<string> {
    const param = {
        loginID: Number(getLongTypeLoginID()),
        clientType: 0, // 终端类型，0：PC 1：Mobile
        // name: getOem(),
    };
    return commonSearch_go('/Common/GetClientMenus', param)
        .then((res) => {
            APP.set('menus', res);
            console.log('交易端菜单', res);
            return 'ok';
        })
        .catch((err: Error) => {
            throw new Error(`获取PC交易端菜单失败:${err}`);
        });
}

/**
 * 查询交易端列表头信息
 * @returns
 */
export function QueryTableDefine(): Promise<type.TableDefineRsp[]> {
    const param = {
        tableType: 2, // 列表类型 - 1：管理端 2：H5终端 3：移动终端
    };
    return commonSearch_go('/Common/QueryTableDefine', param)
        .then((res) => {
            console.log('查询交易端列表头信息', res);
            APP.set('tableHead', res);
            return res;
        })
        .catch((err: Error) => {
            throw new Error(`查询交易端列表头信息:${err}`);
        });
}
