/**
 * Created by zhang on 2018/9/10.
 */
import BaseController from "../../common/base/base_controller.js";
import CompilationModel from '../../users/models/compilation_model.js';
let config = require("../../../config/config.js");
import landFacade from "../facade/land_facade.js";
import { checkCompilationPermission } from '../../common/base/base_util.js';

class LandController extends BaseController {
    async main(request, response) {
        let compilationModel = new CompilationModel();
        let compilationList = await compilationModel.getPermissionCompilationList(request, { _id: 1, name: 1 });
        compilationList.unshift({ _id: 'all', name: '所有' });
        let activeCompilation = compilationList.find(compilation => compilation._id.toString() === request.query.filter);
        if (activeCompilation) {
            activeCompilation.active = 'active';
        } else {
            compilationList[0].active = 'active'
        }
        let filter = request.query.filter ? { compilationId: request.query.filter } : {};
        let landLibs = await landFacade.findByCondition(filter, { rates: 0 }, false);
        const compilationPermission = request.session.managerData.compilationPermission || [];
        landLibs = landLibs.filter(lib => compilationPermission.includes(lib.compilationId));
        let randerData = {
            title: '土地补偿库',
            userAccount: request.session.managerData.username,
            userID: request.session.managerData.userID,
            landLibs,
            compilationList: compilationList,
            layout: 'maintain/common/html/layout'
        };
        response.render("maintain/land_lib/html/main", randerData);
    }
    async addLib(request, response) {
        try {
            await landFacade.addLib(request.body);
        } catch (error) {
            console.log(error);
        }
        response.redirect(request.headers.referer);
    }
    async findLib(request, response) {
        let result = {
            error: 0
        };
        try {
            let data = request.body.data;
            data = JSON.parse(data);
            let conditions = { ID: data.ID };
            let resultData = await landFacade.findByCondition(conditions);
            result.data = resultData;
        } catch (err) {
            console.log(err);
            result.error = 1;
            result.message = err.message;
        }
        response.json(result);
    }
    async saveLib(request, response) {
        let result = {
            error: 0
        };
        try {
            let data = request.body.data;
            data = JSON.parse(data);
            let resultData = await landFacade.saveLib(data);
            result.data = resultData;
        } catch (err) {
            console.log(err);
            result.error = 1;
            result.message = err.message;
        }
        response.json(result);
    }
    async deleteLibByID(request, response) {
        let result = {
            error: 0
        };
        try {
            let data = request.body.data;
            data = JSON.parse(data);
            let resultData = await landFacade.deleteLibByID(data.ID);
            result.data = resultData;
        } catch (err) {
            console.log(err);
            result.error = 1;
            result.message = err.message;
        }
        response.json(result);
    }
    async edit(request, response) {
        let libID = request.params.libID;
        let landLib = await landFacade.findByCondition({ 'ID': libID });
        if (landLib) {
            checkCompilationPermission(request, response, landLib.compilationId, '/land/main');

            let randerData = {
                title: '土地补偿库',
                mainURL: '/land/main',
                libName: landLib.libName,
                userAccount: request.session.managerData.username,
                userID: request.session.managerData.userID,
                landList: JSON.stringify(landLib.regions),
                libID: libID,
                LicenseKey: config.getLicenseKey(process.env.NODE_ENV),
                layout: 'maintain/common/html/edit_layout'
            };
            response.render("maintain/land_lib/html/edit", randerData);
        } else {
            response.redirect(request.headers.referer);
        }
    }
}

export default LandController;