<div id='ListMainContainer'>
</div>



<script>
    Target_Uploaded_Files = [];

    defaultBackOnclick = null;
    if (typeof LoadDataPageFunc === 'undefined') {
        LoadDataPageFunc = {};
    }

    if (typeof LoadDataPageFunc.Settings === 'undefined') {
        LoadDataPageFunc.Settings = {};
    }

    LoadDataPageFunc.Settings.PageName = 'Photos';
    LoadDataPageFunc.currentTargetPage = 'ViewAllPhotos';

    LoadDataPageFunc.URLs = {};

    if (typeof currenttarget === 'string') {
        currenttarget = tryparsingJSON(currenttarget);
    }

    if (typeof currenttarget === 'object') {
        LoadDataPageFunc.Settings.Phototype = currenttarget['type'];
        targethash = currenttarget['t'];
    } else {
        targethash = currenttarget;
    }

    if (typeof LoadDataPageFunc.Settings.Phototype === 'undefined') {
        LoadDataPageFunc.Settings.Phototype = 'ProductMarket';
    }

    LoadDataPageFunc.URLs.QueryListData = '/Request/Photos/' + LoadDataPageFunc.Settings.Phototype;
    LoadDataPageFunc.Settings.CurrentTargetRequired = true;
    LoadDataPageFunc.Settings.DefaultDatatoSend = null;


    LoadDataPageFunc.NewRow = function (url, hashkey) {
        if (!hashkey || !url) { return false; }
        return [url, '', 'PhotoViewer', hashkey];
    };




    LoadDataPageFunc.Settings = {};
    LoadDataPageFunc.Settings.PageName = 'List';
    LoadDataPageFunc.Settings.DefaultCardNoDetailsMessage = 'No Data';
    LoadDataPageFunc.Settings.DefaultRequestType = 'POST';
    LoadDataPageFunc.Settings.fromVarCache = true;
    LoadDataPageFunc.Settings.SortArray = function (arr) {
        return arr.sort((a, b) => new Date(b.created) - new Date(a.created));
    };


    LoadDataPageFunc.ids = {};
    LoadDataPageFunc.ids.MainContainer = 'ListMainContainer';

    LoadDataPageFunc.ids.ListContainer = 'ListContainer';
    LoadDataPageFunc.ids.HashKeyContainer = 'ListRowCardHash';
    LoadDataPageFunc.ids.MainCardBody = 'MAINCARDBODY_LIST';








    $$$ = {};
    $$$.UpdateMainContainer = function (html) {
        $('#' + LoadDataPageFunc.ids.MainContainer).html(html);
    };

    LoadDataPageFunc.Settings = {};
    LoadDataPageFunc.Settings.ViewDetailsOnclick = function (data) {
        return `ButtonGo('ViewDetails', '${data}')`;
    };


    LoadDataPageFunc.CheckCachefromURLandChangeURLToBlob = function (photourl) {
        if (!photourl) { return ''; }
        photoblob = reqcacheload(photourl);
        if (photoblob) {
            photourl = URL.createObjectURL(photoblob);
        }
        return photourl;
    }




    LoadDataPageFunc.URLs.PreloadPhoto = function (hash) {
        return '/RequestData/File/' + hash;
    };




    LoadDataPageFunc.populatelist = function () {

        let urlArrays = []; let photoPromises = [];
        let newhtmlrows = ''; let htmlarrayrows = [];

        let PhotolistHashToHTML = function (PhotoHashArray) {

            if (typeof PhotoHashArray === 'string') { PhotoHashArray = JSON.parse(PhotoHashArray); }
            let count = PhotoHashArray.length;

            for (let dd = 0; dd < count; dd++) {
                photoPromises.push(
                    LoadAndCreateURLfromFileHash(PhotoHashArray[dd]).then(bloburl => {
                        urlArrays.push(bloburl);
                    })
                );
            }

            Promise.all(photoPromises).then(() => {

                for (let aa = 0; aa < count; aa++) {
                    if (!PhotoHashArray[aa] || !urlArrays[aa]) { continue; }
                    htmlarrayrows.push(LoadDataPageFunc.NewRow(urlArrays[aa], PhotoHashArray[aa]));
                }


                newhtmlrows = UIServices_FullDIV_GOTOPAGE_Array('', viewallhref = '', viewallonclick = '', viewalltext = '', htmlarrayrows);


                $('#' + LoadDataPageFunc.ids.ListContainer).html(newhtmlrows);
                const elementsWithIconBox = document.querySelectorAll('.icon-box');
                elementsWithIconBox.forEach(element => {
                    element.classList.remove('icon-box');
                });

            }).catch(error => {
                $('#' + LoadDataPageFunc.ids.ListContainer).html('Error Loading Photos');
            });

        };



        if (LoadDataPageFunc.Details && LoadDataPageFunc.Details.photourl) {
            let photoarray = LoadDataPageFunc.Details.photourl;
            let PhotolistCount = photoarray.length;
            if (!PhotolistCount) { return false; }
            PhotolistHashToHTML(LoadDataPageFunc.Details.photourl);

            return true;
        }



        let request = new RequestData(true);
        request
            .url(LoadDataPageFunc.URLs.QueryListData)
            .type(LoadDataPageFunc.Settings.DefaultRequestType)
            .data({ target: targethash })
            .fromVarCache(true)
            .success((response) => {


                if (!response) {
                    $('#card-body-' + LoadDataPageFunc.ids.MainCardBody).html(LoadDataPageFunc.Settings.DefaultCardNoDetailsMessage);
                    return;
                }

                let List = response;




                const count = List.length;



                for (let i = 0; i < count; i++) {
                    // Push each promise into the array
                    photoPromises.push(
                        LoadAndCreateURLfromFileHash(List[i]).then(bloburl => {
                            urlArrays.push(bloburl);
                        })
                    );
                }

                Promise.all(photoPromises).then(() => {

                    for (let i = 0; i < count; i++) {
                        if (!List[i] || !urlArrays[i]) { continue; }
                        htmlarrayrows.push(LoadDataPageFunc.NewRow(urlArrays[i], List[i]));
                    }


                    newhtmlrows = UIServices_FullDIV_GOTOPAGE_Array('', viewallhref = '', viewallonclick = '', viewalltext = '', htmlarrayrows);


                    $('#' + LoadDataPageFunc.ids.ListContainer).html(newhtmlrows);
                    const elementsWithIconBox = document.querySelectorAll('.icon-box');
                    elementsWithIconBox.forEach(element => {
                        element.classList.remove('icon-box');
                    });

                }).catch(error => {
                    $('#' + LoadDataPageFunc.ids.ListContainer).html('Error Loading Photos');
                });



            })
            .error((err) => {
                console.error(err);
            })
            .go();

    };

    LoadDataPageFunc.ListContainerMessage = function (message = 'No Photos') {

        ListContainer = '<div id="' + LoadDataPageFunc.ids.ListContainer + '"><center>' + message + '</center></div>';
        $$$.UpdateMainContainer(UICardSimple('', ListContainer, LoadDataPageFunc.ids.MainCardBody));

    };





    LoadDataPageFunc.main = function () {
        if (LoadDataPageFunc.Settings.CurrentTargetRequired && (!targethash || targethash === '0')) {
            $$$.UpdateMainContainer('<center>No Target<br><br>' + buttonprimary('Go Back', '', "Backkey()") +
                '<br><br>' + buttonprimary('Home', '', "ButtonGo('Home', '')") + '</center>'); return false;
        }



        LoadDataPageFunc.ListContainerMessage('Loading Please Wait...');



        return true;

    };

    $(document).ready(function () {

        LoadDataPageFunc.main();
        LoadDataPageFunc.populatelist();
        changeTopbarTitle(LoadDataPageFunc.Settings.PageName);


    });
</script>