(function ($) {

    $.fn.dataTable.ext.errMode = 'throw';

    $(document).ready(function () {
        var storage = $('#transactionlist').DataTable({
            dom: '<"row"<"col-sm-12"rt>><"row"<"col-sm-3"l><"col-sm-3"i><"col-sm-6"p>><"clear">',
            processing: true,
            serverSide: true,
            ordering: true,
            order: [[0, 'desc']],
            columnDefs: [{ orderable: false, targets: 'no-sort' }],
            language: {
                "processing": "<div class='table-loading'></div>"
            },
            ajax: {
                url: __rCore.datatable.transaction,
                method: 'POST',
                data: function (d) {
                    var data = $('#__aclSearchTransaction').serializeArray();
                    $.each(data, function (i, v) {
                       d[v.name] = v.value;
                    });
                }
            }
        });

        $('.input-daterange').datepicker({
            format: "d M yyyy",
            clearBtn: true,
            autoclose: true,
            endDate: "+1d",
            todayHighlight: true,
            zIndexOffset: 13
        });

        $('#__aclSearchTransaction').submit(function (e) {
           e.preventDefault();
           storage.draw();
        });

        $('body').on('click', 'a.aclpay.bkash', function(e){
            e.preventDefault();
            var reference = $(this).attr('acl-pay');
            var payable = $(this).parent().parent().find('td:nth-child(6)').text();
            var fund = $(this).parent().parent().find('td:nth-child(5)').text();
            var client = $(this).parent().parent().find('td:nth-child(3)').text();

            if(reference && reference.length >0) {
                $.confirm({
                    title: 'Paying Transaction',
                    content: '' +
                        '<table class="table table-bordered table-striped">' +
                        '<tbody>' +
                        '<tr>' +
                        '<td>Client</td><td>'+client+'</td>' +
                        '</tr>' +
                        '<tr>' +
                        '<td width="30%">Reference</td><td>'+reference+'</td>' +
                        '</tr>' +
                        '<tr>' +
                        '<td>Fund</td><td>'+fund+'</td>' +
                        '</tr>' +
                        '<tr>' +
                        '<td>Amount</td><td>'+payable+'</td>' +
                        '</tr>' +
                        '</tbody>' +
                        '</table>' +
                        '<form action="javascript:void(0)">' +
                        '<div class="form-group">' +
                        '<label>bKash TrxId</label>' +
                        '<input type="text" placeholder="type N/A if not required" class="trxid form-control" required />' +
                        '</div>' +
                        '<div class="form-group">' +
                        '<label>Mobile Number</label>' +
                        '<input type="text" placeholder="type N/A if not required" class="bmobile form-control" required />' +
                        '</div>' +
                        '</form>',
                    buttons: {
                        formSubmit: {
                            text: 'Pay',
                            btnClass: 'btn-green',
                            action: function () {
                                var trxid = this.$content.find('.trxid').val();
                                var bmobile = this.$content.find('.bmobile').val();
                                var self = this;
                                if(!trxid && !bmobile){
                                    $.alert('TrxId and Mobile Number is required');
                                    return false;
                                }
                                $.ajax({
                                    url: __rCore.ajax,
                                    dataType: 'json',
                                    method: 'post',
                                    data : {
                                        context : __rCore.context.tbp,
                                        data: {
                                            trxid : trxid,
                                            reference : reference,
                                            mobile : bmobile
                                        }
                                    },
                                    beforeSend : function () {
                                        self.showLoading(true);
                                    }
                                }).done(function (response) {
                                    if(response.state) {
                                        self.close();
                                        swal("Successful", response.message ? response.message : 'Transaction paid successfully', "success");
                                        storage.draw(false);
                                    } else {
                                        self.hideLoading(true);
                                        $.alert( response.message ? response.message : 'Error Occurred. Please Try Again' );
                                    }
                                }).fail(function(){
                                    self.hideLoading(true);
                                    $.alert('Error Occurred. Please Try Again');
                                });

                                return false;
                            }
                        },
                        cancel: function () {

                        },
                    },
                    onContentReady: function () {
                        var jc = this;
                        this.$content.find('form').on('submit', function (e) {
                            e.preventDefault();
                            jc.$$formSubmit.trigger('click');
                        });
                    }
                });
            }
        });

        $('body').on('click', 'a.aclcancel.transaction', function (e) {
            e.preventDefault();
            var reference = $(this).attr('acl-cancel');
            var payable = $(this).parent().parent().find('td:nth-child(6)').text();
            var fund = $(this).parent().parent().find('td:nth-child(5)').text();
            var client = $(this).parent().parent().find('td:nth-child(3)').text();


            if(reference && reference.length >0) {
                $.confirm({
                    title: 'Are you sure ?',
                    content: '' +
                        '<table class="table table-bordered table-striped">' +
                        '<tbody>' +
                        '<tr>' +
                        '<td>Client</td><td>'+client+'</td>' +
                        '</tr>' +
                        '<tr>' +
                        '<td width="30%">Reference</td><td>'+reference+'</td>' +
                        '</tr>' +
                        '<tr>' +
                        '<td>Fund</td><td>'+fund+'</td>' +
                        '</tr>' +
                        '<tr>' +
                        '<td>Amount</td><td>'+payable+'</td>' +
                        '</tr>' +
                        '</tbody>' +
                        '</table>' +
                        '<p>You are going to cancel the transaction. Press Yes to Cancel or No to Dismiss</p>',
                    buttons: {
                        paytransaction: {
                            text: 'YES',
                            btnClass: 'btn-green',
                            action: function () {
                                var self = this;
                                $.ajax({
                                    url: __rCore.ajax,
                                    dataType: 'json',
                                    method: 'post',
                                    data : {
                                        context : __rCore.context.tca,
                                        data: {
                                            reference : reference,
                                        }
                                    },
                                    beforeSend : function () {
                                        self.showLoading(true);
                                    }
                                }).done(function (response) {
                                    if(response.state) {
                                        self.close();
                                        swal("Successful", response.message ? response.message : 'Transaction cancelled successfully', "success");
                                        storage.draw(false);
                                    } else {
                                        self.hideLoading(true);
                                        $.alert( response.message ? response.message : 'Error Occurred. Please Try Again' );
                                    }
                                }).fail(function(){
                                    self.hideLoading(true);
                                    $.alert('Error Occurred. Please Try Again');
                                });

                                return false;
                            }
                        },
                        cancel: {
                            text : 'NO',
                            action : function () {

                            }
                        },
                    }
                });
            }

        });

        $('body').on('click', 'a.aclrollback.transaction', function (e) {
            e.preventDefault();
            var reference = $(this).attr('acl-rollback');
            var payable = $(this).parent().parent().find('td:nth-child(6)').text();
            var fund = $(this).parent().parent().find('td:nth-child(5)').text();
            var client = $(this).parent().parent().find('td:nth-child(3)').text();


            if(reference && reference.length >0) {
                $.confirm({
                    title: 'Are you sure ?',
                    content: '' +
                        '<table class="table table-bordered table-striped">' +
                        '<tbody>' +
                        '<tr>' +
                        '<td>Client</td><td>'+client+'</td>' +
                        '</tr>' +
                        '<tr>' +
                        '<td width="30%">Reference</td><td>'+reference+'</td>' +
                        '</tr>' +
                        '<tr>' +
                        '<td>Fund</td><td>'+fund+'</td>' +
                        '</tr>' +
                        '<tr>' +
                        '<td>Amount</td><td>'+payable+'</td>' +
                        '</tr>' +
                        '</tbody>' +
                        '</table>' +
                        '<p>You are going to unpaid the transaction again. Press Yes to Unpaid or No to Dismiss</p>',
                    buttons: {
                        paytransaction: {
                            text: 'YES',
                            btnClass: 'btn-green',
                            action: function () {
                                var self = this;
                                $.ajax({
                                    url: __rCore.ajax,
                                    dataType: 'json',
                                    method: 'post',
                                    data : {
                                        context : __rCore.context.tua,
                                        data: {
                                            reference : reference,
                                        }
                                    },
                                    beforeSend : function () {
                                        self.showLoading(true);
                                    }
                                }).done(function (response) {
                                    if(response.state) {
                                        self.close();
                                        swal("Successful", response.message ? response.message : 'Transaction unpaid successfully', "success");
                                        storage.draw(false);
                                    } else {
                                        self.hideLoading(true);
                                        $.alert( response.message ? response.message : 'Error Occurred. Please Try Again' );
                                    }
                                }).fail(function(){
                                    self.hideLoading(true);
                                    $.alert('Error Occurred. Please Try Again');
                                });

                                return false;
                            }
                        },
                        cancel: {
                            text : 'NO',
                            action : function () {

                            }
                        },
                    }
                });
            }

        });

        $('body').on('click', 'a.aclexpired.transaction', function (e) {
            e.preventDefault();
            var reference = $(this).attr('acl-expired');
            var payable = $(this).parent().parent().find('td:nth-child(6)').text();
            var fund = $(this).parent().parent().find('td:nth-child(5)').text();
            var client = $(this).parent().parent().find('td:nth-child(3)').text();


            if(reference && reference.length >0) {
                $.confirm({
                    title: 'Are you sure ?',
                    content: '' +
                        '<table class="table table-bordered table-striped">' +
                        '<tbody>' +
                        '<tr>' +
                        '<td>Client</td><td>'+client+'</td>' +
                        '</tr>' +
                        '<tr>' +
                        '<td width="30%">Reference</td><td>'+reference+'</td>' +
                        '</tr>' +
                        '<tr>' +
                        '<td>Fund</td><td>'+fund+'</td>' +
                        '</tr>' +
                        '<tr>' +
                        '<td>Amount</td><td>'+payable+'</td>' +
                        '</tr>' +
                        '</tbody>' +
                        '</table>' +
                        '<p>You are going to allow expired transaction payment. ' +
                        'Generally, the payment made 24 hours ago is not accepted. You are about to cancel this restriction.<br>'+
                        'Press Yes to Allow or No to Dismiss</p>',
                    buttons: {
                        paytransaction: {
                            text: 'YES',
                            btnClass: 'btn-green',
                            action: function () {
                                var self = this;
                                $.ajax({
                                    url: __rCore.ajax,
                                    dataType: 'json',
                                    method: 'post',
                                    data : {
                                        context : __rCore.context.tae,
                                        data: {
                                            reference : reference,
                                        }
                                    },
                                    beforeSend : function () {
                                        self.showLoading(true);
                                    }
                                }).done(function (response) {
                                    if(response.state) {
                                        self.close();
                                        swal("Successful", response.message ? response.message : 'Transaction Expire restriction removed successfully', "success");
                                        storage.draw(false);
                                    } else {
                                        self.hideLoading(true);
                                        $.alert( response.message ? response.message : 'Error Occurred. Please Try Again' );
                                    }
                                }).fail(function(){
                                    self.hideLoading(true);
                                    $.alert('Error Occurred. Please Try Again');
                                });

                                return false;
                            }
                        },
                        cancel: {
                            text : 'NO',
                            action : function () {

                            }
                        },
                    }
                });
            }

        });

        $('body').on('click', 'a.aclremove.transaction', function (e) {
            e.preventDefault();
            var reference = $(this).attr('acl-remove');
            var payable = $(this).parent().parent().find('td:nth-child(6)').text();
            var fund = $(this).parent().parent().find('td:nth-child(5)').text();
            var client = $(this).parent().parent().find('td:nth-child(3)').text();


            if(reference && reference.length >0) {
                $.confirm({
                    title: 'Are you sure ?',
                    content: '' +
                        '<table class="table table-bordered table-striped">' +
                        '<tbody>' +
                        '<tr>' +
                        '<td>Client</td><td>'+client+'</td>' +
                        '</tr>' +
                        '<tr>' +
                        '<td width="30%">Reference</td><td>'+reference+'</td>' +
                        '</tr>' +
                        '<tr>' +
                        '<td>Fund</td><td>'+fund+'</td>' +
                        '</tr>' +
                        '<tr>' +
                        '<td>Amount</td><td>'+payable+'</td>' +
                        '</tr>' +
                        '</tbody>' +
                        '</table>' +
                        '<p>You are going to remove the transaction. Press Yes to Remove or No to Dismiss</p>',
                    buttons: {
                        paytransaction: {
                            text: 'YES',
                            btnClass: 'btn-green',
                            action: function () {
                                var self = this;
                                $.ajax({
                                    url: __rCore.ajax,
                                    dataType: 'json',
                                    method: 'post',
                                    data : {
                                        context : __rCore.context.tra,
                                        data: {
                                            reference : reference,
                                        }
                                    },
                                    beforeSend : function () {
                                        self.showLoading(true);
                                    }
                                }).done(function (response) {
                                    if(response.state) {
                                        self.close();
                                        swal("Successful", response.message ? response.message : 'Transaction removed successfully', "success");
                                        storage.draw(false);
                                    } else {
                                        self.hideLoading(true);
                                        $.alert( response.message ? response.message : 'Error Occurred. Please Try Again' );
                                    }
                                }).fail(function(){
                                    self.hideLoading(true);
                                    $.alert('Error Occurred. Please Try Again');
                                });

                                return false;
                            }
                        },
                        cancel: {
                            text : 'NO',
                            action : function () {

                            }
                        },
                    }
                });
            }

        });

        $('body').on('click', 'a.aclpay.dynamic', function(e){
            e.preventDefault();
            var reference = $(this).attr('acl-pay');
            var payable = $(this).parent().parent().find('td:nth-child(6)').text();
            var fund = $(this).parent().parent().find('td:nth-child(5)').text();
            var client = $(this).parent().parent().find('td:nth-child(3)').text();

            if(reference && reference.length >0) {
                $.confirm({
                    title: 'Paying Transaction',
                    content: '' +
                        '<table class="table table-bordered table-striped">' +
                        '<tbody>' +
                        '<tr>' +
                        '<td>Client</td><td>'+client+'</td>' +
                        '</tr>' +
                        '<tr>' +
                        '<td width="30%">Reference</td><td>'+reference+'</td>' +
                        '</tr>' +
                        '<tr>' +
                        '<td>Fund</td><td>'+fund+'</td>' +
                        '</tr>' +
                        '<tr>' +
                        '<td>Amount</td><td>'+payable+'</td>' +
                        '</tr>' +
                        '</tbody>' +
                        '</table>',
                    buttons: {
                        paytransaction: {
                            text: 'Pay',
                            btnClass: 'btn-green',
                            action: function () {
                                var self = this;
                                $.ajax({
                                    url: __rCore.ajax,
                                    dataType: 'json',
                                    method: 'post',
                                    data : {
                                        context : __rCore.context.tdp,
                                        data: {
                                            reference : reference,
                                        }
                                    },
                                    beforeSend : function () {
                                        self.showLoading(true);
                                    }
                                }).done(function (response) {
                                    if(response.state) {
                                        self.close();
                                        swal("Successful", response.message ? response.message : 'Transaction paid successfully', "success");
                                        storage.draw(false);
                                    } else {
                                        self.hideLoading(true);
                                        $.alert( response.message ? response.message : 'Error Occurred. Please Try Again' );
                                    }
                                }).fail(function(){
                                    self.hideLoading(true);
                                    $.alert('Error Occurred. Please Try Again');
                                });

                                return false;
                            }
                        },
                        cancel: function () {

                        },
                    }
                });
            }
        });

        $('body').on('click', 'a.aclreview.transaction', function (e) {
            e.preventDefault();
            var reference = $(this).attr('acl-review');

            if(reference && reference.length >0) {
                $.dialog({
                    title: 'Transaction Details',
                    content: function () {
                        var self = this;
                        return $.ajax({
                            url: __rCore.ajax,
                            dataType: 'json',
                            method: 'post',
                            data : {
                                context : __rCore.context.tdr,
                                data: {
                                    reference : reference,
                                }
                            },
                        }).done(function (response) {
                            self.setContent(response.message ? response.message : 'Error Occurred. Please try again later');
                        }).fail(function(){
                            self.setContent('Error Occurred. Please try again later');
                        });
                    },
                    columnClass : 'col-md-8 col-md-offset-2'
                });
            }
        });



    });
}(jQuery));