<div id='NewPropertyFormContainer'>




</div>
<br><br><br>

<script>

    LoadDataPageFunc.PageTitle = 'New Property';
    InitDataPageFuncOBJ();
    Target_Uploaded_Files = [];
    LoadDataPageFunc = {};
    LoadDataPageFunc.NewProperty = {};
    LoadDataPageFunc.ids = {};
    var currentDropzone;
    var ongoingUploads = 0;
    var yesnomodalDecision = null;

    LoadDataPageFunc.NewPropertyValidate = function () {
        let name = $('#NewPropertyPropertyName').val();
        let description = $('#NewPropertyPropertyDescription').val();
        let status = $('#NewPropertyStatus').val() || 0;
        let remarks = $('#NewPropertyRemarks').val();
        let location = $('#NewPropertyLocation').val();
        let category = $('#NewPropertyCategory').val();
        let subcategory = $('#NewPropertySubCategory').val();
        let sqm = $('#NewPropertysqm').val();
        let bedrooms = $('#NewPropertyBedrooms').val();
        let rooms = $('#NewPropertyRooms').val();
        let toilet = $('#NewPropertyToilet').val();
        let kitchen = $('#NewPropertyKitchen').val();
        let floors = $('#NewPropertyFloors').val();
        let price = $('#NewPropertyPrice').val();

        if (!name || !description || !location || !sqm || !Target_Uploaded_Files.length) { return false; }
        return { name: name, description: description, status: status, remarks: remarks, location: location, category: category, subcategory: subcategory, sqm: sqm, bedrooms: bedrooms, rooms: rooms, toilet: toilet, kitchen: kitchen, floors: floors, photourl: JSON.stringify(Target_Uploaded_Files),price:price };
    };

    LoadDataPageFunc.UpdateModalDecision=function(type){
        $(document).ready(function () {
        yesnomodalDecision = type;
        modalhide("ModalYesNoWaitForFinishedUpload");
        $("#ModalYesNoWaitForFinishedUpload").remove();
        });
    };

    LoadDataPageFunc.TrytoSubmitNewProperty = function () {
        let ValidateData = LoadDataPageFunc.NewPropertyValidate();
        if (!ValidateData) {
            $('#IncompleteInputModal').remove();
            ModalQuickDismiss('Invalid/Incomplete', 'The following are required<br><br>Property Name<br>Description<br>Location<br>Size in SQM<br>At Least One Photo<br><br>Please Input the following properly if not done so. Thank you.','IncompleteInputModal');
            return false;
        }

        if (ongoingUploads) {
            yesnomodalDecision = null;
            $('#ModalYesNoWaitForFinishedUpload').remove();
            ModalYesNo('Photo Upload in Progress!', 'Some Files are not finished uploading.', 'LoadDataPageFunc.UpdateModalDecision(1);', 'Continue', 'LoadDataPageFunc.UpdateModalDecision(-1);', 'Wait', modalid = 'ModalYesNoWaitForFinishedUpload', modaltohide = '', functiontodo = '', conditiontrue = true);
            if (yesnomodalDecision === -1 || yesnomodalDecision === null) {
                hidemodal(modalid);
                $('#'+modalid).remove();
                return false;
            }
        }


        request.type('POST').data(ValidateData).url('/NewProperty/Form/submit').fromVarCache(false).success((response) => {

            if (response['success'] === true) {
                ModalQuickDismiss('Success', 'New Property Created', 'NewPropertysModalSuccess', modaltohide = '', function () {
                    ButtonGo('ViewPropertyDetails', response['data']);
                });
            } else if (response['success'] === true) {
                ModalQuickDismiss('Error', 'Unable to Submit. <br><br>' + response['error'], 'NewPropertysModalError');
            }
        }).error((errorstring) => {
            ModalQuickDismiss('Error', 'An Error Occured<br>' + errorstring + '<br><br>Please Try Again Later');
        }).go()
    };


    LoadDataPageFunc.ReplaceSelectOptionsfromServer = function (url, id, fromVarCache = true, type = 'POST') {
        if (!url || !id) { return false; }
        requestWHash.type(type).url(url).fromVarCache(fromVarCache).success((response) => {
            UIReplaceCurrentOptionsSelect(id, response);
        }).go();
    };



    if (typeof request === 'undefined') {
        var request = new RequestData(false);
        var requestWHash = new RequestData(true);
    }

    LoadDataPageFunc.ids.ClearPhotosButton = 'newpropertydropzoneclearbutton';

    LoadDataPageFunc.ShowPhotoClearButton = function () {
        $('#' + LoadDataPageFunc.ids.ClearPhotosButton).show();
    };

    LoadDataPageFunc.HidePhotoClearButton = function () {
        $('#' + LoadDataPageFunc.ids.ClearPhotosButton).hide();
    };

    



    LoadDataPageFunc.NewProperty.InitializePhotoDropZone = function () {
        Target_Uploaded_Files = [];
        currentDropzone = DropZoneFunc.InitializeDropZone('/File/Upload/Property', (response) => {
            if (response.length === 72) {
                Target_Uploaded_Files.push(response);
                LoadDataPageFunc.ShowPhotoClearButton();
            } else {
                currentDropzone.removeFile(currentDropzone.files[currentDropzone.files.length - 2]);
            }
        }, dropzoneid = 'newpropertydropzonephoto', DropZoneFunc.AcceptedFilesString.images, 'file', maxfilesize = 100);
    };

    LoadDataPageFunc.ClearPhotos = function () {
        DropZoneFunc.ClearDropzoneUpload();
        LoadDataPageFunc.HidePhotoClearButton();
    };

    LoadDataPageFunc.FormLoad = function () {
        ReusableUIElements.NewPropertyGenerateUI();
    };

    LoadDataPageFunc.Initialization = function () {
        LoadDataPageFunc.FormLoad();
        changeTopbarTitle(LoadDataPageFunc.PageTitle);
    };


    $(document).ready(function () {


        LoadDataPageFunc.Initialization();
        $('#' + LoadDataPageFunc.ids.ClearPhotosButton).hide();

        $('#' + LoadDataPageFunc.ids.ClearPhotosButton).off('click').on('click', function () {
            LoadDataPageFunc.ClearPhotos();
        });



        Preloaders.Datalist.NewPropertyCategory(function () { ReqCachetoDatalist('/Datalist/NewPropertyCategory', 'NewPropertyCategoryDataList', replace = true); });
        Preloaders.Datalist.NewPropertySubCategory(function () { ReqCachetoDatalist('/Datalist/NewPropertySubCategory', 'NewPropertySubCategoryDataList', replace = true); });
        

        LoadDataPageFunc.NewProperty.InitializePhotoDropZone();
        DropZoneFunc.ClearDropzoneUpload();

        ReqCachetoDatalist('/Datalist/NewPropertySubCategory', 'NewPropertySubCategory', replace = true);
        ReqCachetoDatalist('/Datalist/NewPropertyLocation', 'NewPropertyLocationDataList', replace = true);

    });




</script>