/**
 *   换货 Model
 *   DOC: http://git.yoho.cn/yoho-documents/api-interfaces/blob/master/订单/exchange.md
 */
const utils = '../../../utils';
const api = global.yoho.API;
const _ = require('lodash');
const decodeURIComponentExt = require(`${utils}/string-process`).decodeURIComponent;

const exchange = {
    // 获取 换货列表
    getOrderData(uid, orderId) {
        return api.get('', {
            method: 'app.change.goodsList',
            uid: uid,
            order_code: orderId
        }, {
            cache: true,
            code: 200
        });
    },

    // 加载用户可选择的退货方式列表
    getDelivery(uid, areaCode) {
        return api.get('', {
            method: 'app.change.getDelivery',
            area_code: areaCode,
            uid: uid
        }, {
            cache: true,
            code: 200
        });
    },

    submitExchange(uid, params) {
        if (params.address_id) {
            params.address_id = decodeURIComponentExt(params.address_id);
        }

        let data = Object.assign(params, {
            uid,
            method: 'app.change.submit'
        });

        if (!data.zip_code) {
            delete data.zip_code;
        }

        return api.get('', data);
    },

    /**
     * 取消换货申请
     * @param uid
     * @param id
     */
    cancelApply(uid, id) {
        return api.post('', {
            uid: uid,
            id: id,
            method: 'app.change.cancel'
        });
    },

    // 获取商品持否支持上门换货
    getrefreshDelivery(uid, areaCode, skns, orderCode) {
        return api.get('', {
            method: 'app.change.refreshDelivery',
            area_code: areaCode,
            uid: uid,
            skns: skns,
            order_code: orderCode
        }, {
            code: 200
        }).then(result => {
            _.forEach(result.data, function(val, index) {
                if (val.is_support === 'N') {
                    result.data.splice(index, 1);
                }
            });
            return result;
        });
    }
};

module.exports = exchange;