'use strict';

async function lineGet (ctx) {
    try {
        const models = ctx.fs.dc.models;
        const { fleet } = ctx.query;
        const findOption = {
            order: [['id', 'DESC']],
            where: {

            },
        }
        if (fleet) {
            findOption.where.fleet = { $like: `%${fleet}%` }
        }

        const roadRes = await models.BusLine.findAll(findOption)

        ctx.status = 200;
        ctx.body = roadRes
    } catch (error) {
        ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
        ctx.status = 400;
        ctx.body = {
            message: typeof error == 'string' ? error : undefined
        }
    }
}

async function lineEdit (ctx) {
    try {
        const models = ctx.fs.dc.models;
        const data = ctx.request.body;

        if (!data.lineId) {
            await models.BusLine.create(data)
        } else {
            await models.BusLine.update(
                data, {
                where: {
                    id: data.lineId
                }
            })
        }

        ctx.status = 204
    } catch (error) {
        ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
        ctx.status = 400;
        ctx.body = {
            message: typeof error == 'string' ? error : undefined
        }
    }
}

async function lineDel (ctx) {
    try {
        const models = ctx.fs.dc.models;
        const { lineId } = ctx.params;

        await models.BusLine.destroy({
            where: {
                id: lineId
            }
        })

        ctx.status = 204
    } catch (error) {
        ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
        ctx.status = 400;
        ctx.body = {
            message: typeof error == 'string' ? error : undefined
        }
    }
}


async function carGet (ctx) {
    try {
        const models = ctx.fs.dc.models;
        const { fleet } = ctx.query;
        const findOption = {
            order: [['id', 'DESC']],
            where: {

            },
        }
        if (fleet) {
            findOption.where.fleet = { $like: `%${fleet}%` }
        }

        const roadRes = await models.BusCar.findAll(findOption)

        ctx.status = 200;
        ctx.body = roadRes
    } catch (error) {
        ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
        ctx.status = 400;
        ctx.body = {
            message: typeof error == 'string' ? error : undefined
        }
    }
}

async function carEdit (ctx) {
    try {
        const models = ctx.fs.dc.models;
        const data = ctx.request.body;

        if (!data.carId) {
            await models.BusCar.create(data)
        } else {
            await models.BusCar.update(
                data, {
                where: {
                    id: data.carId
                }
            })
        }

        ctx.status = 204
    } catch (error) {
        ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
        ctx.status = 400;
        ctx.body = {
            message: typeof error == 'string' ? error : undefined
        }
    }
}

async function carDel (ctx) {
    try {
        const models = ctx.fs.dc.models;
        const { carId } = ctx.params;

        await models.BusCar.destroy({
            where: {
                id: carId
            }
        })

        ctx.status = 204
    } catch (error) {
        ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
        ctx.status = 400;
        ctx.body = {
            message: typeof error == 'string' ? error : undefined
        }
    }
}

module.exports = {
    lineGet, lineEdit, lineDel,
    carGet, carEdit, carDel,
};