import Vue from "vue"
let prototype = Vue.prototype;

export default {
    stateText(listType, orderType, state) {
        if (listType == 't1') {
            switch (state) {
                case 0: return ['待支付', '为保证师傅按时上门，建议尽快选择师傅并完成支付'];
                case 1: return ['待师傅接单'];
                case 2: return ['服务中'];
                case 4: return ['待支付尾款'];
                case 5: return ['服务已完成，待确认'];
                case 6: return ['订单已完成'];
                case 7: return ['退款申请审核中'];
                case 8: return ['已退款'];
                case 9: return ['退款未通过'];
            }
        } else if (listType == 't2') {
            switch (state) {
                case 0:
                    if (orderType == 2) {
                        return ['待师傅接单'];
                    }
                    return ['待选择师傅'];
                case 1:
                    if (orderType == 2) {
                        return ['师傅已接单，待支付'];
                    }
                    return ['已选择师傅，待支付'];
                case 2: return ['服务中'];
                case 4: return ['待支付尾款'];
                case 5: return ['服务已完成，待确认'];
                case 6: return ['订单已完成'];
                case 7: return ['退款申请审核中'];
                case 8: return ['已退款'];
                case 9: return ['退款未通过'];
            }
        } else if (listType == 't3') {
            switch (state) {
                case 0: return ['待支付'];
                case 1: return ['待接单'];
                case 2: return ['进行中'];
                case 3: return ['已完成'];
            }
        }

        return ['未知', '#000000'];
    },
    /**
     * 下单
     */
    createOrder(order) {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "order.create.t1",
                data: order,
            }).then((response) => {
                if (response.code == 1) {
                    return resolve(response.data);
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    /**
     * 支付订单
     */
    payOrder(id) {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "order.pay.t1",
                data: {
                    id: id
                }
            }).then(response => {
                if (response.code == 1) {
                    uni.requestPayment({
                        ...response.data,
                        provider: "wxpay",
                        success() {
                            resolve();
                        },
                        fail() {
                            reject();
                        },
                    });
                }
            }).catch(e => {
                throw e
            });
        });
    },
    /**
     * 订单列表
     */
    orderList(options) {
        return new Promise((resolve, reject) => {
            prototype.$request(options.request).then(response => {
                if (response.code == 1) {
                    let list = [];
                    response.data.forEach((item) => {
                        list.push({
                            listType: options.listType,
                            id: item.id,
                            orderId: item.order,
                            cate: "待返数据",
                            content: item.desc,
                            createTime: item.time,
                            serviceTime: item.times,
                            orderType: item.types,
                            state: item.status,
                            worker: item.shifu,
                            workerList: [],
                        });
                    });
                    return resolve(list);
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    /**
     * 订单详情
     */
    orderDetail(options) {
        return new Promise((resolve, reject) => {
            prototype.$request(options.request).then(response => {
                if (response.code == 1) {
                    return resolve({
                        id: response.data.id,
                        address: response.data.address,
                        orderId: response.data.order,
                        cate: "待返数据",
                        content: response.data.desc,
                        createTime: response.data.time,
                        serviceTime: response.data.times,
                        orderType: response.data.types,
                        state: response.data.status,
                        images: [],
                        worker: [],
                        workerList: [],
                    });
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
}
