let u = new URL(location.href)
urlPrivateAPI = u.origin + '/api'

api = new API({
    url: urlPrivateAPI,
    timeout: 10000, // 10 sec
})

Handball = 2

document.addEventListener('DOMContentLoaded',  () => {
    let lastTeamID;

    let zoneSelect = new Select({
        field: document.getElementById('zone'),
        key: 'zoneID',
        name: 'name'
    })

    zoneSelect.on('select', zoneID => {
        aliasesModel.clear()

        api.req({
            func: 'listTeamsByZone',
            data: zoneID,
            onSuccess: resp => {
                teamSelect.setList(resp)
            }
        })

        lastTeamID = ''

        let obj = f.getFormData()
        obj.teamID = ''

        f.setObj(obj)
    })

    let teamSelect = new Select({
        field: document.getElementById('team'),
        key: 'teamID',
        name: 'canonicalName'
    })

    teamSelect.on('select', teamID => {
        aliasesModel.load({
            data: teamID,
        })
        /*
        api.req({
            func: 'listFootballTeamAliases',
            data: teamID,
            onSuccess: resp => {
                //aliasesModel.setList(resp)
                
            }
        })
        */

       lastTeamID = teamID

        let obj = f.getFormData()
        obj.teamID = teamID

        f.setObj(obj)
    })

    let aliasesModel = new ListModel({
        template: {
            id: 'listItem'
        },
        key: 'aliasID',
        container: 'aliases',
        btnRemove: 'removeAlias',
        ajax: {
            api: api,
            listFunc: 'listTeamAliases',
            removeFunc: 'removeTeamAlias'
        }
    })

    let f = new Formobj({
        form: 'add',
        schema: {
            name: 'str',
            teamID: 'str'
        },
        validate: obj => {
            if (obj.teamID == '') {
                return {
                    message: 'Не выбрана команда!'
                }
            }
        },
        api: api,
        submitFunc: 'addTeamAlias',
        editable: true,
        onSubmitResult: () => {
            aliasesModel.load({
                data: lastTeamID
            })
        }
    })

    api.req({
        func: 'listTeamZones',
        data: Handball,
        onSuccess: resp => {
            zoneSelect.setList(resp)
        }
    })
})