<div class="page-title">
    <h1><?php echo $this->user->lang("Event List");?></h1>
</div>

<div class="cm-holder dt-info">
    <div class="head padding-bottom">
        <div class="dt-header">
            <div class="dt-header-left">

                <div class="dt-filters">

                    <div class="filter-group">
                        <label><?php echo $this->user->lang("Date Created");?></label>
                        <input
                            class="form-control"
                            type="date"
                            name="startdate"
                            id="startdate"
                        />
                        <span class="separator">-</span>
                        <input
                            class="form-control"
                            type="date"
                            name="enddate"
                            id="enddate"
                        />
                    </div>

                    <div class="filter-group">
                        <label><?php echo $this->user->lang("Show");?></label>
                         <select id="filterByType">
                            <option value="all"><?php echo $this->user->lang('All events'); ?></option>
                            <?php if ( isset($this->profileAndPackagesEvents) ) { ?>
                            <option value="profileandpackages"><?php echo $this->user->lang('Profile and Packages events'); ?></option>
                            <option value="domainrenewals"><?php echo $this->user->lang('Domain Renewal Notifications'); ?></option>
                            <?php } ?>

                            <?php if ( isset($this->profileEvents) ) { ?>
                            <option value="profileandpackages"><?php echo $this->user->lang('Profile events'); ?></option>
                            <?php } ?>

                            <?php if ( isset($this->billingEvents) ) { ?>
                            <option value="billing"><?php echo $this->user->lang('Billing events'); ?></option>
                            <?php } ?>

                            <?php if ( isset($this->supportEvents) ) { ?>
                            <option value="support"><?php echo $this->user->lang('Support Tickets events'); ?></option>
                            <?php } ?>

                            <option value="note"><?php echo $this->user->lang('Client/Group Note'); ?></option>
                            <option value="emails"><?php echo $this->user->lang('Email events'); ?></option>
                            <option value="errors"><?php echo $this->user->lang('Process Errors'); ?></option>
                            <option value="login"><?php echo $this->user->lang('Login Attempts'); ?></option>
                        </select>
                    </div>

                </div>
            </div>

            <div class="dt-header-right">
                <div class="filter-group dt-length"></div>
            </div>

        </div>
    </div>

    <div class="dt-wrapper pull-top">
        <div class="table-responsive">
            <table id="event-list" class="display" style="width:100%">
                <thead>
                    <tr>
                        <th class=""><?php echo $this->user->lang('Id'); ?></th>
                        <th class="width4 "><?php echo $this->user->lang('User'); ?></th>
                        <th class="width4 "><?php echo $this->user->lang('Action'); ?></th>
                    </tr>
                </thead>
            </table>
        </div>
    </div>
</div>

<script>
    var eventlist = eventlist || {};

    $(document).ready(function () {
        document.getElementById("startdate").value = parseFormattedDate("<?php echo $this->startDateValue; ?>");
        document.getElementById("enddate").value = parseFormattedDate("<?php echo $this->endDateValue; ?>");

        var ajaxUrl = 'index.php?fuse=home&action=geteventlist&controller=events&limit=0';
        var table = $('#event-list').DataTable({
            "searching": false,
            "ajax": {
                url: ajaxUrl,
                dataSrc: 'data', // The root is data in the ajax call.
                data: function(d) {
                    d.startdate = formatDate($('#startdate').val());
                    d.enddate = formatDate($('#enddate').val());
                    d.eventtype = $('#filterByType').val();
                }
            },
            "columns": [
                { "data": "event_id" },
                {
                    "data": "loguser",
                    render: function (data, type, row) {
                        if (type === 'display') {
                            var text = row.loguser.replace(/\[color=(\w+)\](.+)/, '<font color="$1">$2</font>');
                            return "<strong>"+ clientexec.htmlspecialchars(text) + "</strong><br/>Ip: "+row.logipaddress;
                        }

                        return data;
                    }
                },
                {
                    "data": "logaction",
                    render: function (data, type, row) {
                        if (type === 'display') {
                            var returnString = clientexec.htmlspecialchars(row.logaction);

                            switch (row.tpl) {
                                case 'link':
                                    returnString = returnString.replace(/\[link\]/, row.link);
                                    break;
                                case 'clickForBody':
                                    returnString = String.format("{0} (<a onclick=\"event.preventDefault(); eventlist.eventdetailwindow({1},'email');\" href='#'>view</a>)",row.logaction,row.entryid);
                                    break;
                                case 'clickForStaffNote':
                                    returnString = String.format("{0} (<a onclick=\"event.preventDefault(); eventlist.eventdetailwindow({1},'note');\" href='#'>view</a>)",row.logaction,row.entryid);
                                    break;
                                case 'clickForProfileDetails':
                                    returnString = String.format("{0} (<a onclick=\"event.preventDefault(); eventlist.eventdetailwindow({1},'profile');\" href='#'>view</a>)", row.logaction,row.entryid);
                                    break;
                                case 'clickForPaypalCallbackDetails':
                                    returnString = String.format("{0} (<a onclick=\"event.preventDefault(); eventlist.eventdetailwindow({1},'paypal');\" href='#'>view</a>)", row.logaction,row.entryid);
                                    break;
                                case 'clickFor2CheckoutCallbackDetails':
                                    returnString = String.format("{0} (<a onclick=\"event.preventDefault(); eventlist.eventdetailwindow({1},'2checkout');\" href='#'>view</a>)", row.logaction,row.entryid);
                                    break;
                                case 'clickForPackageDetails':
                                    returnString = String.format("{0} (<a onclick=\"event.preventDefault(); eventlist.eventdetailwindow({1},'package');\" href='#'>view</a>)", row.logaction,row.entryid);
                                    break;
                            }

                            return "<strong>"+clientexec.lang("On")+" "+row.logdate+"</strong><br/>"+returnString;
                        }

                        return data;
                    }
                }
            ],
            "order": [[0, 'desc']]
        });

        $('#startdate, #enddate, #filterByType').on('change', function () {
            table.ajax.reload();
        });

        function formatDate(dateString) {
            if (dateString == '') {
                return '';
            }

            date = new Date(dateString+'T12:00:00Z');
            options = {
                day:   "2-digit",
                month: "2-digit",
                year:  "numeric"
            };

            switch (clientexec.dateFormat) {
                case 'm/d/Y':
                    newDate = new Intl.DateTimeFormat("en-US", options).format(date);
                    break;
                case 'd/m/Y':
                    newDate = new Intl.DateTimeFormat("en-GB", options).format(date);
                    break;
            }

            return newDate;
        }

        function parseFormattedDate(dateString) {
            if (dateString == '') {
                return '';
            }

            switch (clientexec.dateFormat) {
                case 'm/d/Y':
                    parts = dateString.split('/');

                    return `${parts[2]}-${parts[0].padStart(2, "0")}-${parts[1].padStart(2, "0")}`;
                case 'd/m/Y':
                    parts = dateString.split('/');

                    return `${parts[2]}-${parts[1].padStart(2, "0")}-${parts[0].padStart(2, "0")}`;
            }
        }

        eventlist.eventwindow = new RichHTML.window({
            height: '400',
            width: '550',
            hideTitle: true
        });

        eventlist.eventdetailwindow = function(id, entryType)
        {
            if (entryType == "email") {
                eventlist.eventwindow.setUrl('index.php?fuse=clients&view=historyfullemail');
            } else if (entryType == "note"){
                eventlist.eventwindow.setUrl('index.php?fuse=clients&view=eventlogstaffnote');
            } else {
                eventlist.eventwindow.setUrl('index.php?fuse=clients&view=eventdetails');
            }

            eventlist.eventwindow.show({params:{entryId:id,entryType:entryType}});
        }
    });
</script>
