<template>
    <modal :id="id" tabindex="-1" role="dialog" :aria-labelledby="id+'Label'">
        <h5 slot="header" class="modal-title" :id="id+'Label'">
            Edit Credential
        </h5>
        <div slot="body">
            <div class="form">
                <autocomplete-custom-dropdown :options="descriptions" :value="creddesc" v-model="creddesc" :allow-custom="true"></autocomplete-custom-dropdown>
            </div>
            <div class="form-group">
                <label for="newusername">Username</label>
                <input type="text" :name="'newusername'+data.credid" :id="'newusername'+data.credid" class="form-control credential" v-model="data.creduser">
            </div>
            <div class="form-group">
                <label for="newpassword">Password</label>
                <input type="text" :name="'newpassword'+data.credid" :id="'newpassword'+data.credid" class="form-control credential" v-model="data.credpass">
                <button type="button" class="btn btn-secondary" @click="getRandomPassword(data.credid)">Generate Random</button>
            </div>
            <div class="form-group"></div>
        </div>
        <div slot="footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary" @click="updateCredential()">Save</button>
        </div>
    </modal>
</template>
<script>
export default {
    props: ['populateWith', 'modalId', 'descriptions'],
    data() {
        return {
            data: JSON.parse(JSON.stringify(this.populateWith)),
            creddesc: {},
            id: this.modalId,
            errors: []
        }
    },
    mounted () {
        // Definitely needs to be refactored into something that runs once
        // per page instead of once per credential. Only putting this here now
        // to test something else.
        this.creddesc = {
            'id': this.data.credtype,
            'name': this.data.creddesc
        }
    },
    methods: {
        rndStr(len) {
            let text = ""
            let chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%"
        
            for( let i=0; i < len; i++ ) {
                text += chars.charAt(Math.floor(Math.random() * chars.length))
            }
            return text
        },
        getRandomPassword(id) {
            this.data.credpass = this.rndStr(16);
        },
        updateCredential() {
            console.log(this.creddesc)
            if (this.creddesc.id === "custom") {
                this.data.credtype = 1
                this.data.creddesc = this.creddesc.name
            }
            else {
                this.data.credtype = this.creddesc.id
                this.data.creddesc = this.creddesc.name
            }
            axios.put('/api/credentials/' + this.data.credid, this.data).then(response => {}).catch(error =>{console.error(error)});
            $('#credential'+this.data.credid+'editModal').modal('hide');
        }
    }
    
}
</script>