/**
 * 我的投诉controller
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2016/8/18
 */
'use strict';

const complaints = require('../models/complaints');

/**
 * 我的投诉页
 * @function index
 * @param { String } page 页码
 * @return { Object } 投诉列表数据
 */
const index = (req, res, next) => {
    let uid = req.user.uid;
    let page = req.query.page || 1;

    complaints.getComplaintsList(uid, page).then(result => {
        res.render('complaints', result);
    }).catch(next);
};

/**
 * 添加投诉接口
 * @function submit
 * @param { String } url 图片路径
 * @return { Object } 原图片路径
 */
const submit = (req, res, next) => {
    let uid = req.user.uid;
    let params = req.body;

    if (!req.xhr || !uid) {
        return next();
    }

    complaints.addComplaints(uid, params).then(result => {
        res.json(result);
    }).catch(next);
};

/**
 * 取消投诉接口
 * @function submit
 * @param { String } url 图片路径
 * @return { Object } 原图片路径
 */
const cancel = (req, res, next) => {
    let uid = req.user.uid;
    let id = req.body.id;

    if (!req.xhr || !uid) {
        return next();
    }

    complaints.cancelComplaints(uid, id).then(result => {
        res.json(result);
    }).catch(next);
};

module.exports = {
    index,
    submit,
    cancel
};