<br><br><br><br>
<div class="card" id="secondary-card" style="display:none;">
    <div class="card-header ui-sortable-handle" style="cursor: move;">
        <h3 class="card-title">Credit Requests</h3>
        <div class="card-tools"></div>
    </div>
    <div class="card-body">

        <table id="Credit_Requests_list_table">
            <thead>
                <tr>
                    <th>From</th>
                    <th>User</th>
                    <th>Request<br>Date</th>
                    <th>Request<br>Amount</th>
                    <th>ACTION</th>
                </tr>
            </thead>
            <tbody>

            </tbody>
        </table>


    </div>
</div>





<script>
    function ApproveCreditRequestSuccessModal(Number, amount) {
        const modalid = "modal-approve-credit_request-success";
        const modaltitle = "Success";
        const modalbody = `
    <p>The following Credit Request is approved.</p><br>`+ CreditRequestMessageSpecificAmmountsandRequestor(Number, amount);


        const modalfooter = `<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>`;

        CreateAndShowModal(modalid, modaltitle, modalbody, modalfooter, modalfooterclose = false, topclosebutton = true, modalbodyclass = 'modal-body', modalheaderclass = 'modal-header');
        $('#modal-approve-credit_request-confirmation').modal('hide');
        ReloadPage();
    }


    function ApproveCreditRequestFailedModal(Number, amount,response) {
        if (!response){response='';}
        const modalid = "modal-approve-credit_request-failed";
        const modaltitle = "Error";
        const modalbody = `
    <p>Error! The following Credit request was not approved. Please try again later.</p><br>`+ CreditRequestMessageSpecificAmmountsandRequestor(Number, amount)+
`<br><br>`+response;


        const modalfooter = `<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>`;

        CreateAndShowModal(modalid, modaltitle, modalbody, modalfooter, modalfooterclose = false, topclosebutton = true, modalbodyclass = 'modal-body', modalheaderclass = 'modal-header');
        $('#modal-approve-credit_request-confirmation').modal('hide');
        ReloadPage();
    }



    function ApproveCreditRequest(Number, amount, hashkey) {
        function ApproveCreditRequestNow(response) {
            if (response === true) {
                ApproveCreditRequestSuccessModal(Number, amount);
            } else if (response === false) {
                ApproveCreditRequestFailedModal(Number, amount);
            } else {
                ApproveCreditRequestFailedModal(Number, amount,response);
            }
        }


        AjaxDo('?admin/advanced/credit_request/approve', {
            target_hash: hashkey
        }, ApproveCreditRequestNow, null, reqtype = 'POST');
    }



    function RejectCreditRequestSuccessModal(Number, amount) {
        const modalid = "modal-reject-credit_request-success";
        const modaltitle = "Success";
        const modalbody = `
    <p>The following Credit Request is Denied.</p><br>` + CreditRequestMessageSpecificAmmountsandRequestor(Number, amount);


        const modalfooter = `<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>`;

        CreateAndShowModal(modalid, modaltitle, modalbody, modalfooter, modalfooterclose = false, topclosebutton = true, modalbodyclass = 'modal-body', modalheaderclass = 'modal-header');
        $('#modal-reject-credit_request-confirmation').modal('hide');
        ReloadPage();
    }


    function RejectCreditRequestFailedModal(Number, amount) {
        const modalid = "modal-reject-credit_request-failed";
        const modaltitle = "Failed";
        const modalbody = `
    <p>Error! The following Credit Request was not Denied. Please try again later</p><br>` + CreditRequestMessageSpecificAmmountsandRequestor(Number, amount);


        const modalfooter = `<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>`;

        CreateAndShowModal(modalid, modaltitle, modalbody, modalfooter, modalfooterclose = false, topclosebutton = true, modalbodyclass = 'modal-body', modalheaderclass = 'modal-header');
        $('#modal-reject-credit_request-confirmation').modal('hide');
        ReloadPage();

    }


    function RejectCreditRequest(Number, amount, hashkey) {
        function RejectCreditRequestNow(response) {
            if (response === true) {
                RejectCreditRequestSuccessModal(Number, amount);
            } else if (response === false) {
                RejectCreditRequestFailedModal(Number, amount);
            }
        }

        AjaxDo('?admin/advanced/credit_request/reject', {
            target_hash: hashkey
        }, RejectCreditRequestNow, null, reqtype = 'POST');





    }

    function CreditRequestMessageSpecificAmmountsandRequestor(Number, amount) {
        return ` Requested By : ${Number}<br>
    Amount&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp &nbsp: ${amount}<br>`;
    }

    function ApproveCreditRequestModal(Number, amount, Hashkey) {
        const modalid = "modal-approve-credit_request-confirmation";
        const modaltitle = "Approve";
        const modalbody = `
    <p>Are You sure you want to approve the credit request?</p><br>` +
            CreditRequestMessageSpecificAmmountsandRequestor(Number, amount);
        const modalfooter = `<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button"  class="btn btn-primary" onclick="ApproveCreditRequest('${Number}',${amount},'${Hashkey}');" id="approve-credit-request">Continue</button>`;

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

    }

    function RejectCreditRequestModal(Number, amount, Hashkey) {

        const modalid = "modal-reject-credit_request-confirmation";
        const modaltitle = "Reject";
        const modalbody = `
    <p>Are you sure you want to deny the credit request?</p><br>` +
            CreditRequestMessageSpecificAmmountsandRequestor(Number, amount);

        const modalfooter = `<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button"  class="btn btn-primary" onclick="RejectCreditRequest(${Number},${amount},'${Hashkey}');" id="approve-credit-request">Continue</button>`;

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

    }



    function GenerateCreditRequestDetailsRow(From,To, date, amount, status, Hashkey) {
        let activecheckbox = '';

        const approvebutton = `<button class="btn btn-warning btn-block" onclick="ApproveCreditRequestModal('${To}',${amount},'${Hashkey}')">Approve</button>`;
        const rejectbutton = `<button class="btn btn-danger btn-block" onclick="RejectCreditRequestModal('${To}',${amount},'${Hashkey}')">Reject</button>`;

        if (status === 0) {
            actionbutton = approvebutton + rejectbutton;
        } else if (status === -1) {
            actionbutton = 'Denied';
        } else if (status === 1) {
            actionbutton = 'Approved';
        }else if (status === 2) {
            actionbutton = 'Directly Sent by You.';
        }

        return `<tr>
    <td>${From}</td>
      <td>${To}</td>
      <td>${date}</td>
      <td>${amount}</td>
      <td>${actionbutton}</td>
  </tr>`;
    }


    async function GenerateCreditRequestRows(responsearray) {
        //console.log(responsearray);
        $("#Credit_Requests_list_table").find("tbody").html('');
        let newhtmltable = '';

        for (let i = 0; i < responsearray.length; i++) {


            newhtmltable += GenerateCreditRequestDetailsRow(responsearray[i]['creditor'],responsearray[i]['target_user'], responsearray[i]['request_date'], responsearray[i]['amount'], responsearray[i]['status'], responsearray[i]['hashkey']);

        }

        //$("#User_list_table").find("tbody").replaceWith(newhtmltable);
        // new DataTable('#User_list_table');


        $("#Credit_Requests_list_table").find("tbody").html(newhtmltable);
        $("#Credit_Requests_list_table").DataTable({
            "destroy": true,
            order: [
                [1, 'desc']
            ],
            pageLength: 5,
            lengthMenu: [
                [5],
                [5]
            ]
        })


        $('#secondary-card').fadeIn(200);


    }


    function GenerateCreditRequestTable() {
        AjaxDo('?admin/list/credit_requests', null, GenerateCreditRequestRows, null, reqtype = 'POST');
    }


    GenerateCreditRequestTable();
</script>