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 BlogPost {
    //--------------------------
    // constructor
    //--------------------------
    constructor() {
        var folio = [];
        this.dataUtils = new DataUtils();
    }
    //--------------------------
    // methods
    //--------------------------
    start() {}

    submitPost(edit, uploadFiles) {
        let self = this;
        return new Promise(function(resolve, reject) {
            //collect form data
            //if(!this.validateForm())
            var postData = 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.
                postData.append('feature_image', file, file.name);
            }
            //var category = document.getElementById("content_category");
            //let project_form = document.forms.namedItem("folio-project");
            var txt = document.createElement("textarea");
            txt.innerHTML = document.getElementById('entry_text').innerHTML;
            console.log(txt);
            postData.append("title", document.getElementById('entry_title').value);
            postData.append('slug', new StringUtils().cleanString(document.getElementById('entry_title').value));
            postData.append("entry_plaintext", txt.value);
            postData.append("tags", document.getElementById('entry_tags').value);
            let postURL;
            let postEventType;
            if (edit) {
                let postID = document.getElementById('post-sumbit-btn').getAttribute('data-id');
                console.log("POST ID: "+postID);
                postURL = "/api/blog/update/" + postID;
                postEventType = DataEvent.POST_UPDATED;
                
            } else {
                postURL = "/api/blog/add";
                postEventType = DataEvent.POST_ADDED;
            }
            self.dataUtils.request(postURL, postEventType, REQUEST_TYPE_POST, CONTENT_TYPE_FORM, postData)
                .then((response) => {
                    resolve({
                        response
                    })
                }).catch((err) => {
                reject({
                    err
                });
            })
        });
    }

    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 { BlogPost as default }
