<script type="text/javascript">

    var newdata = new Array();
    var lines = new Array();
    $(function(){
        $("#newpo").click(function(){

            if(lines.length>0)
                $.ajax({
                    url: "<?= site_url() ?>purchase/purchaseorders/newpo2",
                    data: {polines: escape(serialize(lines))},
                    dataType: "json",
                    type: "POST",
                    success: function(data){
                        document.location.href="<?= site_url() ?>purchase/purchaseorders/newpo";
                    },
                error: function(xhr, ajaxOptions, thrownError){
                    alert(thrownError);

                }
            });

        });
        loadgrid();
       
    });
    $(document).ready(function() {
        $("button").button();
    });

    function loadgrid()
    {

        var loc = document.getElementById('location');
        var locid= loc.options[loc.options.selectedIndex].value;

        $("#lines").GridUnload();

        jQuery("#lines").jqGrid({
            url:'<?= site_url() ?>inventory/reorderlevel/getproductsbwtoib',
            postData: {location: locid},
            datatype: "json",
            mtype : "post",
            colNames:['','PRODUCT', 'STOCK', 'MINIMUM', 'MAXIMUM', 'PO QTY', 'NEW PO'],
            colModel:[
                {name:'ID',index:'ID',hidden:true, width:200},
                {name:'PRODUCT',index:'PRODUCT', width:200},
                {name:'STOCK',index:'STOCK', width:50, align:"center"},
                {name:'MINIMUM',index:'MINIMUM', width:50, align:"center",editoptions:{size:10}},
                {name:'MAXIMUM',index:'MAXIMUM', width:50, align:"center",editoptions:{size:10}},
                {name:'POQTY',index:'POQTY', width:80, align:"center",editoptions:{size:10}},
                {name:'NEWPO',index:'NEWPO', width:80, align:"center",editoptions:{size:10}}
            ],
            rowNum:15,
            rowList:[15,30,45],
            rownumbers: true,
            key: true,
            rownumWidth: 40,
            height: "auto",
            autowidth: true,
            pager: '#pgtoolbar1',
            sortname: 'PRODUCT',
            viewrecords: true,
            multiselect: true,
            sortorder: "ASC",
            reloadAfterEdit: true,
            reloadAfterSubmit: true,
            editurl:'url',
            onSelectRow: function(ids, status) {
                pid = null;

                pid = jQuery("#lines").jqGrid('getCell', ids, 'ID');
                if(status==true)
                {
                    qttystock = jQuery("#lines").jqGrid('getCell', ids, 'STOCK');
                    qttyminimum = jQuery("#lines").jqGrid('getCell', ids, 'MINIMUM');
                    poqty = jQuery("#lines").jqGrid('getCell', ids, 'POQTY');
                    rqnty = qttyminimum-poqty - qttystock;

                       
                    lines.push({ID:pid,QUANTITY:rqnty});
                    var grdata = serialize(lines);
                      
                }
                else{
                    for(var i=0;i<lines.length;i++)
                    {
                        if(lines[i]['ID']==pid)
                        {
                            lines.splice(i, 1);
                            break;
                        }
                    }
                }

            }

        });
        
    }
</script>

<div class="main content clearfix">
    <div class="page-heading">
        <h2>Inventory Broken</h2>
    </div>
    <!-- end page-heading -->
    <table border="0" width="100%" cellpadding="0" cellspacing="0" id="content-table">
        <tr>
            <th rowspan="3" class="sized"><img src="<?= site_url() ?>images/shared/side_shadowleft.jpg" width="20" height="300" alt="" /></th>
            <th class="topleft"></th>
            <td id="tbl-border-top">&nbsp;</td>
            <th class="topright"></th>
            <th rowspan="3" class="sized"><img src="<?= site_url() ?>images/shared/side_shadowright.jpg" width="20" height="300" alt="" /></th>
        </tr>
        <tr>
            <td id="tbl-border-left"></td>
            <td>
                <!--  start content-table-inner ...................................................................... START -->
                <div id="content-table-inner">
                    <!--  start table-content  -->
                    <div id="table-content">
                        <?php
                        if ($this->session->flashdata('msgt') == 'E') {
                            echo '
				<div id="message-red">
				<table border="0" width="100%" cellpadding="0" cellspacing="0">
				<tr>
					<td class="red-left">' . $this->session->flashdata('msg') . '</td>
					<td class="red-right"><a class="close-red"><img src="' . site_url() . 'images/table/icon_close_red.gif"   alt="" /></a></td>
				</tr>
				</table>
				</div>
						';
                        }

                        if ($this->session->flashdata('msgt') == 'S') {
                            echo '
					<div id="message-green">
					<table border="0" width="100%" cellpadding="0" cellspacing="0">
					<tr>
						<td class="green-left">' . $this->session->flashdata('msg') . '</a></td>
						<td class="green-right"><a class="close-green"><img src="' . site_url() . 'images/table/icon_close_green.gif"   alt="" /></a></td>
					</tr>
					</table>
					</div>
					';
                        }
                        ?>
                        <!--  start product-table ..................................................................................... -->
                        <div id="toolbartop">
                            <span class="button-strip"><p align="left">
					Warehouse
                                    <select name="location" id="location" onchange="loadgrid()" >
                                        <?= $locations ?>
                                    </select>

                                </p></span></div>

                        <table id="lines"></table>
                        <div id="pgtoolbar1"></div>
                        <br>
<!--                        <form action="<?= site_url() . 'inventory/reorderlevel/add/'; ?>" method="post">-->

                        <input type="hidden" name="lines" value="" />
                        <input type="hidden" name="submitkey" value="reorder" />
                        <button id="newpo" name="newpo">NewPO</button>

                        <!--                        </form>-->

                        <!--  end content-table  -->
                    </div>
                    <div class="clear"></div>

                </div>
                <!--  end content-table-inner ............................................END  -->
            </td>
            <td id="tbl-border-right"></td>
        </tr>
        <tr>
            <th class="sized bottomleft"></th>
            <td id="tbl-border-bottom">&nbsp;</td>
            <th class="sized bottomright"></th>
        </tr>
    </table>

    <div class="clear">&nbsp;</div>
</div>