

var DataSource, customerId = [];
$(function () {

    GetCustomerID();
    GetDataSource();
    BindGrid();


});


function GetDataSource() {

    DataSource = new kendo.data.DataSource({
        pageSize: 10,

        transport: {
            read: {
               /// var data = [];

                //Add sample data here to reproduce the issue
                //data.push({
                //    "id": 1,
                //    "Title": "Some title",
                //    "Summary": "another title",
                //    "StartDate": new Date(2013, 1, 1),
                //    "EndDate": new Date(2013, 1, 2),
                //    "CustomerID": 3
                //});
                //e.success(data);
                type: "GET",
                url: "../kendo.svc/GetAllAnnouncements",
                contentType: "application/json; charset=utf-8",
                dataType: "json"
            },

            create: function (e) {
                debugger;

                var item = e.data;
                item.Id = $("#kendoGrid").data("kendoGrid").dataSource.data().length + 1;
                e.success(item);
            },

            update: function (e) {
                e.success();
            },

            destroy: function (e) {
                e.success();
            },


            parameterMap: function (data, operation) {

                if (operation == "create") {

                    //  return kendo.stringify({ newAnnouncement: data });
                }

                else if (operation == "update") {

                    // return kendo.stringify({ updateAnnouncement: data });
                }

                else if (operation == "destroy") {
                    // return kendo.stringify({ id: data.id });
                }
            }

        },

        schema: {

            parse: function (data) {

                return JSON.parse(data.d);

            },
            model: {
                id: "id",
                fields: {
                    "id": { type: "number" },
                    "Title": { type: "string", required: true },
                    "Summary": { type: "string" },
                    "StartDate": { type: "date", format: "{ 0: dd / MM / yyyy }", required: true },
                    "EndDate": { type: "date", format: "{ 0: dd / MM / yyyy }" },
                    "CustomerID": { field: "CustomerID", defaultValue: 1 }

                }
            }
        }
    });

    DataSource.read();
}


function BindGrid() {
    $('#kendoGrid').kendoGrid({



        pageable: true,
        toolbar: ["create"],
        columns: [
            { field: "Title", title: "Title" },
            { field: "Summary", title: "Summary" },
            { field: "StartDate", title: "Start Date", template: '#=kendo.toString(StartDate,"dd/MM/yyyy")#' },
            { field: "EndDate", title: "End Date", template: '#=kendo.toString(EndDate,"dd/MM/yyyy")#' },
            {
                field: "CustomerID",
                editor: CustomerEditor,
                title: "CustomerID",
            },
            { command: ["edit", "destroy"] }
        ],
        editable: "popup",
        dataSource: DataSource

    });
}


function CustomerEditor(container, options) {

    $('.k-input').attr('required', 'required');

    $('<input data-bind="value:' + options.field + '"/>')
        .appendTo(container)
        .kendoDropDownList({
            dataValueField: "CustomerID",
            dataTextField: "CustomerID",
            dataSource: customerId
        });
}




function GetCustomerID() {

	//for (var i = 0; i < 10; i++) {

           //     customerId.push({
                    CustomerID: i+1
            //    });
          //  }

    $.ajax({

        type: "GET",
        url: "../kendo.svc/GetCustomerId",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            var jsonData = JSON.parse(data.d);
            for (var i = 0; i < jsonData.length; i++) {

                customerId.push({
                    CustomerID: jsonData[i].CustomerID
                });
            }
        },
        error: function (error) {

            alert("error");
        }
    });
}
