import DataUtils, { REQUEST_TYPE_GET, REQUEST_TYPE_PUT, REQUEST_TYPE_POST, REQUEST_TYPE_DELETE, CONTENT_TYPE_JSON, CONTENT_TYPE_FORM } from '../tools/utilities/DataUtils.jsx';
import * as DataEvent from '../tools/events/DataEvent.jsx';
import StringUtils from '../tools/utilities/StringUtils.jsx';
class ProjectFolio {
    //--------------------------
    // constructor
    //--------------------------
    constructor() {
        var folio = [];
        this.dataUtils = new DataUtils();
    }
    //--------------------------
    // methods
    //--------------------------
    start() {}

    submitProject(edit, uploadFiles) {
        let self = this;
        return new Promise(function(resolve, reject) {
            //collect form data
            //if(!this.validateForm())
            var projectData = new FormData();
            //let projectImages = document.getElementById('projectImages');
            //var fileSelect = projectImages;
            var files = uploadFiles;
            for (var i = 0; i < files.length; i++) {
                var file = files[i];
                // Check the file type.
                if (!file.type.match('image.*')) {
                    continue;
                }
                // Add the file to the request.
                projectData.append('folioImages', file, file.name);
            }
            var category = document.getElementById("content_category");
            let project_form = document.forms.namedItem("folio-project");
            projectData.append("title", project_form.title.value);
            projectData.append('slug', new StringUtils().cleanString(project_form.title.value));
            projectData.append("tools", project_form.tools.value);
            projectData.append("url", project_form.url.value);
            projectData.append("description", project_form.description.value);
            projectData.append("type", type.options[type.selectedIndex].text);

            let postURL
            let postEventType
                
            if (edit) {
                let postID = document.getElementById('btn-submit').getAttribute('data-id');
                postURL = "/api/folio/update/" + postID;
                postEventType = DataEvent.PROJECT_UPDATED;
            } else {
                postURL = "/api/folio/add";
                postEventType = DataEvent.PROJECT_ADDED;
            }

            self.dataUtils.request(postURL, postEventType, REQUEST_TYPE_POST, CONTENT_TYPE_FORM, projectData)
                .then((response) => {
                    resolve({
                        response
                    })
                }).catch((err) => {
                reject({
                    err
                });
            })
        });
    }

    addNewProject(uploadFiles) {

        var request = new XMLHttpRequest();
        request.open("POST", "/folio", true);
        request.onload = function(oEvent) {
            if (request.status == 200) {
                let response = JSON.parse(request.response);
                if (response.message == "New Content Entered") {
                    console.log("SET");
                    project_form.reset();
                } else {
                    console.log(response.message);
                }
            } else {
                //console.log(request);
            }
        };
        request.send(projectData);
    }
    validateForm() {
        let valid = false;
        if (this.entry_form.title.value == "" || this.entry_form.price.value == "" || this.entry_form.description == "") {
            return valid;
        } else {
            valid = true;
            return valid;
        }
    }
//--------------------------
// event handlers
//--------------------------
}
export { ProjectFolio as default }
