/**
 * @Author: 嗨噜客(三亚)<fm453>
 * @Date: 2023-07-19 16:03:07
 * @FilePath: apis/ctms/orderpre.js
 * @Description: 
 * @Email:  393213759@qq.com
 * Copyright (c) 2025 by www.hiluker.cn, All Rights Reserved. 
 */
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.orderBookForm;
        // 指定第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.orderBookForm;
        index = index || 0;
        return storage.get(lsIndex, index);
    },
    delDraft: function(index = null) {
        var lsIndex = constant.orderBookForm;
        index = index || 0;
        storage.remove(lsIndex, index);
    },

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

    // 下单询价
    create: function(formData) {
        //缓存表单
        this.draft(formData)
        //网络请求
        var data = formData;
        var url = apis.OrderBookCreate;
        if (formData.id) url = apis.OrderBookEdit; //有ID传入时，即为更新
        var header = {
            'content-type': 'application/x-www-form-urlencoded'
        };
        return request({
            'url': url,
            'method': 'POST',
            'data': data,
            "headers": header,
            'params': {}
        })
    },

    cancel: function(oid) {
        //网络请求
        var data = {
            'id': oid
        };
        var url = apis.OrderBookCancel;
        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;
        })
    },

    //获取询价单列表
    list: function(formData = null, page = 0, psize = 10, ls = null) {
        page = page || 1;
        psize = psize || 10;
        //缓存表单
        var lsIndex = ls || constant.orderBookSearch
        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.OrderBookSearch;
        var header = {
            'content-type': 'application/x-www-form-urlencoded'
        };
        return request({
            'url': url,
            'method': 'POST',
            'data': data,
            "headers": header,
            'params': {}
        }).then((res) => {
            switch (res.code) {
                default:
                    uni.showToast({
                        title: "没有查询到相关订单!",
                        icon: "fail"
                    });
                    break;
                case 0:
                    var orders = {};
                    var totalCount = 0;
                    storage.set(constant.orderBookList, orders, 'page-' + page);
                    break;
                case 200:
                    var orders = res.data.orders;
                    var totalCount = res.data.total;
                    storage.set(constant.orderBookList, totalCount, 'total');
                    storage.set(constant.orderBookList, orders, 'page-' + page);
                    for (var x in orders) {
                        var order = orders[x];
                        var oid = order.id;
                        storage.set(constant.orderBookDetail, order, oid);
                    }
                    break;
            }
            return res.data;
        })
    },

    //询价单详情
    //@oid,cache(是否使用缓存)
    detail: function(oid, cache = null) {
        var data = {
            id: oid
        };
        var url = apis.OrderBookDetail;
        if (cache) {
            return new Promise((resolve, reject) => {
                var res = storage.get(constant.orderBookDetail, oid);
                if (res) {
                    resolve(res)
                } else {
                    // reject('没有查询到该运单的缓存')
                    resolve(false)
                }
            });
        }
        return request({
            'url': url,
            'method': 'POST',
            'data': data,
            'params': {}
        }).then((res) => {
            if (res.code == 200) {
                var order = res.data;
                storage.set(constant.orderBookDetail, order, oid);
                return res.data;
            } else {
                uni.showToast({
                    title: "询价单信息获取失败!",
                    icon: "fail"
                });
            }
        })
    }
}