$(function () {
    'use strict';

    BWA.Popup.BudgetExecute = BWA.Popup.BudgetExecute || {};
    BWA.Popup.BudgetExecute.create = function (viewModel, options) {

        options = options || {};

        var onSelectedBudgets = options.onSelectedBudgets;
        var popupVisible = ko.observable(false);
        var budgetDataSource = BWA.DataUtil.createDataSource({
            dataSourceOptions: {
                store: BemsWebApplication.odata.FmsBudgetDetailExecutionEx,
                select: ['SiteId', 'Year', 'Month']
            }
        });

        var selectedYear = ko.observable();
        var selectedMonth = ko.observable();

        var dataViewModel = new BemsWebApplication.FmsBudgetViewModel(),
            initialized = false,
            gridView;

        //    
        //    equipmentFactory = BemsWebApplication.Factory.Equipment,
        //    dataSource = BWA.DataUtil.createDataSource({ dataSourceOptions: equipmentFactory.getDataSourceForDataGrid() }, 'FmsEquipmentEx');

        var eq = BWA.DataUtil.constructEqualFilter,
               and = BWA.DataUtil.andFilter;

        // 자재 검색시 나타나는 팝업은 공통 및 자신이 포함된 비즈니스 영역만 해당됨 
        var userInfo = BWA.UserInfo;

        var dataGridOptions = utils.datagrid.defaultOptions({
            pager: {
                showPageSizeSelector: false,
                allowedPageSizes: []
            },

            dxDataSource: budgetDataSource,

            // 아래들이 동작하지 않음
            selectedSourceItem: dataViewModel,
            handleDataGridRowClick: function () {
            },
            columns: [
                { dataField: 'Year', caption: 'Year', alignment: 'center', sortOrder: 'desc' },
                { dataField: 'Month', caption: 'Month', alignment: 'center', sortOrder: 'desc' },
            ],
            wordWrapEnabled: true
            // cellPrepared: BemsWebApplication.Factory.Equipment.cellPrepared
        });


        dataGridOptions.handleDataGridRowClick = function (id, dataGrid, clickRow) {

            var data = clickRow.data;
            if (!_.isUndefined(onSelectedBudgets)) {
                onSelectedBudgets([BWA.DataUtil.convertViewModelToJS(data)]);
            }

            popupVisible(false);
        }
       
        var toolbarItems = [
            { location: 'before', text: '실적등록 월 선택' },
            { location: 'after', widget: 'button', options: { text: $G('select'), icon: 'save', clickAction: handlePopupButtonSelect } }, //, visible: isMultipleSelect, 
            { location: 'after', widget: 'button', options: { text: $G('close'), icon: 'close', clickAction: handlePopupButtonClose } } // 
        ];

        var popupOptions = {

            width: '400px',
            height: 'auto',
            visible: popupVisible,
            closeOnOutsideClick: true,
            //showingAction: handlePopupShowing,
            shownAction: handlePopupShown,
            animation: window.utils.popup.createAnimation()
        };

        function handlePopupShown() {

            if (initialized === false) {
                gridView = $('#budgetDataGridForYear').dxDataGrid('instance');
                initialized = true;
            }

            gridView.clearSelection();
            gridView.refresh();
        }

        function handlePopupButtonSelect() {

            //dataViewModel = gridView.getSelectedRowsData()[0];
            //selectedYear = dataViewModel.Year();
            //popupVisible(false);

            var array = gridView.getSelectedRowsData()[0];
            if (!_.isUndefined(onSelectedBudgets)) {
                onSelectedBudgets(array);
            }
            popupVisible(false);
        }

        function handlePopupButtonClose() {
            popupVisible(false);
        }


        return {
            dataModel: dataViewModel,
            //dataSource: dataSource,
            selectedYear: selectedYear,
            selectedMonth: selectedMonth,
            popupOptions: popupOptions,
            toolbarItems: toolbarItems,
            dataGridOptions: dataGridOptions,
            popupVisible: popupVisible,
            onSelectedBudgets:onSelectedBudgets, // hcLee 2015 12 09
            show: function () {
                popupVisible(true);
            }
        };
    };
});