rem ' Customer master file maintenance (Character 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:
print 'window'(10,10,55,9,"Customers")
print 'sb',
print @(6,1),"ID:"
print @(4,2),"Name:"
print @(3,3),"Phone:"
print 'cf',

id$="BASIS"
gosub fetch

while 1
id:
    print @(0,5),'cl',"Enter ID, F4 = End"
    input @(10,1),'uc',id$,'lc'
    if ctl=2 or ctl=3 then goto id
    if ctl=4 then break
    if id$="" then id$=customer.id$
    gosub fetch
name:
    print @(0,5),'cl',"Enter Name, F3 = Back, F4 = End"
    input @(10,2),name$
    if ctl=2 then goto name
    if ctl=3 or ctl=4 then goto id
    if len(cvs(name$,3)) then customer.name$ = name$
    print @(10,2),name$
phone:
    print @(0,5),'cl',"Enter Phone, F3 = Back, F4 = End"
    input@(10,3),phone$
    if ctl=2 then goto phone
    if ctl=3 then goto name
    if ctl=4 then goto id
    if len(cvs(phone$,3)) then customer.phone$ = phone$
    print @(10,3),phone$
action:
    input @(0,5),'cl',"F1 = Update, F2 = Delete, F3 = Back, F4 = End: ",*
    if ctl<2 then gosub update; continue
    if ctl=2 then gosub remove; continue
    if ctl=3 then goto phone
    if ctl=4 then break
wend

release

clear:
    dim customer$:fattr(customer$)
    gosub display
return

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

display:
    print 'cf',
    print @(10,1),cvs(customer.id$,3)
    print @(10,2),cvs(customer.name$,3)
    print @(10,3),cvs(customer.phone$,3)
return

update:
    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
