<style type="text/css">
    .DTTT {
        display: none;
    }
    #productsdatatable_filter {
        display: none;
    }
</style>

<!-- Page Content -->
<div class="page-content">

    <!-- Page Header -->
    <div class="page-header position-relative">
        <div class="header-title">
        <h1>
            <?php echo $this->lang->line("ADDPRICELIST"); ?>
         </h1>
     </div>
 
     <!--Header Buttons-->
     <div class="header-buttons">
        <a class="sidebar-toggler" href="#">
            <i class="fa fa-arrows-h"></i>
        </a>
        <a class="refresh" id="refresh-toggler" href="">
            <i class="glyphicon glyphicon-refresh"></i>
        </a>
        <a class="fullscreen" id="fullscreen-toggler" href="#">
            <i class="glyphicon glyphicon-fullscreen"></i>
        </a>
    </div>
    <!--Header Buttons End-->
</div>
<!-- /Page Header -->
<!-- Page Body -->
<div class="page-body" id="resources">
    <div class="widget-body">
        <div class="col-md-12" style="padding-bottom: 12px;">
                Tariff Name : <strong><?php echo $tariffname[0]->NAME; ?></strong>
            </div>
            <div class="col-md-4">
                <input type="text" class="form-control" id="txtsearch" placeholder="Search ..." style="width: 100%;" />
            </div>
            <div class="col-md-6">
                <select id="categorylist" style="width:100%;">
                    <option value=""><?php echo $this->lang->line("SELECTCATEGORY"); ?></option>
                    <?php
                    if($categories) {
                        foreach($categories as $cat) {
                    ?>
                        <option value="<?php echo $cat->ID; ?>"><?php echo $cat->NAME; ?></option>
                    <?php
                        }
                    }
                    ?>
                </select>
            </div>
        <input type="hidden" name="hdsearch" id="hdsearch" value="" />
        <input type="hidden" name="hdcategory" id="hdcategory" value="" />
        <button type="button" class="btn btn-blue shiny" id="searchbtn"><?php echo $this->lang->line("SEARCH"); ?></button>
    </div>
    <div class="well with-header" style="padding-top: 20px; overflow: auto;">
        <table class="table table-bordered table-hover" id="productsdatatable">
            <thead>
                <tr>
                    <td>
                        <strong><?php echo $this->lang->line("PRODUCT"); ?></strong>
                    </td>
                    <td>
                        <strong><?php echo $this->lang->line("SELLPRICE"); ?></strong>
                    </td>
                    <td>
                        <strong><?php echo $this->lang->line("SELLPRICEPLUSTAX"); ?></strong>
                    </td>
                    <td>
                        <input type="text" name="alltariffprice" id="alltariffprice" class='form-control'
                            placeholder="<?php echo $this->lang->line("ENTERRATEFORALLPRODUCTS"); ?>"
                            style="width: 100%;" onchange="settariffrateforallproducts(this.value)" />
                    </td>
                </tr>
            </thead>
        </table>
    </div>
</div>
<!-- /Page Body -->
</div>
<!-- /Page Content -->
</div>
<!-- /Page Container -->
<!-- Main Container -->
</div>
<script src="<?php echo base_url();?>assets/js/dropzone/dropzone.min.js"></script>
<script src="<?php echo base_url();?>assets/js/select2/select2.js"></script>
<script type="text/javascript">
    $("#categorylist").select2();
    $("#txtbarcode").focus();
    
    $(document).ready(function () {
        var table = $('#productsdatatable').DataTable({
        "processing": true,
        "serverSide": true,
        "aLengthMenu": [
            [100,500,1000],
            [100,500,1000]
        ],
        "sPaginationType": "bootstrap",
        "sDom": "Tflt<'row DTTTFooter'<'col-sm-6'i><'col-sm-6'p>>",
        "tableTools": {
            "aButtons": ["copy"]
        },
        "ajax":{
            "url": "<?php echo base_url(); ?>inventory/tariff/GetproductList",
            "dataType": "json",
            "type": "POST",
            "data": function(d){
                d.searchtext = $('#txtsearch').val();
                d.searchcategory = $('#categorylist').val();
                d.tariffid = '<?php echo $tariffid; ?>'
            }
        },
        "oLanguage": {
            "sEmptyTable":     "<?php echo $this->lang->line("NOTHINGFOUND"); ?>",
            "sInfo":           "<?php echo $this->lang->line("SHOWINGPAGE"); ?> _START_ <?php echo $this->lang->line("TO"); ?> _END_ <?php echo $this->lang->line("OF"); ?> _TOTAL_ <?php echo $this->lang->line("ENTRIES"); ?>",
            "sInfoEmpty":      "<?php echo $this->lang->line("SHOWINGPAGE"); ?> 0 <?php echo $this->lang->line("TO"); ?> 0 <?php echo $this->lang->line("OF"); ?> 0 <?php echo $this->lang->line("ENTRIES"); ?>",
            "sInfoPostFix":    "",
            "sInfoThousands":  ",",
            "sLengthMenu":     "<?php echo $this->lang->line("SHOW"); ?> _MENU_ <?php echo $this->lang->line("ENTRIES"); ?>",
            "sLoadingRecords": "<?php echo $this->lang->line("LOADING"); ?>",
            "sProcessing":     "<?php echo $this->lang->line("PROCESSING"); ?>",
            "sSearch":         "",
            "sZeroRecords":    "<?php echo $this->lang->line("NORECORDSAVAILABLE"); ?>",
            "oPaginate": {
                "sFirst":    "<?php echo $this->lang->line("FIRST"); ?>",
                "sLast":     "<?php echo $this->lang->line("LAST"); ?>",
                "sNext":     "<?php echo $this->lang->line("NEXT"); ?>",
                "sPrevious": "<?php echo $this->lang->line("PREVIOUS"); ?>"
            }
        },
        "columns": [
                { "data": "NAME",
                    "bSortable": false },
                { "data": "PRICESELL",
                    "bSortable": false,
                    "className": "text-right" },
                { "data": "PRICESELLTAX",
                    "bSortable": false,
                    "className": "text-right" },
                {
                    "data": null,
                    "bSortable": false,
                    "mRender": function (data, type, full) {
                        return "<input type='text' class='form-control' onchange=saveproducttariff('"+data.ID+"',this.value,'"+data.TAXRATE+"') value='"+data.TARIFFPRICE+"' />\n\
                        <span id='"+data.ID+"' style='display:none;'><i class='fa fa-check'></i> Saved</span>";
                    }
                }
            ]	 
        });
        $("#searchbtn").click(function() {
            var txtsearch    =   $('#txtsearch').val();
            var categorylist    =   $('#categorylist').val();
            $("#hdsearch").val(txtsearch);
            $("#hdcategory").val(categorylist);
            table.draw();
        });
    });
    
    function saveproducttariff(prid,amount,taxrate=0) {
        $.ajax({
            method: "POST",
            url: "<?php echo base_url('inventory/tariff/managetariffproduct/'); ?>",
            data: {
                prid: prid,
                tariffid:'<?php echo $tariffid; ?>',
                amount:amount,
                taxrate:taxrate
            },
            dataType: "json"
        }).done(function (msg) {
            $("#"+msg.prid).fadeIn(600).fadeOut(600);
        });
    }
    
    function settariffrateforallproducts(commontxtval) {
        if(commontxtval!='') {
            var r = confirm("<?php echo $this->lang->line("AREYOUWANTTOUPDATEALLTHEPRODUCT"); ?>");
            if (r == true) {
                var searchval   =   $("#hdsearch").val();
                var searchcategory   =   $("#hdcategory").val();
                $(".loading-container").removeClass("loading-inactive");
                $(".loading-container").html("<div style='text-align: center;padding-top: 5%;font-size: 25px;color:  #ffff;'><?php echo $this->lang->line("PLEASEWAITWHILEUPDATINGPRODUCTPRICE"); ?></div>");
                $.ajax({
                    method: "POST",
                    url: "<?php echo base_url('inventory/tariff/updateallproductstariffprices/'); ?>",
                    data: {
                        tariffid:'<?php echo $tariffid; ?>',
                        commontxtval:commontxtval,
                        searchval:searchval,
                        searchcategory:searchcategory
                    },
                    dataType: "json"
                }).done(function (msg) {
                    location.reload();
                });
            }
        }
    }
    
</script>