
<script>

function DisableUserButtonHtml(){return HomeMenuButtons('assets/disableuser.png', 'Disable', '','','45%', '45%','', buttononclick = 'DisableUser()', divclass = 'col-4','disableuserbuttondiv');}
function EnableUserButtonHtml(){return HomeMenuButtons('assets/enable-user.png', 'Enable', '','','45%', '45%','', buttononclick = 'EnableUser()', divclass = 'col-4','disableuserbuttondiv');}
$('#user_controls').append(DisableUserButtonHtml());
//$('#user_controls').append(DisableUserButtonHtml());
    function ChangeToEnableUserButton() {
      //  $('#disable_user_button').html('Enable<br>User');
     //   $('#disable_user_button').attr("class", "btn btn-warning btn-block");
     //   $('#disable_user_button').attr("onclick", "EnableUser()");
     //   $('#disable_user_button').show();
     $('#disableuserbuttondiv').remove();
     $('#user_controls').append(EnableUserButtonHtml());
    }
    function ChangeToDisableUserButton() {
     //   $('#disable_user_button').html('Disable<br>User');
     //   $('#disable_user_button').attr("class", "btn btn-danger btn-block");
    //    $('#disable_user_button').attr("onclick", "DisableUser()");
    //    $('#disable_user_button').show();
    $('#disableuserbuttondiv').remove();
    $('#user_controls').append(DisableUserButtonHtml());
    }

    




    function ModalDisableSuccess() {
        let modalid = "DisableUserSuccessModal";
        let modaltitle = 'Sucess';
        let modalbody = 'User is now Disabled';

        let modalfooter = ``;
        CreateAndShowModal(modalid, modaltitle, modalbody, modalfooter, modalfooterclose = true, topclosebutton = true, modalbodyclass = 'modal-body', modalheaderclass = 'modal-header');
    }

    function ModalDisableFailed(response) {
        let modalid = "DisableUserFailedModal";
        let modaltitle = 'Failed';
        let modalbody = 'Unable to Disable User'.response;

        let modalfooter = ``;
        CreateAndShowModal(modalid, modaltitle, modalbody, modalfooter, modalfooterclose = true, topclosebutton = true, modalbodyclass = 'modal-body bg-warning', modalheaderclass = 'modal-header');
    }

    function ModalEnableSuccess(){
        let modalid = "EnableUserSuccessModal";
        let modaltitle = 'Sucess';
        let modalbody = 'User is now Enabled';

        let modalfooter = ``;
        CreateAndShowModal(modalid, modaltitle, modalbody, modalfooter, modalfooterclose = true, topclosebutton = true, modalbodyclass = 'modal-body', modalheaderclass = 'modal-header');
    }
    function ModalEnableFailed(response){
        let modalid = "EnableUserFailedModal";
        let modaltitle = 'Failed';
        let modalbody = 'Unable to Enable User.<br>'.response;

        let modalfooter = ``;
        CreateAndShowModal(modalid, modaltitle, modalbody, modalfooter, modalfooterclose = true, topclosebutton = true, modalbodyclass = 'modal-body bg-warning', modalheaderclass = 'modal-header');
    }


    function EnableUserNow() {

        function TryToEnable(response) {
            $("#EnableUserConfirmationDialog").modal('hide');
            if (response===true) {
                ModalEnableSuccess();
            } else {
                ModalEnableFailed(response);
            }
            CheckIfUserActive();
            loaduserdetails();
        }

        AjaxDo('?admin/user/enable', { user_target: currenttarget }, TryToEnable, null, reqtype = 'POST');


    }


    function DisableUserNow() {

        function TryToDisable(response) {
            $("#DisableUserConfirmationDialog").modal('hide');
            if (response===true) {
                ModalDisableSuccess();
                ChangeToDisableUserButton();
            } else {
                ModalDisableFailed(response);
            }
            CheckIfUserActive();
            loaduserdetails();
        }

        AjaxDo('?admin/user/disable', { user_target: currenttarget }, TryToDisable, null, reqtype = 'POST');
    }
    function DisableUser() {

        let modalid = "DisableUserConfirmationDialog";
        let modaltitle = 'Disable User?';
        let modalbody = 'Are you sure you want to disable user?<br> The user would not be able to access his/her account?';

        let modalfooter = `<button type="button" class="btn btn-warning" data-dismiss="modal">Cancel</button>
    <button type="button" class="btn btn-danger" onclick="DisableUserNow()" id="request-credit-now">Continue</button>`;

        CreateAndShowModal(modalid, modaltitle, modalbody, modalfooter, modalfooterclose = false, topclosebutton = true, modalbodyclass = 'modal-body', modalheaderclass = 'modal-header');
    }

    function EnableUser() {

let modalid = "EnableUserConfirmationDialog";
let modaltitle = 'Enable User?';
let modalbody = 'Are you sure you want to Enable user?<br> The user would have access his/her account?';

let modalfooter = `<button type="button" class="btn btn-warning" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-danger" onclick="EnableUserNow()" id="request-credit-now">Continue</button>`;

CreateAndShowModal(modalid, modaltitle, modalbody, modalfooter, modalfooterclose = false, topclosebutton = true, modalbodyclass = 'modal-body', modalheaderclass = 'modal-header');
}



    function CheckIfUserActive() {

        function ChangeDisableButtonState(response) {
            console.log(response);
        if (response===true){ChangeToDisableUserButton();}
        else if(response===false){
            ChangeToEnableUserButton();
        }


            }

        AjaxDo('?admin/user/isactive', { user_target: currenttarget }, ChangeDisableButtonState, null, reqtype = 'POST');
    }

    $(document).ready(function () {
        CheckIfUserActive();



    });



</script>