import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import { Accounts } from 'meteor/accounts-base';

// Accounts.onLogin(function(user) {
//     debugger;
//     console.log(",", user.user._id)
// });

import Worklist from '../collections/worklist.js';


Meteor.methods({
    'Fetchworklist' (devicemap) {

        check(devicemap, String);
        this.unblock();
        if (this.userId) {
            try {

                const result = HTTP.call('GET', `http://192.168.0.156/Broker/HISIntegration.svc/rest/Fetchworklist/${devicemap}`);
                let worklistHttp = JSON.parse(result.content);

                Worklist.remove({ _userid: this.userId, _devicemap: devicemap });

                // worklist.forEach((patient) => {
                //     Worklist.insert({ PatientID: worklist.PatientID, _userid: worklist._userid, _devicemap: devicemap }, {
                //         $set: {
                //             ...patient
                //         }
                //     });
                // })


                worklistHttp.WorkListItems.forEach((patient) => {
                    Worklist.insert({
                        ...patient,
                        _userid: this.userId,
                        _devicemap: devicemap
                    });
                })


                // worklist._userid = this.userId;
                // worklist._devicemap = devicemap;
                // Worklist.upsert({ _userid: worklist._userid, _devicemap: devicemap }, { $set: worklist });
                return true
            } catch (e) {
                // Got a network error, timeout, or HTTP error in the 400 or 500 range.
                return false;
            }
        }
    }
});