
// Core imports
import { Component, OnInit, Input } from '@angular/core'

// Core Services


// Module Services
import { SendCartService } from '../../services/svc.send-cart';

/**
 * @name Sales Representative Component
 * @description Creates an input field to post Sales Rep data into the send-cart object
 * @author Kasper Hansen
 * @since 19/11/18
 * @extends void
 * @todo Move out of the bus block, however the API does not support this at this time.
 */
@Component({
    moduleId: module.id+ '',
    selector: 'send-cart-sales-rep',
    templateUrl: './template/t--sales-rep.pug',
    styleUrls: ['sty.sales-rep.scss']
})

export class SalesRepComponent {

    // ---- Variables ---- \\
    @Input('state') state: boolean

    content: string = ''
    private _salesRepSessionId: string = 'salesRep'
    
    constructor(
        private _sendCart: SendCartService
    ) { }


    // ---- Lifecycle hooks ---- \\
    ngOnInit() {
        new Promise((resolve) => {
            resolve(this._sendCart.sessionStore('get'))
        }).then((response) => {
            let salesRep = response[this._salesRepSessionId]
            
            if(!!salesRep)
                this.content = response[this._salesRepSessionId]
        })

    }


    // ---- Functions ---- \\
    /**
     * handshakes input event to session and cart
     * 
     * @param event 
     */
    setSalesRep(event) {
        let value = event.srcElement.value

        this._sendCart.sessionStore('set', this._salesRepSessionId, value)
        this._sendCart.setSalesRep(value)
    }

}
