<div class="page-title">
    <h1><?php echo $this->user->lang("Support Settings - Custom Fields"); ?></h1>
    <span><?php echo $this->user->lang("Determine what custom fields Clientexec will expose for your clients to fill out with their support tickets. Can be associated to ticket types."); ?></span>
</div>
<div class="cm-holder settings-wrapper dt-info">
    <div class="head">
        <button class="action-buttons blue" id="add-button">
            <i class="icon-ic-21"></i>
            <?php echo $this->user->lang('Add Custom Field'); ?>
        </button>
    </div>
    <div class="dt-wrapper pull-top">
        <div class="table-responsive">
            <table id="table-list" class="display" style="width:100%">
                <thead>
                    <tr>
                        <th></th>
                        <th class="text-center"><input type="checkbox" id="checkAll"></th>
                        <th class=""></th>
                        <th class=""><?php echo $this->user->lang('Name'); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($this->fields as $field) { ?>
                    <tr id="<?php echo $field['id']; ?>">
                        <td><?php echo $field['fieldOrder']; ?></td>
                        <td></td>
                        <td></td>
                        <td><a class='edit-link' data-id='<?php echo $field['id']; ?>'><?php echo $field['text']; ?></a></td>
                    </tr>
                    <?php } ?>
                </tbody>
            </table>
        </div>
        <div class="table-actions">
            <button class="action-buttons" id="delete-button" disabled="disabled"><i class="icon-ic-25"></i><?php echo $this->user->lang('Delete'); ?></button>
        </div>
    </div>
</div>

<script>
    $(document).ready(function () {
        var addEditWindow = new RichHTML.window({
            url: 'index.php?fuse=admin&view=productcustomfield&controller=settings&settings=support',
            actionUrl: 'index.php?fuse=admin&action=savecustomfield',
            showSubmit: true,
            title: clientexec.lang("Add/Edit Custom Field"),
            onSubmit: function (data) {
                jsonData = clientexec.parseResponse(data);
                if (jsonData.success == true) {
                    window.location.reload();
                }
            }
        });

        var table = $('#table-list').DataTable({
            order: [
                [0, 'asc']
            ],
            searching: false,
            select: {
                style: 'multi',
                selector: 'td:first-child'
            },
           rowReorder: {
                dataSrc: 0,
                selector: '.reorder-handle'
            },
            columnDefs: [
                { targets: 0, visible: false },
                {
                    targets: 1,
                    "orderable": false,
                    className: 'select-checkbox',
                    checkboxes: { selectRow: true },
                    width: '1%'
                },
                {
                    targets: 2,
                    width: '5%',
                    className: 'reorder-handle text-center',
                    orderable: false,
                    render: function () {
                        return '<i class="bi bi-grip-vertical"></i>';
                    }
                },
                {
                    targets: 3,
                    width: '65%'
                },
            ]
        });

        table.on('row-reorder', function (e, diff, edit) {
            table.one('draw', function () {
                var newOrder = table.rows().ids().toArray().join(',');
                $.ajax({
                    url: 'index.php?fuse=admin&action=reordercustomfields',
                    method: 'POST',
                    data: { 'fields': newOrder, type: 'package' },
                    traditional: true,
                });
            });
        });

        $('#add-button').on('click', function() {
            addEditWindow.show();
        });

        $('#table-list').on('click', '.edit-link', function(e) {
            e.preventDefault();
            addEditWindow.show({
                params: {
                    id: $(this).data('id'),
                }
            });
        });

        $('#delete-button').click(function () {
            RichHTML.msgBox(clientexec.lang('Are you sure you want to delete the selected custom field(s)?'),
            {
                type:"confirm"
            }, function(result) {
                if ( result.btn === clientexec.lang("Yes") ) {
                    var rowIds = [];
                    var selectedRows = table.rows({ selected: true }).data();
                    selectedRows.each(function(value, index) {
                        rowIds.push(value.DT_RowId);
                    });

                    $.post("index.php?fuse=admin&action=deletecustomfield", { ids: rowIds, type: 'package' }, function(data) {
                        jsonData = clientexec.parseResponse(data);
                        if (jsonData.success == true) {
                            window.location.reload();
                        }
                    });
                }
            });
        });

        table.on('select.dt deselect.dt', function() {
            var selectedRows = table.rows({ selected: true }).count();
            if (selectedRows > 0) {
                $('.table-actions .action-buttons').prop('disabled', false);
            } else {
                $('.table-actions .action-buttons').prop('disabled', true);
            }
        });

        $('#checkAll').on('click', function () {
            if (this.checked) {
                table.rows({page: 'current'}).every(function () {
                    var rowNode = this.node();
                    if ($(rowNode).find('td.select-checkbox').length) {
                        this.select();
                    }
                });
            } else {
                table.rows().deselect();
            }
        });
    });
</script>