//---------------------------------------------------------------------------------------------
// This file was AUTO-GENERATED by "SPA Views" Xomega.Net generator.
//
// Manual CHANGES to this file WILL BE LOST when the code is regenerated.
//---------------------------------------------------------------------------------------------

import { SalesOrderObject } from 'DataObjects/Sales/SalesOrderObjectCustomized';
import { DetailsViewModel, ViewParams, DataRow, ViewModel, ViewEvent, ViewSelectionEvent } from 'xomega';
import * as controls from 'xomega-controls';

class SalesOrderView extends DetailsViewModel {

    public obj: SalesOrderObject;
    public activeTab = ko.observable('Customer');

    public constructor() {
        super();
        this.DetailsObject = this.obj = new SalesOrderObject();
    }

    protected getBaseTitle(): string {
        return 'Sales Order';
    }

    public attached(view, parent) {
        if (this.Params[ViewParams.Mode] == ViewParams.ModePopup)
            controls.modalViewPopup(this, view);
        else if (this.Params[ViewParams.Mode] == ViewParams.ModeInline)
            controls.vSplitViewPopup(this, view);
    }

    protected LinkCustomerLookupLookUp_Params(): any {
        let vm = this;
        let params: any = {};
        params[ViewParams.Action] = ViewParams.ActionSelect;
        params[ViewParams.SelectionMode] = ViewParams.SelectionModeSingle;
        params["StoreNameOperator"] = "CN";
        params["StoreName"] = vm.obj.CustomerObject.LookupObject.StoreName.EditStringValue();
        params["PersonNameOperator"] = "CN";
        params["PersonName"] = vm.obj.CustomerObject.LookupObject.PersonName.EditStringValue();
        params[ViewParams.Mode] = ViewParams.ModePopup;
        params[ViewParams.Source] = "LinkCustomerLookupLookUp";
        return params;
    }

    public LinkCustomerLookupLookUp_Click() {
        if (!this.LinkCustomerLookupLookUp_Enabled()) return;
        let params = this.LinkCustomerLookupLookUp_Params();
        let vm = this;
        this.navigateTo('CustomerListView', params);
    }

    public LinkCustomerLookupLookUp_Enabled() {
        return true;
    }

    protected LinkCustomerLookupLookUp_HandleResult(selectedRows: Array<DataRow>) {
        if (!selectedRows == null || selectedRows.length != 1) return;
        let vm = this;
        let row: any = selectedRows[0];
        vm.obj.CustomerObject.CustomerId.InternalValue(row.CustomerId);
        vm.obj.CustomerObject.StoreId.InternalValue(row.StoreId);
        vm.obj.CustomerObject.StoreName.InternalValue(row.StoreName);
        vm.obj.CustomerObject.PersonId.InternalValue(row.PersonId);
        vm.obj.CustomerObject.PersonName.InternalValue(row.PersonName);
        vm.obj.CustomerObject.AccountNumber.InternalValue(row.AccountNumber);
        vm.obj.CustomerObject.TerritoryId.InternalValue(row.TerritoryId);
    }

    protected LinkDetailDetails_Params(data): any {
        let vm = this;
        let params: any = {};
        params["SalesOrderDetailId"] = data.SalesOrderDetailId;
        params[ViewParams.Mode] = ViewParams.ModePopup;
        params[ViewParams.Source] = "LinkDetailDetails";
        return params;
    }

    public LinkDetailDetails_Click(data) {
        if (!this.LinkDetailDetails_Enabled(data)) return;
        let params = this.LinkDetailDetails_Params(data);
        let vm = this;
        this.navigateTo('SalesOrderDetailView', params);
    }

    public LinkDetailDetails_Enabled(data) {
        return true;
    }

    protected LinkDetailNew_Params(): any {
        let vm = this;
        let params: any = {};
        params[ViewParams.Action] = ViewParams.ActionCreate;
        params["SalesOrderId"] = vm.obj.SalesOrderId.EditStringValue();
        params[ViewParams.Mode] = ViewParams.ModePopup;
        params[ViewParams.Source] = "LinkDetailNew";
        return params;
    }

    public LinkDetailNew_Click() {
        if (!this.LinkDetailNew_Enabled()) return;
        let params = this.LinkDetailNew_Params();
        let vm = this;
        this.navigateTo('SalesOrderDetailView', params);
    }

    public LinkDetailNew_Enabled() {
        return !this.obj.IsNew();
    }

    protected onChildEvent(child: ViewModel, e: ViewEvent) {
        let vm = this;
        if (e instanceof ViewSelectionEvent && child.Params && child.Params[ViewParams.Source] === 'LinkCustomerLookupLookUp')
            vm.LinkCustomerLookupLookUp_HandleResult(e.SelectedRows);
        if (e.isClosed() && child.Params && child.Params[ViewParams.Source] === 'LinkDetailDetails')
            vm.obj.DetailList.clearSelectedRows();
        super.onChildEvent(child, e);
    }
}

export = SalesOrderView;
