{"size": 3802, "ext": "js", "max_stars_repo_path": "src/main/webapp/screens/CRUD/CRUD.js", "max_stars_repo_name": "blakemcbride/Kiss", "max_stars_repo_stars_event_min_datetime": "2020-06-15T15:56:47.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-14T23:52:03.000Z", "max_issues_repo_path": "src/main/frontend/screens/CRUD/CRUD.js", "max_issues_repo_name": "starikode430/Kiss", "max_issues_repo_issues_event_min_datetime": "2021-01-29T14:46:57.000Z", "max_issues_repo_issues_event_max_datetime": "2021-01-29T18:28:33.000Z", "max_forks_repo_path": "src/main/frontend/screens/CRUD/CRUD.js", "max_forks_repo_name": "starikode430/Kiss", "max_forks_repo_forks_event_min_datetime": "2019-09-24T21:42:37.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-09T04:13:36.000Z"}
{"max_stars_count": 36, "max_issues_count": 1, "max_forks_count": 6, "avg_line_length": 29.937007874, "max_line_length": 105, "alphanum_fraction": 0.4786954235}

/* global $$ */

'use strict';

(async function () {

    const WS = 'services/Crud';

    const columnDefs = [
        {headerName: 'Last Name', field: 'lastName', width: 33  },
        {headerName: 'First Name', field: 'firstName', width: 33 },
        {headerName: 'Phone Number', field: 'phoneNumber', width: 33 }
    ];
    const grid = new AGGrid('grid', columnDefs, 'id');
    grid.show();

    async function updateGrid() {
        grid.clear();
        $$('edit').disable();
        $$('delete').disable();
        const res = await Server.call(WS, 'getRecords');
        if (res._Success)
            grid.addRecords(res.rows);
    }

    updateGrid();

    $$('new').onclick(() => {
        $$('erp-title').setValue('Add Record');
        $$('erp-first-name').clear();
        $$('erp-last-name').clear();
        $$('erp-phone-number').clear();
        Utils.popup_open('edit-record-popup', 'erp-first-name');

        $$('erp-ok').onclick(async () => {
            if ($$('erp-first-name').isError('First Name'))
                return;
            if ($$('erp-last-name').isError('Last Name'))
                return;
            const data = {
                firstName: $$('erp-first-name').getValue(),
                lastName: $$('erp-last-name').getValue(),
                phoneNumber: $$('erp-phone-number').getValue()
            };
            const res = await Server.call(WS, 'addRecord', data);
            if (res._Success) {
                Utils.popup_close();
                updateGrid();
            }
        });

        $$('erp-cancel').onclick(() => {
            Utils.popup_close();
        });
    });

    function edit() {
        const row = grid.getSelectedRow();
        $$('erp-title').setValue('Edit Record');
        $$('erp-first-name').setValue(row.firstName);
        $$('erp-last-name').setValue(row.lastName);
        $$('erp-phone-number').setValue(row.phoneNumber);
        Utils.popup_open('edit-record-popup', 'erp-first-name');

        $$('erp-ok').onclick(async () => {
            if ($$('erp-first-name').isError('First Name'))
                return;
            if ($$('erp-last-name').isError('Last Name'))
                return;
            const data = {
                id: row.id,
                firstName: $$('erp-first-name').getValue(),
                lastName: $$('erp-last-name').getValue(),
                phoneNumber: $$('erp-phone-number').getValue()
            };
            const res = await Server.call(WS, 'updateRecord', data);
            if (res._Success) {
                Utils.popup_close();
                updateGrid();
            }
        });

        $$('erp-cancel').onclick(() => {
            Utils.popup_close();
        });

    }

    $$('edit').onclick(edit);
    grid.setOnRowDoubleClicked(edit);

    grid.setOnSelectionChanged((rows) => {
        $$('edit').enable(rows);
        $$('delete').enable(rows);
    });

    $$('delete').onclick(() => {
        Utils.yesNo('Conformation', 'Are you sure you want to delete the selected record?', async () => {
            const row = grid.getSelectedRow();
            const data = {
                id: row.id
            };
            const res = await Server.call(WS, 'deleteRecord', data);
            if (res._Success) {
                updateGrid();
            }
        });
    });

    $$('report').onclick(async () => {
        // This will only work if you have groff installed on your computer
        const res = await Server.call(WS, 'runReport');
        if (res._Success) {
            Utils.showReport(res.reportUrl);
        }
    });

    $$('export').onclick(async () => {
        const res = await Server.call(WS, 'runExport');
        if (res._Success) {
            Utils.showReport(res.exportUrl);
        }
    });


})();