
<div id='addon-content-preloader' class='preloader loading'>
    <span class='preloader'></span>
</div>

<div class='addon-content'>


    <div class="chooseProductPanel panel-body">
        <table class='table chooseProductPanel' id='productlimitdata'>
            <thead>
                <tr>
                    <th></th>
                    <th>Product Name</th>
                    <th>Description</th>
                </tr>
            </thead>
            <tbody>
                {foreach $productList as $category}
                    <tr name='cat{$category.category_id}'class="active group_name" style="border: 1px solid #b6b6b6; ">
                        <td ><input type="checkbox" class="test groupSelect" name="{$category.id}"  value="cat{$category.category_id}" ></td>
                        <td style="color:#0f8be6"><strong style="font-size: 15px;">{$category.category_name}</strong></td>
                        <td  style="color:#0f8be6"> </td>
                    </tr>                          
                    {foreach $category.products as $prodl}
                        <tr name='{$prodl.name}' style="border: 1px solid #ddd">
                            <td><input type="checkbox" name="td_productid" class="test cat{$category.category_id} generalSelect" value="{$prodl.id}"
                                       {if $prodl.value == 1} checked{/if}
                                       ></td>
                            <td><input type="hidden" name="td_productname" value="{$prodl.name}">{$prodl.name}</td>
                            <td><input type="hidden" name="td_productdesc" value="{$prodl.name}">{$prodl.description}</textarea>
                            </td>
                        </tr>               

                    {/foreach}
                {/foreach}
            </tbody>
        </table>
        <div class="pull-right">
            <button class="btn btn-success btn-inverse btnsave bmConfigAcceptSave">Save</button>        
            <input type="hidden" class="bmConfigAcceptSave" id="saveallData">
        </div>
    </div>
</div>

{literal}
    <script>
        $(document).ready(function () {
            mainSelect();
        });

        if ($('.groupSelect').length) {
            $(".groupSelect").on('click', function () {
                groupName = $(this).val();
                value = $(this).is(':checked');
                $("input:checkbox." + groupName).prop('checked', this.checked);
            }
            );
        }
        function mainSelect() {
            $("input:checkbox.groupSelect").each(function (index) {
                val = $(this).val();
                if ($('.' + val + ':checked').length === $('.' + val).length) {
                    $(this).prop('checked', 'checked');
                } else {
                    $(this).prop('checked', '');

                }

            });
        }

        $(".generalSelect").on('click', function () {
            mainSelect();
        }
        );

        if ($('.bmConfigAcceptSave').length) {
            $(".bmConfigAcceptSave").on('click', function (e) {
                console.log('sss');
                $("#Loader").show();
                e.preventDefault();
                var arr = new Array();
                $('#productlimitdata > tbody > tr').each(function (k, v) {
                    if ($(v).hasClass('group_name') === false) {
                        arr.push({
                            'id': $($(v).find('input[name=td_productid]')).val(),
                            'value': $($(v).find('input[name=td_productid]')).is(':checked'),
                            'description': $($(v).find('textarea[name=td_description]')).val()
                        });
                    }
                });
                console.log(arr);
                $.post("addonmodules.php?module=ProductDescriptionForInvoicesFree&json=json",
                        {
                            "saveProdList": JSON.stringify(arr)
                        },
                        function (r) {
                            $("#Loader").hide();
                            result = JSON.parse(r.data);
                    console.log(result.result);
                            if (result.result == true)
                            {
                                $("#pl_failsafe").modal('hide');
                                $('#MDAlerts').find('[data-prototype=success]').show();
                                $('#MDAlerts').find('.note-success strong').html('Changes have been saved successfully.');
                            } else
                            {
                                $("#pl_failsafe").modal('hide');
                                $('#MDAlerts').find('[data-prototype=error]').show();
                                $('#MDAlerts').find('.note-danger strong').html('Something went wrong');
                                setTimeout(function () {
                                    $('#MDAlerts').find('[data-prototype=error]').fadeOut()
                                }, 2000);
                            }
                        },
                        "json");
            });
        }


    </script>{/literal}
