const { Controller } = require('libflitter')

class CobaltController extends Controller {
    static get services() {
        return [...super.services, 'Vue']
    }

    async listing(req, res, next) {
        return res.page('cobalt:listing', {
            ...this.Vue.data({ resource: this._get_resource(req.params) }),
            ...this.Vue.session(req)
        })
    }

    async form(req, res, next) {
        const other = {
            mode: (req.query.id ? (req.query.mode === 'view' ? 'view' : 'update') : 'insert'),
            form_id: req.query.id || ''
        }

        return res.page('cobalt:form', {
            ...this.Vue.data({ resource: this._get_resource(req.params), ...other }),
            ...this.Vue.session(req),
        })
    }

    _get_resource(params) {
        const resource = params.resource
        delete params.resource

        const parts = [resource]
        let i = 0
        while ( params[i] ) {
            parts.push(params[i])
            i++
        }

        return parts.join('')
    }

}

module.exports = exports = CobaltController
