rem ' Customer master file maintenance (BBj GUI user interface)

dim customer$:"id:c(6),name:c(32),phone:c(24)"
filename$ = "customer.dat"
customer = unt
open (customer,err=makefile)filename$
goto init

makefile:
mkeyed filename$,[0:1:6],0,64
open (customer)filename$
while 1
   dread customer.id$,customer.name$,customer.phone$,err=eof
   write record(customer)customer$
   continue
eof:
   break
wend
data "BASIS","BASIS International Ltd.","+1.505.345.5232"
data "CHILE","Chile Company","+1.555.555.1212"

init:
sysgui = unt
open (sysgui)"X0"; rem ' ALIAS X0 SYSGUI

sysgui! = bbjapi().getSysGui()
window! = sysgui!.addWindow(100,100,280,170,"Customers",$00010003$,$00c00000$)
window!.addStaticText(101,10,10,80,30,"ID:",$8000$)
id! = window!.addEditBox(102,100,10,70,30,$$,$$)
window!.addStaticText(103,10,50,80,30,"Name:",$8000$)
name! = window!.addEditBox(104,100,50,170,30,$$,$$)
window!.addStaticText(105,10,90,80,30,"Phone:",$8000$)
phone! = window!.addEditBox(106,100,90,170,30,$$,$$)
update! = window!.addButton(201,10,130,80,30,"Update",$$)
delete! = window!.addButton(202,100,130,80,30,"Delete",$$)
clear! = window!.addButton(203,190,130,80,30,"Clear",$$)

id!.setText("BASIS")
id!.focus()
gosub fetch

id!.setCallback(id!.ON_EDIT_MODIFY,"toggle")
id!.setCallback(id!.ON_LOST_FOCUS,"fetch")
update!.setCallback(update!.ON_BUTTON_PUSH,"update")
delete!.setCallback(delete!.ON_BUTTON_PUSH,"remove")
clear!.setCallback(clear!.ON_BUTTON_PUSH,"clear")
window!.setCallback(window!.ON_CLOSE,"eoj")

process_events

eoj:
release

toggle:
    id$ = cvs(id!.getText(),7)
    update!.setEnabled(len(id$))
    delete!.setEnabled(len(id$))
return

fetch:
    id$ = pad(cvs(id!.getText(),7),6)
    if customer.id$ = id$ then return
    dim customer$:fattr(customer$)
    let customer.id$ = id$
    read record(customer,key=customer.id$,dom=notfound)customer$
notfound:
    gosub display
return

update:
    customer.id$ = ctrl(sysgui,102,1)
    customer.name$ = ctrl(sysgui,104,1)
    customer.phone$ = ctrl(sysgui,106,1)
    write record (customer)customer$
    i = msgbox("Customer "+customer.id$+" updated.",0,"Updated")
    gosub clear
return

remove:
    remove (customer,key=customer.id$,dom=nodelete)
    i = msgbox("Customer "+customer.id$+" deleted.",0,"Deleted")
nodelete:
    gosub clear
return

clear:
    dim customer$:fattr(customer$)
    gosub display
    id!.focus()
return

display:
    id!.setText(cvs(customer.id$,3))
    name!.setText(cvs(customer.name$,3))
    phone!.setText(cvs(customer.phone$,3))
return
