/**
 *  个人中心 退换货
 *  @author 陈轩 <xuan.chen@yoho.cn>
 */
'use strict';
const helpers = global.yoho.helpers;
const returnsModel = require('../models/returns');

// 退货时传退货收件地址
const GOODS_BACK_ADDRESS = {
    address: '江苏南京市江宁经济开发区苏源大道87号YOHO!有货物流中心东一楼',
    name: 'YOHO!有货仓库',
    phone: '400-889-9646',
    postcode: '211106'
};

/**
 * 我的退换货-列表页
 */
const index = (req, res, next) => {
    const page = req.query.page;

    req.ctx(returnsModel).getReturnsList(req.user.uid, page, 10)
        .then(data => {
            const viewData = Object.assign({
                module: 'home',
                page: 'returns',
                meReturnsPage: true
            }, data);

            res.render('returns/returns', viewData);
        })
        .catch(next);
};

/**
 * 我的订单-退货申请页
 */
const refundApply = (req, res, next) => {
    const orderCode = req.query.orderCode;
    const uid = req.user.uid;

    req.ctx(returnsModel).getOrderRefund(orderCode, uid).then(result => {
        res.render('returns/returns-apply', result);
    }).catch(next);
};

/**
 * 我的订单-退货提交
 */
const saveRefund = (req, res, next) => {
    const uid = req.user.uid;

    req.ctx(returnsModel).saveRefund(req, uid).then(result => {
        res.send(result);
    }).catch(next);
};

/**
 * 退货成功
 */
const refundSuccess = (req, res) => {
    let resData = {
        save: {
            type: '退货',
            ordersHref: helpers.urlFormat('/home/orders'),
            returnsHref: helpers.urlFormat('/home/returns'),
            refund: GOODS_BACK_ADDRESS
        }
    };

    res.render('returns/returns-save', {returns: resData});
};

/**
 * 换货成功
 */
const exchangeSuccess = (req, res) => {
    let resData = {
        save: {
            type: '换货',
            ordersHref: helpers.urlFormat('/home/orders'),
            returnsHref: helpers.urlFormat('/home/returns')
        }
    };

    res.render('returns/returns-save', {returns: resData});
};

/**
 * 我的退换货-退货详情页
 */
const refundDetail = (req, res, next) => {
    const code = req.query.id;
    const uid = req.user.uid;
    let resData = {
        module: 'home',
        page: 'returns',
        backAddress: GOODS_BACK_ADDRESS
    };

    req.ctx(returnsModel).getRefundDetail(code, uid).then(result => {
        Object.assign(resData, result);
        res.render('returns/returns-detail', resData);
    }).catch(next);
};

/**
 * 我的订单-提交换货申请
 */
const saveExchange = (req, res, next) => {

    let uid = req.user.uid;

    req.ctx(returnsModel).saveExchange(req, uid).then(result => {
        res.send(result);

    }).catch(next);
};

/**
 * 我的退换货-换货详情页
 */
const exchangeDetail = (req, res, next) => {
    const code = req.query.id;
    const uid = req.user.uid;
    let resData = {
        module: 'home',
        page: 'returns'
    };

    req.ctx(returnsModel).getChangeDetail(code, uid).then(result => {
        Object.assign(resData, result);
        res.render('returns/returns-detail', resData);
    }).catch(next);
};

/**
 * 我的订单-获取换货方式
 */
const getDelivery = (req, res, next) => {
    let orderCode = req.body.orderCode;
    let areaCode = req.body.areaCode;
    let skns = req.body.skns;

    // 获取必要参数
    let uid = req.user.uid;

    // 调用接口获得该用户支持的换货方式(白金会员可享受上门换货,偏远地区不支持上门换货)
    req.ctx(returnsModel).getDelivery(orderCode, areaCode, uid, skns).then(result => {
        res.send(result);
    }).catch(next);
};

/**
 * 我的订单-换货申请页
 */
const exchangeApply = (req, res, next) => {
    const orderCode = req.query.orderCode;
    const uid = req.user.uid;

    req.ctx(returnsModel).getOrderExchange(orderCode, uid).then(result => {
        res.render('returns/returns-apply', result);
    }).catch(next);
};

/**
 * 取消退货申请
 */
const cancelRefund = (req, res, next) => {
    let id = req.body.id;
    let uid = req.user.uid;

    req.ctx(returnsModel).getCancelRefund(id, uid).then(result => {
        res.send(result);
    }).catch(next);
};

/**
 * 取消换货申请
 */
const cancelChange = (req, res, next) => {
    let id = req.body.id;
    let uid = req.user.uid;

    req.ctx(returnsModel).getCancelChange(id, uid).then(result => {
        res.send(result);
    }).catch(next);
};

/**
 * 我的退换货-设置快递单号
 */
const setExpressNumber = (req, res, next) => {

    let uid = req.user.uid;
    let id = req.body.id;
    let isChange = req.body.isChange === 'false' ? false : true;
    let expressId = req.body.expressId;
    let expressNumber = req.body.expressNumber;
    let expressCompany = req.body.expressCompany;

    req.ctx(returnsModel).setExpressNumber(id, expressId, expressNumber, uid, expressCompany, isChange).then(result => {

        if (res.code && res.code === 201) {
            res.json({message: '请求失败'});
        }

        if (id || expressId || expressNumber || uid || expressCompany || isChange) {
            res.send(result);
        }

    }).catch(next);

};

const refundCompute = (req, res, next) => {
    let uid = req.user.uid;
    let orderCode = req.body.orderCode;
    let goods = req.body.goods;

    req.ctx(returnsModel).refundCompute(uid, orderCode, goods).then(d => {
        res.json(d);
    }).catch(next);
};

module.exports = {
    index,
    refundApply,
    saveRefund,
    refundSuccess,
    refundDetail,
    saveExchange,
    exchangeApply,
    exchangeSuccess,
    exchangeDetail,
    getDelivery,
    cancelRefund,
    cancelChange,
    setExpressNumber,
    refundCompute
};