<?lsmb INCLUDE 'ui-header.html' ?>
<?lsmb PROCESS 'elements.html' ?>
<div id="modify_user">
    <font size="15">
        <strong>
            <?lsmb IF user.user.username ?>
                Editing User <?lsmb user.user.username ?>
            <?lsmb ELSE?>
                New User
            <?lsmb END?>
        </strong>
    </font>
    
    <form method="POST" action="admin.pl">
        <input type="hidden" name="action" value="<?lsmb IF user.user.username?>edit_user<?lsmb ELSE?>new_user<?lsmb END?>"/>
        <table>

            <?lsmb UNLESS user.user.username?>
            <tr>
                <td>Username</td>
                <td>
                    <input type="textarea" name="username" value="<?lsmb user.user.username?>"/>
                </td>
            </tr>
            <?lsmb END?>
            
            <tr>
                <td>
                    <select name="salutation">
                        <?lsmb FOREACH sal = salutations ?>
                            <option value="<?lsmb sal.id?>"><?lsmb sal.salutation?></option>
                        <?lsmb END?>
                    </select>
                </td>
            </tr>
            
            <tr>
                <td>First Name</td>
                <td>
                    <input type="textarea" name="first_name" value="<?lsmb user.employee.first_name?>"/>
                </td>
            </tr>
            
            <tr>
                <td>Last Name</td>
                <td>
                    <input type="textarea" name="last_name" value="<?lsmb user.employee.last_name?>"/>
                </td>
            </tr>
            <tr>
                <td>Employee #</td>
                <td>
                    <input type="textarea" name="employeenumber" value="<?lsmb user.employee.employeenumber?>"/>
                </td>
            </tr>
        </table>
        <input type="submit" value="Save User" />
    </form>
    <?lsmb IF user.user.username?>
        <form name="location" method="POST" action="admin.pl">
            <?lsmb IF location?>
            <input type="hidden" name="location_id" value="<?lsmb location.id?>"/>
            <?lsmb END?>
            <input type="hidden" name="action" value="save_location"/>
            <input type="hidden" name="user_id" value="<?lsmb user.user.id?>"/>
            <table>

                <tr>
                    <td colspan="8">
                        <hr/>
                        Location
                    </td>
                </tr>
            
                <tr>
                    <td></td>
                    <td>
                        Address
                    </td>
                    <td>
                        City
                    </td>
                    <td>
                        State/Province
                    </td>
                    <td>
                        Zip/Postal Code
                    </td>
                    <td>
                        Country
                    </td>
                </tr>
                
                <?lsmb FOR location IN user.locations ?>
                
                    <tr>
                        <td>
                            <a href="admin.pl?action=edit_location&amp;id=<?lsmb location.id?>">Edit</a>
                            <a href="admin.pl?action=delete_location&amp;id=<?lsmb location.id?>">Delete</a>
                        <td>
                        
                            <?lsmb location.line_one?>
                            <?lsmb location.line_two?>
                            <?lsmb location.line_three?>
                            </a>
                        </td>
                        <td>
                            <?lsmb location.city?>
                        </td>
                        <td>
                            <?lsmb location.state?>
                        </td>
                        <td>
                            <?lsmb location.zipcode?>
                        </td>
                         <td>
                            <?lsmb location.country?>
                         </td>
                    </tr>
                
                <?lsmb END?>
                <!-- The editable one. -->
                <tr>
                    <td></td>
                    <td>
                        <input type="textarea" name="address1" value="<?lsmb location.line_one?>"/>
                        <input type="textarea" name="address2" value="<?lsmb location.line_two?>"/>
                        <input type="textarea" name="address3" value="<?lsmb location.line_three?>" />
                    </td>
                    <td valign="top">
                        <input type="textarea" name="city" value="<?lsmb location.city?>"/>
                    </td>
                    <td valign="top">
                        <input type="textarea" name="state" value="<?lsmb location.state?>"/>
                    </td>
                    <td valign="top"> 
                        <input type="textarea" name="zipcode" value="<?lsmb location.zipcode?>"/>
                    </td>
                    <td valign="top">
			<?lsmb PROCESS select element_data = {
				name = "country"
				options = countries
				default_values = [location.country_id]
				value_attr = "id"
				text_attr = "name"
			} ?>
                    </td>
                </tr>
                <tr>
                    <td>
                    <?lsmb IF location?>
                    <input type="submit" value="Edit Location">
                    <input type="hidden" name="location_id" value="<?lsmb location.id?>"/>
                    <?lsmb ELSE?>
                    <input type="submit" value="Save Location">    
                    <?lsmb END?>
                    </td>
                </tr>
            </form>
        </table>

        
        
        <table>
            <form name="contacts" method="POST" action="admin.pl">
            <input type="hidden" name="action" value="save_contact"/>
            <input type="hidden" name="user_id" value="<?lsmb user.user.id?>"/>
            <tr>
                <td colspan="8"><hr/></td>
            </tr>
            
            <tr>
                <td></td>
                
                <td>
                    Contact type
                </td>
                <td>
                    Contact
                </td>
            </tr>
            
            <?lsmb FOR l_contact IN user.contacts?>
                
                <tr>
                    <td>
                        <a href="admin.pl?edit_contact&contact_id=<?lsmb l_contact.id?>&user_id=<?lsmb user.user.id?>">Edit</a>
                        <a href="admin.pl?delete_contact&contact_id=<?lsmb l_contact.id?>&user_id=<?lsmb user.user.id?>">Delete</a>
                    </td>
                    <td><?lsmb l_contact.class?></td>
                    <td><?lsmb l_contact.contact?></td>
                </tr>
            <?lsmb END?>
            <tr>
                <td></td>
                <td>
                    <?lsmb PROCESS select element_data = {
        				name = "contact_class"
        				options = contact_classes
        				default_values = [contact.contact_class_id]
        				value_attr = "id"
        				text_attr = "name"
        			} ?>
                </td>
                <td>
                    <input type="textarea" name="contact" value="<?lsmb contact.contact?>"/>
                </td>
            </tr>
            <tr>
                <td>
                    <?lsmb IF contact?>
                    <input type="submit" value="Edit Contact">
                    <input type="hidden" name="contact_id" value="<?lsmb contact.id?>"/>
                    <?lsmb ELSE?>
                    <input type="submit" value="Save Contact">
                    <?lsmb END?>
                </td>
            </tr>
            </form>
        </table>
        <form name="groups" method="POST" action="admin.pl">
            <input type="hidden" name="action" value="save_groups"/>
            <table>
                <!-- Groups section -->
                <tr>
                <?lsmb FOREACH role IN roles ?>
                <?lsmb IF loop.count % 2 == 0?>
                </tr>
                <tr>
                <?lsmb END?>
                    <td>
                        <input type="checkbox" name="<?lsmb loop.index?>" value="1" 
                            <?lsmb FOREACH rolname IN user.roles ?>
                                <?lsmb IF role == rolname?>
                                    checked
                                <?lsmb END?>
                            <?lsmb END?> />
                        <?lsmb role?>
                    </td>
                <?lsmb END?>
                </tr>
            </table>
        
            <table>
                <tr>
                    <td><button value="groups">Save Groups</button></td>
                    <td><button name="method" value="cancel">Cancel</td>
                </tr>
            </table>
        </form>   
<?lsmb END?>
</div>