/**
 * 通知管理控制器
 *
 * @author CaiAoLin
 * @date 2017/6/7
 * @version
 */
import BaseController from "../../common/base/base_controller";
import MessageModel from "../models/message_model";
import Config from "../../../config/config";
let config = require("../../../config/config.js");

class NotifyController extends BaseController {

    /**
     * 用户通知页面
     *
     * @param {object} request
     * @param {object} response
     * @return {void}
     */
    async index(request, response) {
        let messageModel = new MessageModel();
        let type = request.query.type;
        type = type === undefined ? messageModel.USER : type;
        type = parseInt(type);

        let condition = messageModel.getFilterCondition(request);
        condition.message_type = type;

        let messageList = await messageModel.getList(condition);

        // 获取消息总数
        let total = await messageModel.count();

        // 获取用户通知总数
        let userMessageTotal = await messageModel.count({message_type: messageModel.USER});
        // 获取内部通知总数
        let systemMessageTotal = await messageModel.count({message_type: messageModel.SYSTEM});

        // 分页数据
        let page = request.query.page === undefined ? 1 : request.query.page;
        let pageData = {
            current: page,
            total: parseInt(total / Config.pageSize),
            queryData: response.locals.urlQuery
        };

        let renderData = {
            layout: 'users/views/layout/layout',
            messageList: messageList,
            pages: pageData,
            userMessageTotal: userMessageTotal,
            systemMessageTotal: systemMessageTotal,
            type: type,
            filter: request.query,
            LicenseKey:config.getLicenseKey(process.env.NODE_ENV)
        };
        response.render('users/views/notify/index', renderData);
    }

    /**
     * 新增消息页面
     *
     * @param {object} request
     * @param {object} response
     * @return {void}
     */
    async add(request, response) {
        let messageModel = new MessageModel();
        let type = request.query.type;
        type = type === undefined ? messageModel.USER : type;
        type = parseInt(type);

        // 获取用户通知总数
        let userMessageTotal = await messageModel.count({message_type: messageModel.USER});
        // 获取内部通知总数
        let systemMessageTotal = await messageModel.count({message_type: messageModel.SYSTEM});

        let renderData = {
            layout: 'users/views/layout/layout',
            messageData: [],
            type: type,
            userMessageTotal: userMessageTotal,
            systemMessageTotal: systemMessageTotal,
            LicenseKey:config.getLicenseKey(process.env.NODE_ENV)
        };
        response.render('users/views/notify/save', renderData);
    }

    /**
     * 新增操作
     *
     * @param {object} request
     * @param {object} response
     * @return {void}
     */
    addMessage(request, response) {
        let url = '/notify';
        try {
            let sessionUserData = request.session.managerData;
            let messageModel = new MessageModel();
            let result = messageModel.addData(request.body, sessionUserData.username);

            if (!result) {
                throw '新增信息失败';
            }
        } catch (error) {
            console.log(error);
            url = request.headers.referer;
        }

        response.redirect(url);
    }

    /**
     * 编辑消息
     *
     * @param {object} request
     * @param {object} response
     * @return
     */
    async modify(request, response) {
        let id = request.params.id;
        if (id <= 0) {
            return response.redirect('/notify');
        }

        // 查找对应数据
        let messageModel = new MessageModel();
        let messageData = await messageModel.findDataByCondition({_id: id});

        let type = request.query.type;
        type = type === undefined ? messageModel.USER : type;
        type = parseInt(type);

        // 获取用户通知总数
        let userMessageTotal = await messageModel.count({message_type: messageModel.USER});
        // 获取内部通知总数
        let systemMessageTotal = await messageModel.count({message_type: messageModel.SYSTEM});

        let renderData = {
            layout: 'users/views/layout/layout',
            messageData: messageData,
            userMessageTotal: userMessageTotal,
            systemMessageTotal: systemMessageTotal,
            type: type,
            LicenseKey:config.getLicenseKey(process.env.NODE_ENV)
        };
        response.render("users/views/notify/save", renderData);
    }

    /**
     * 修改消息
     *
     * @param {object} request
     * @param {object} response
     * @return {void}
     */
    async modifyMessage(request, response) {
        let id = request.params.id;

        try {
            let sessionUserData = request.session.managerData;
            let modifyData = request.body;
            modifyData.last_update = sessionUserData.username;
            // 修改对应数据
            let messageModel = new MessageModel();
            let result = messageModel.modifyData(id, modifyData);

            if (!result) {
                throw '修改数据失败';
            }
        } catch (error) {
            console.log(error);
        }
        response.redirect(request.headers.referer);
    }

    /**
     * 删除对应消息
     *
     * @param {object} request
     * @param {object} response
     * @return {void}
     */
    async delete(request, response) {
        let id = request.params.id;

        try {
            // 修改对应数据
            let messageModel = new MessageModel();
            let result = await messageModel.deleteById(id, true);

            if (!result) {
                throw '删除数据失败';
            }
        } catch (error) {
            console.log(error);
        }
        response.redirect('/notify');
    }

    /**
     * 发布消息
     *
     * @param {object} request
     * @param {object} response
     * @return {void}
     */
    release(request, response) {
        let id = request.params.id;

        try {
            let messageModel = new MessageModel();
            let sessionUserData = request.session.managerData;
            let result = messageModel.release(id, sessionUserData.username);

            if (!result) {
                throw '发布消息失败';
            }
        } catch (error) {
            console.log(error);
        }

        response.redirect(request.headers.referer);
    }

}

export default NotifyController;