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

export default {
    /**
     * 安装分类
     */
    getInstallCate() {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "service.cate",
            }).then((response) => {
                if (response.code == 1) {
                    let list = [];
                    response.data.forEach((item) => {
                        let child = [];
                        item.child.forEach((v) => {
                            child.push({
                                id: v.id,
                                name: v.name,
                                cover: v.more,
                                page: "",
                            });
                        });
                        list.push({
                            id: item.id,
                            name: item.name,
                            cover: item.more,
                            child: child,
                            page: "",
                        });
                    });
                    return resolve(list);
                }
                return reject(response.msg);
            });
        });
    },
    /**
     * 售后分类
     */
    getAfterMarketCate() {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "service.aftermarket",
            }).then((response) => {
                if (response.code == 1) {
                    let list = [];
                    response.data.forEach((item) => {
                        list.push({
                            id: item.id,
                            name: item.name,
                            cover: item.more,
                            page: item.name == "货物配送" ? "/pages/service/other/distribution" : "/pages/demand/demand",
                        });
                    });
                    return resolve(list);
                }
                return reject(response.msg);
            });
        });
    },
    /**
     * 保险价格
     */
    getInsurancePrice() {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "service.insurance",
            }).then((response) => {
                if (response.code == 1) {
                    let money = parseFloat(response.data.money);
                    resolve(money > 0 ? money : 0)
                }
            }).catch(e => { });
        });
    },
    /**
     * 服务详情
     */
    getServiceDetail(id) {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "service.detail",
                data: {
                    id: id,
                },
            }).then((response) => {
                if (response.code == 1) {
                    return resolve(response.data);
                }
                return reject(response.msg);
            }).catch(e => { });
        })
    },
    /**
     * 工程提交
     */
    projectApply(data) {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "service.project",
                data: data
            }).then(response => {
                if (response.code == 1) {
                    return resolve(response.msg)
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    /**
     * 企业提交
     */
    businessApply(data) {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "service.business",
                data: data
            }).then(response => {
                if (response.code == 1) {
                    return resolve(response.msg)
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    /**
     * 联保提交
     */
    afterMarketApply(data) {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "service.afterMarket",
                data: data
            }).then(response => {
                if (response.code == 1) {
                    return resolve(response.msg)
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    /**
     * 创建需求订单
     */
    createDemandOrder(data) {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "order.create.t2",
                data: data,
            }).then(response => {
                if (response.code == 1) {
                    return resolve(response);
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    /**
     * 获取货运配置
     */
    distributionConfig() {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "service.distribution.config",
            }).then(response => {
                if (response.code == 1) {
                    return resolve({
                        unitPrice: response.data.money,
                        minDistance: response.data.min,
                    });
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    distributionCar() {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "service.distribution.carType",
            }).then(response => {
                if (response.code == 1) {
                    let list = [];
                    response.data.forEach(item => {
                        list.push({
                            id: item.id,
                            name: item.title
                        });
                    });
                    return resolve(list);
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    /**
     * 创建货运订单
     */
    createDistributionOrder(data) {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "order.create.t3",
                data: data,
            }).then(response => {
                if (response.code == 1) {
                    return resolve(response.data);
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    /**
     * 支付需求订单
     */
    payDemandOrder(id) {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "order.pay.t2",
                data: {
                    id: id
                }
            }).then(response => {
                if (response.code == 1) {
                    uni.requestPayment({
                        ...response.data,
                        provider: "wxpay",
                        success() {
                            resolve();
                        },
                        fail() {
                            reject();
                        }
                    });
                }
            }).catch(e => {
                throw e
            });
        });
    },
    /**
     * 支付货运订单
     */
    payDistributionOrder(id) {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "order.pay.t3",
                data: {
                    id: id,
                }
            }).then(response => {
                if (response.code == 1) {
                    uni.requestPayment({
                        ...response.data,
                        provider: "wxpay",
                        success() {
                            resolve();
                        },
                        fail() {
                            reject();
                        }
                    });
                }
            }).catch(e => { });
        });
    }
}
