'use strict';

/**
 *
 *
 * @author Zhong
 * @date 2019/1/25
 * @version
 */

import BaseController from "../../common/base/base_controller";
import vvTaxFacade from "../facade/vvTax_facade";
let config = require("../../../config/config.js");

class VVTaxController extends BaseController{
    async main(request, response) {
        let vvTaxLibs = await vvTaxFacade.findByCondition({},false,false);
        let randerData = {
            title:'车船税库',
            userAccount: request.session.managerData.username,
            userID: request.session.managerData.userID,
            vvTaxLibs:vvTaxLibs,
            layout: 'maintain/common/html/layout'
        };
        response.render("maintain/vehicleVesselTax_lib/html/main", randerData);
    }
    async addLib(request, response){
        try {
            await vvTaxFacade.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 vvTaxFacade.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 vvTaxFacade.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 vvTaxFacade.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 vvTaxLib = await vvTaxFacade.findByCondition({'ID':libID}, true);
        if(vvTaxLib){
            let randerData = {
                title:'车船税库',
                mainURL:'/vvTax/main',
                libName:vvTaxLib.name,
                userAccount: request.session.managerData.username,
                userID: request.session.managerData.userID,
                vvTaxList:JSON.stringify(vvTaxLib.data),
                libID:libID,
                LicenseKey:config.getLicenseKey(process.env.NODE_ENV),
                layout: 'maintain/common/html/edit_layout'
            };
            response.render("maintain/vehicleVesselTax_lib/html/edit", randerData);
        }else {
            response.redirect(request.headers.referer);
        }
    }
}
export default VVTaxController;