angular.module('PGSQLApp')
    .component('serverLocks', {
        templateUrl: 'components/serverLocks.html',
        controller: function ($log, $rootScope, connectionManager) {
            let ctrl = this
            ctrl.gridOptions = {}
            
            ctrl.gridOptions.columnDefs = [
                { name:'PID', field: 'pid', width:80 },
                { name:'Database', field: 'datname', width:100 },
                { name:'Lock Type', field: 'locktype', width:100  },
                { name:'Target Relation', field: 'relation', width:250  },
                { name:'Page', field: 'page', width:100 },
                { name:'Tuple', field: 'tuple', width:230 },
                { name:'vXID (target)', field: 'wait_event', width:70 },
                { name:'XID (target)', field: 'wait_event', width:100},
                { name:'Class', field: 'classid', width:100},
                { name:'Object ID', field: 'objid', width:100},
                { name:'vXID (owner)', field: 'virtualtransaction', width:100},
                { name:'Mode', field: 'mode', width:100},
                { name:'Granted', field: 'granted', width:100},
            ];

            $rootScope.$on('activeConnection-change', handleActiveConnectionChange)

            function handleActiveConnectionChange( evt, connId){
                $log.debug('active changed', connId);
                connectionManager.fetchLocks( connId )
                    .then((result)=>{
                        $log.debug('locks', result)
                        ctrl.gridOptions.data = result.data
                    })
            }
        }
    })