const requestFunc = async(url, method = "GET", data = null, token = null) => {
	apihost = 'http://127.0.0.1/api'
	method = method.toLocaleUpperCase()
	let fullurl = `${apihost}${url}`;
	let options = {
	method: method,
	headers: {
	"Content-Type": "application/json",
	"Authorization": `Bearer ${token}`,
	},
};

switch(method) {
	case "PUT":
	delete options.headers["Content-Type"];
	options.body = data;
	break;
	case "POST": case "PATCH": case "DELETE":
	options.body = JSON.stringify(data);
	break;
}

const res = await fetch(fullurl, options);
return await res.json();
};
var vueApp = new Vue({
        el: '#app',
        delimiters: ['${', '}'],
        data: {
            subjects:[],
            selectedRow: {
                "Name": "",
                "ID_professionalmodule": 0,
                "Shortname": "",
                "IDStype":0,
                "ID": ""
            }, 
         },
        methods: {
        	async getSubjects(){
        		this.subjects = await requestFunc("/subject/", "GET")
        	},
            selectRow(row, index){
                let rows = document.querySelectorAll('.clickHover')
                this.selectedRow = row;
                if(rows[index].style.backgroundColor != 'red'){
                    rows[index].style.backgroundColor = 'red'
                }
                else{
                    rows[index].style.backgroundColor = 'white'
                    this.clearSelectedRow()
                }   
            },
            clearSelectedRow(){
                this.selectedRow =   {
                "Name": "",
                "ID_professionalmodule": 0,
                "Shortname": "",
                "IDStype":0,
                "ID": ""
                }
            },
            changeRequestType(requestType){
                this.requestType = requestType 
            },
            modalClick(add, requestType) {
                if(add==='yes') {
                    this.clearSelectedRow();
                }
                this.changeRequestType(requestType);
            }         
            
        }, 
        mounted() {
        	this.getSubjects()
        }
    });

addSubject = (requestType) =>{
    let name = document.querySelector('.subjectName').value
    let shortName = document.querySelector('.shortName').value
    var select = document.getElementById("selectProfmodule");
    var value = select.value;
    let type = document.querySelector('.type').checked

    var myHeaders = new Headers();
    myHeaders.append("Content-Type", "application/json");

    var raw = {"Name":name,"ID_professionalmodule":Number(value),"Shortname": shortName, "IDStype": Number(type)};
    if (vueApp.requestType=== 'PATCH') {
            let ID = document.querySelector('.ID').value
            console.log(ID)
            raw.ID= Number(ID)
            
        }
    requestFunc(url="/subject/", method="POST", data=raw)
    alert("Добавлено")
}