/**
 * @Author: 嗨噜客(三亚)<fm453>
 * @Date: 2023-07-19 16:03:07
 * @FilePath: apis/ctms/sync.js
 * @Description: 
 * @Email:  393213759@qq.com
 * Copyright (c) 2025 by www.hiluker.cn, All Rights Reserved. 
 */
// 对异步函数进行同步化模拟
const Confirm = {
    modalPromise: function(title, content, editable) {
        return new Promise((resolve, reject) => {
            uni.showModal({
                title: title,
                content: content,
                editable: editable,
                success: (res) => {
                    if (res.confirm) {
                        if (editable) {
                            resolve(res.content);
                        } else {
                            resolve(true);
                        }
                    } else if (res.cancel) {
                        resolve(false);
                    }
                },
                fail: (err) => {
                    reject(err);
                }
            });
        });
    },

    doSync: async function(title, content, editable) {
        try {
            const res = await this.modalPromise(title, content, editable);
            // console.log('对话框返回', res)
            return res;
        } catch (error) {
            // console.error('Error:', error);
            // 处理错误或用户取消操作...
        }
    }
}

const Actions = {
    actionPromise: function(title, list) {
        return new Promise((resolve, reject) => {
            uni.showActionSheet({
                title: title,
                itemList: list,
                success: function(res) {
                    // console.log('授权点击', res)
                    var index = res.tapIndex;
                    var item = list[index];
                    resolve({
                        'index': index,
                        'item': item
                    })
                },
                fail: function(res) {
                    // console.log(res.errMsg);
                    resolve(false)
                }
            });
        });
    },

    doSync: async function(title, list) {
        try {
            const res = await this.actionPromise(title, list);
            // console.log('actionsheet点击返回', res)
            return res;
        } catch (error) {
            // console.error('Error:', error);
            // 处理错误或用户取消操作...
        }
    }
}

export default {
    Confirm,
    Actions
}