import request from './_utils/request.js'
import apis from './apis.json'
import constant from './_utils/constant.js'
import storage from './_utils/storage.js'
export default {
    //存取草稿(只允许1+5条记录，第1条覆盖保存，后5条循环保存)
    draft: function(formData, index = 0) {
        var lsIndex = constant.orderForm;
        // 指定第1条
        if (index === null) {
            return storage.set(lsIndex, formData, 0);
        }
        //指定第几条缓存
        var i = Number(index);
        if (i > 0) {
            return storage.set(lsIndex, formData, i);
        }
        //确认上次缓存的是第几条
        var lastIndex = storage.get(lsIndex, 'lastIndex');
        var li = lastIndex ? lastIndex : 0;
        if (li < 5) {
            li += 1;
        } else {
            li = 1;
        }
        storage.set(lsIndex, li, 'lastIndex');
        return storage.set(lsIndex, formData, li);

    },
    getDraft: function(index = null) {
        var lsIndex = constant.orderForm;
        index = index || 0;
        return storage.get(lsIndex, index);
    },
    delDraft: function(index = null) {
        var lsIndex = constant.orderForm;
        index = index || 0;
        storage.remove(lsIndex, index);
    },

    //删除缓存
    delDetail: function(oid) {
        storage.remove(constant.orderDetail, oid);
    },

    //取消订单
    cancel: function(oid) {
        //网络请求
        var data = {
            'id': oid
        };
        var url = apis.OrderCancel;
        var header = {
            'content-type': 'application/x-www-form-urlencoded'
        };
        return request({
            'url': url,
            'method': 'POST',
            'data': data,
            "headers": header,
            'params': {}
        }).then((res) => {
            if (!res) {
                return false;
            }
            return res.data;
        })
    },

    // 在线下单
    create: function(formData) {
        //缓存表单
        var lsIndex = constant.orderCreateForm
        storage.set(lsIndex, formData);
        //网络请求
        var data = formData;
        var url = apis.OrderCreate;
        var header = {
            'content-type': 'application/x-www-form-urlencoded'
        };
        return request({
            'url': url,
            'method': 'POST',
            'data': data,
            "headers": header,
            'params': {}
        })
    },

    //查询运单
    search: function(formData = null, page = 0, psize = 0, ls = null) {
        page = page || 1;
        psize = psize || 10;
        //缓存表单
        var lsIndex = ls || constant.orderSearchA
        if (formData) {
            storage.set(lsIndex, formData);
        } else {
            formData = storage.get(lsIndex)
        }

        var data = {
            'search[carno]': formData.carno,
            'search[sn]': formData.ordersn ? formData.ordersn : 0,
            'search[phone]': formData.phone,
            page: page,
            psize: psize, //分页数据大小
        };
        var url = apis.OrderSearch;
        var header = {
            'content-type': 'application/x-www-form-urlencoded'
        };
        return request({
            'url': url,
            'method': 'POST',
            'data': data,
            "headers": header,
            'params': {}
        }).then((res) => {
            if (!res) {
                storage.set(constant.orderList, {});
                return false;
            }
            switch (res.code) {
                default:
                    uni.showToast({
                        title: "没有查询到相关订单!",
                        icon: "fail"
                    });
                    break;
                case 0:
                    var orders = {};
                    var totalCount = 0;
                    storage.set(constant.orderList, orders, 'page-' + page);
                    break;
                case 200:
                    var orders = res.data.orders;
                    var totalCount = res.data.total;
                    storage.set(constant.orderList, totalCount, 'total');
                    storage.set(constant.orderList, orders, 'page-' + page);
                    for (var x in orders) {
                        var order = orders[x];
                        var oid = order.id;
                        storage.set(constant.orderDetail, order, oid);
                    }
                    break;
            }
            return res.data;
        })
    },

    //运单列表，从本地缓存中加载
    list: function(page = 0) {
        //取缓存
        var total = storage.get(constant.orderList, 'total');
        if (total) {
            var orders = storage.get(constant.orderList, 'page-' + page);
            if (orders) {
                return {
                    'total': total,
                    'orders': orders
                }
            }
        }
        return false;
    },

    //运单详情,网络更新
    checkDetail: function(oid) {
        var data = {
            oid: oid
        };
        var url = apis.OrderDetail;
        return request({
            'url': url,
            'method': 'POST',
            'data': data,
            'params': {}
        }).then((res) => {
            if (res.code == 200) {
                var order = res.data;
                storage.set(constant.orderDetail, order, oid);
                return res.data;
            } else {
                uni.showToast({
                    title: "订单信息获取失败!",
                    icon: "fail"
                });
            }
        })
    },

    //运单详情，从缓存中读取
    detail: function(oid) {
        return storage.get(constant.orderDetail, oid);
    },

    //运单验车
    yanche: function(oid, checks, op) {
        var data = {
            oid: oid,
            op: op,
            checks: checks,
        };
        var url = apis.OrderCheck;
        return request({
            'url': url,
            'method': 'POST',
            'data': data,
            'params': {}
        }).then((res) => {
            if (res.code == 200) {
                return res.data;
            } else {
                return false;
            }
        })
    }

}