class ClientInfoDoc(BaseDocument):
    def __init__(self, app):
        BaseDocument.__init__(self, app)
        self.manager = ClientManager(self.app)
        self.body.setAttribute('text', '#000000')
        self.body.setAttribute('background', 'Time-For-Lunch-2.jpg')

    def setID(self, clientid):
        cdata = self.manager.getClientInfo(clientid)
        cdata['addresses'].set_refobject('address', AddressRecord)
        self.current = clientid
        self.clear_body()
        #make header
        #self.header = ClientHeaderElement(cdata['client'])
        self.header = ClientTitleElement(cdata['client'])
        self.body.appendChild(self.header)
        #make main table
        self.mtable = BaseElement('table')
        self.mtable.appendChild(BaseElement('tr'))
        self.body.appendChild(self.mtable)
        #append contacts header
        conheader = ClientSectionHeader(self.current, 'contact', 'Contacts:')
        self.mtable.firstChild.appendChild(TextElement('td', conheader))
        self.contacts = ContactDoc(self.app)
        self.mtable.firstChild.appendChild(TextElement('td', self.contacts))
        #append locations header
        row = BaseElement('tr')
        locheader = ClientSectionHeader(self.current, 'location', 'Locations:')
        row.appendChild(TextElement('td', locheader))
        self.locations = LocationDoc(self.app)
        self.mtable.appendChild(row)
        row.appendChild(TextElement('td', self.locations))
        #insert the contacts, locations and tickets
        self.contacts.set_records(cdata['contacts'], action=None)
        self.locations.set_records(cdata['locations'], action=None)
        for node in self.contacts.records.values():
            node.setAttribute('bgcolor', 'DarkSeaGreen3')
        for node in self.locations.records.values():
            node.setAttribute('bgcolor', 'DarkSeaGreen3')
        self.records = cdata
        #append tag data
        #check for tag data
        tags = self.manager.getTags(clientid)
        row = BaseElement('tr')
        tagdoc = ClientTagTableElement(clientid, tags)
        row.appendChild(tagdoc)
        self.mtable.appendChild(row)
        newtrouble = Anchor('new.trouble.%s' % clientid, 'new trouble')
        
        self.body.appendChild(newtrouble)
class ClientInfoDoc(BaseDocument):
    def __init__(self, app):
        BaseDocument.__init__(self, app)
        self.manager = ClientManager(self.app)
        self.body.setAttribute('text', '#000000')
        self.body.setAttribute('background', 'Time-For-Lunch-2.jpg')

    def setID(self, clientid):
        cdata = self.manager.getClientInfo(clientid)
        self.current = clientid
        self.clear_body()
        #make header
        self.header = ClientHeaderElement(cdata['client'])
        self.body.appendChild(self.header)
        #append contacts header
        conheader = ClientSectionHeader(self.current, 'contact', 'Contacts:')
        self.body.appendChild(conheader)
        self.contacts = ContactTableElement()
        self.body.appendChild(self.contacts)
        #append locations header
        locheader = ClientSectionHeader(self.current, 'location', 'Locations:')
        self.body.appendChild(locheader)
        self.locations = LocationTableElement()
        self.body.appendChild(self.locations)
        #insert the contacts, locations and tickets
        self.appendLocations(cdata)
        self.appendContacts(cdata)

    def _appendRows(self, rows, element, addresses):
        for row in rows:
            row.addressid = AddressLink(self.db, addresses[row.addressid])
            element.appendRowElement(element, row)
            
    def _appendRowsOrig(self, ids, fields, table, idcol, element):
        if len(ids):
            clause = In(idcol, ids)
            for row in self.db.mcursor.select(fields=fields, table=table, clause=clause):
                row.addressid = AddressLink(self.db, row.addressid)
                element.appendRowElement(element, row)
            
    def appendLocations(self, data):
        self._appendRows(data['locations'], self.locations, data['addresses'])

    def appendContacts(self, data):
        self._appendRows(data['contacts'], self.contacts, data['addresses'])