<script>
    $(document).ready(function() {
        $('select').select2();
        jQuery('.datepicker2').datepicker({
            dateFormat: 'dd-mm-yy',
            changeMonth: true,
            changeYear: true
        });

    });
</script>

<script type="text/javascript">
    $(document).ready(function() {
        /* Build the DataTable with third column using our custom sort functions */
        var oTable = $("#test").dataTable({
            "iDisplayLength": 10,
            "aLengthMenu": [5, 10, 25, 50, 100],
            "asSorting": ["asc"], "aTargets": [1],
            "sPaginationType": "full_numbers", "aoColumns": [{
                    "bSortable": false},
                null,
                null,
                null,
                null,
<?php echo $mode == 'post' ? 'null,' : ''; ?>
                {"bSortable": false}]});
        oTable.fnSort([[1, 'asc']]);

        $("table.editable").on("click", ".unconfirm", function() {

            rRow = $(this).parents("tr");
            ID = $('td:first', $(this).parents('tr')).text();

            $("#unconfirm").dialog("open");
        });

        $("#unconfirm").dialog({
            autoOpen: false,
            resizable: false,
            modal: true,
            buttons: {
                "Unconfirm": function() {
//				window.location.href = "<?php echo base_url() ?>po_transaction/purchaseOrderCancel/" + ID;
                    //window.location.href = "purchaseOrderCancel?ID=" + ID;		
                    fct_unconfirm(rRow);

                    $(this).dialog("close");
                },
                Cancel: function() {
                    $(this).dialog("close");
                }
            }
        });


        function fct_unconfirm(row) {
            var journalID = $('td:first', row).attr("data-journalNo");
            var formNo = $('td:first', row).attr("data-formNo");
            $.ajax({
                type: "POST",
                url: "<?php echo base_url() ?>po_transaction/unconfirm_po",
                data: {
                    ID: ID,
                },
                success: function(data) {
                    switch($.trim(data)){
                        case "succes":
                           $(row).remove();
                            alertbox("Uncofirming successfully done ...", 1); 
                           break;
                       case "pr_exist":
                            alertbox("fail to uncofirm, Please do unconfirm all purchase recieve related to this Purchase order then try again...", 2);
                            break;
                       default:
                           alertbox("fail to uncofirm ...", 2);
                           break;
                    }
                },
            });       // Here your ajax action after delete confirmed
        }


        $("table .checkall1").live("click", "checkall1", function() {
            $(oTable.fnGetNodes()).find(':checkbox').prop('checked', this.checked);
        });



        $("table.editable").on("click", ".edit", function() {
            rRow = $(this).parents("tr");
            ID = $('td:first', $(this).parents('tr')).text();
            $("#row_edit").dialog("open");
        });
        $("table.editable").on("click", ".remove", function() {
            rRow = $(this).parents("tr");
            ID = $('td:first', $(this).parents('tr')).text();
            $("#row_delete").dialog("open");
        });
        $("#row_edit").dialog({
            autoOpen: false,
            resizable: false,
            modal: true,
            buttons: {
                "Continue...": function() {
                    window.location.href = "<?php echo base_url() ?>PO_Transaction/edit/" + ID;
                    $(this).dialog("close");
                },
                Cancel: function() {
                    $(this).dialog("close");
                }
            }
        });
        function remove_row(row) {
            row.remove();
            $.ajax({
                type: "POST",
                url: "<?php echo base_url() ?>po_transaction/purchaseOrderCancel",
                data: "ID=" + ID,
                success: function() {
                    alert('Row has been deleted...');
                },
            });       // Here your ajax action after delete confirmed
        }
        $("#row_delete").dialog({
            autoOpen: false,
            resizable: false,
            modal: true,
            buttons: {
                "Delete": function() {
//				window.location.href = "<?php echo base_url() ?>po_transaction/purchaseOrderCancel/" + ID;	
                    //window.location.href = "purchaseOrderCancel?ID=" + ID;		
                    remove_row(rRow);
                    $(this).dialog("close");
                },
                Cancel: function() {
                    $(this).dialog("close");
                }
            }
        });
    });

    function changeList() {
        var selDateFrom = $('#selDateFrom').val();
        var selDateTo = $('#selDateTo').val();
        //var urls = "<?php //echo base_url();  ?>purchaseReports/poLIST/<?php //echo $mode;  ?>/"+selDateFrom+"/"+selDateTo;
        var urls = "<?php echo base_url() . $controller . $mode; ?>/" + selDateFrom + "/" + selDateTo;
        window.location = urls;
    }

    function printPurchaseOrderList() {
        //   alert ($("#test_filter input").val());
        var selDateFrom = $('#selDateFrom').val();
        var selDateTo = $('#selDateTo').val();
        window.open("<?php echo base_url(); ?>PO_Transaction/rptPO/<?php echo $mode; ?>/" + selDateFrom + "/" + selDateTo);
//	window.open("<?php echo base_url() . $controller . $mode; ?>/"+selDateFrom+"/"+selDateTo);
    }

    function updatePosting() {

        var oTable = $("#test").dataTable( );
        $(oTable.fnGetNodes()).find(':checkbox').each(function() {
            if ($(this).is(':checked')) {
                var ID = $('td:first', $(this).parents('tr')).text();
                var mode = $('#mode').val();
                var postData = {
                    'ID': ID
                };
                $.ajax({
                    type: "POST",
                    dataType: "json",
                    url: "<?php echo base_url() ?>PO_Transaction/poConfirm",
                    data: postData,
                    async: false,
                    success: function(content) {
                    }
                });
            }
        });
        window.location.reload(true);
    }

</script>



<div id="content">                        
    <div class="wrap">
        <div class="head">
            <div class="info">
                                                            <!--<h1><?php echo element('compName', $this->session->userdata('logged_in')) ?> [ 
                <?php echo element('compNo', $this->session->userdata('logged_in')) ?> ]</h1>-->
                <ul class="breadcrumb">
                    <li><a href="#"><?php echo $this->lang->line('dashboard') ?></a> <span class="divider">-</span></li>
                    <li><a href="#"><?php echo $this->lang->line('module') ?></a> <span class="divider">-</span></li>
                    <li class="active"><?php echo (isset($poList) && $poList) ? $this->lang->line('rptTitle') : $this->lang->line('title2'); ?></li>
                </ul>
            </div>
            <div class="search">
                <form action="<?php echo base_url() ?>admin/search" method="post">
                    <input name="search_text" type="text" placeholder="search..."/>                                
                    <button type="submit"><span class="i-magnifier"></span></button>
                </form>
            </div>                        
        </div><!-- head --> 
        <div class="content">
            <div class="wrap">                    
                <div class="row-fluid">
                    <div class="span12">
                        <div class="block">
                            <div class="head">
                                <h2><?php echo $this->lang->line('searchparameters'); ?></h2>
                            </div><!-- head -->
                            <div class="content np">   
                                <form method="post" action="<?php echo base_url(); ?>po_transaction/poList">
                                    <div class="controls-row"><!-- Row 4-->
                                        <div class="span6">
                                            <div class="span5"><?php echo form_label($this->lang->line('dateFrom')); ?></div>
                                            <div class="span7">
                                                <div class="input-prepend span12">
                                                    <span class="add-on span2"><i class="i-calendar"></i></span>
                                                    <?php
                                                    $data = array(
                                                        'name' => 'selDateFrom',
                                                        'id' => 'selDateFrom',
                                                        'type' => 'text',
                                                        'class' => 'input-small datepicker2 financhk span10',);
                                                    $js = '';
                                                    echo form_input($data, date("d-m-Y", strtotime($dateFrom)), $js);
                                                    ?>       
                                                </div>
                                            </div>  
                                        </div>
                                        <div class="span6">
                                            <div class="span1"><?php echo form_label($this->lang->line('dateTo')); ?></div>
                                            <div class="span7">
                                                <div class="input-prepend span12">
                                                    <span class="add-on span2"><i class="i-calendar"></i></span>
                                                    <?php
                                                    $data = array(
                                                        'name' => 'selDateTo',
                                                        'id' => 'selDateTo',
                                                        'type' => 'text',
                                                        'class' => 'input-small datepicker2 financhk span10',);
                                                    $js = '';
                                                    echo form_input($data, date("d-m-Y", strtotime($dateTo)), $js);
                                                    ?>       
                                                </div>
                                            </div>
                                        </div>
                                    </div><!-- End Row 4-->
                                    <div class="controls-row">
                                        <div class="span6">
                                            <div class="span5">Display Option</div>
                                            <div class="span7">
                                                <select name="para_display_option" id="para_display_option" class="span12">
                                                    <option <?php if ($para_display_option == "1") echo "selected='selected'"; ?> value="1">UnConfirm Purchase Order</option>
                                                    <option <?php if ($para_display_option == "0") echo "selected='selected'"; ?>value="0">Confirm Purchase Order</option>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="span6 tac">

                                        </div>
                                    </div>

                                    <div class='footer'>
                                        <div class='side fr'>
                                            <input class="btn-form btn-success-form" type="submit" value="<?php echo $this->lang->line('submit'); ?>" />
                                            <?php
//                                        $data = array(
//                                            'name' => 'mysubmit',
//                                            'class' => 'btn-form btn-success-form');
//                                        $js = "onClick='changeList()'";
//                                        echo form_button($data, $this->lang->line('submit'), $js);
                                            ?>
                                        </div>
                                    </div>
                                </form>
                            </div><!-- content -->
                            <div class="head">
                                <h2>Search Result</h2>
                                <div class="side fr">
                                    <?php
                                    if ($mode != "post") {
                                        if (isset($poList) && $poList):
                                            ?>                                                
                                            <a href="<?php echo base_url() ?>purchaseReports/rptPO/<?php echo date('d-m-Y', strtotime($dateFrom)); ?>/<?php echo date('d-m-Y', strtotime($dateTo)); ?>" class="btn-form btn-success-form" name="updatePosting" onclick="" target="_blank"><?php echo $this->lang->line('print'); ?></a>
                                            <?php
                                        else:
                                            ?>
                                            <a href="<?php echo base_url() ?>purchaseReports/rptPOcancel/<?php echo date('d-m-Y', strtotime($dateFrom)); ?>/<?php echo date('d-m-Y', strtotime($dateTo)); ?>" class="btn-form btn-success-form" name="updatePosting" onclick="" target="_blank"><?php echo $this->lang->line('print'); ?></a>
                                        <?php
                                        endif;
                                    }
                                    ?>
                                </div>                                       
                            </div>
                            <div class="content np table-sorting">
                                <table cellpadding="0" cellspacing="0" width="100%" id="test" class="editable">
                                    <thead>
                                        <tr>
                                            <th style="display:none"></th>
                                            <th class="tac">#</th>
                                            <th class="ta"><?php echo $this->lang->line('purchaseOrderNo'); ?></th>
                                            <th><?php echo $this->lang->line('supplierName'); ?></th>
                                            <th class="tac"><?php echo $this->lang->line('poDate'); ?></th>
                                            <th width='55' class='tac'  <?php if ($para_display_option == "0") echo 'style="display:none;"'; ?> ><input type='checkbox' id='checkall1' class='checkall1'/></th>
                                            <th class="tac"><?php echo $this->lang->line('action'); ?></th>
                                            <?php
//                                            if ($mode == "edit") {
//                                                echo"<th width='55' class='tac'>";
//                                                echo $this->lang->line('action');
//                                                echo"</th>";
//                                            } else if ($mode == "post") {
//                                                echo"<th width='55' class='tac'><input type='checkbox' id='checkall1' class='checkall1'/></th>";
//                                                echo"<th width='55' class='tac'>";
//                                                echo $this->lang->line('action');
//                                                echo"</th>";
//                                            } else if ($mode == "rpt") {
//                                                echo"<th width='55' class='tac'>";
//                                                echo $this->lang->line('report');
//                                                echo"</th>";
//                                            } else {
//                                                echo"<th width='55' class='tac'>";
//                                                echo $this->lang->line('action');
//                                                echo"</th>";
//                                            }
                                            ?>	
                                            <?php
                                            /* if($mode == "post"){
                                              echo"<input type='checkbox' class='checkbox' name='updateAble' id='updateAble' value='1'/>";
                                              }
                                              else if ($mode == "rpt"){ //rpt
                                              echo "<a href='".base_url()."purchaseReports/PODetails/".$datatbl->ID."' name='' onclick='' class='btn btn-mini btn-primary edit; i-printer' target='_blank'></a>";
                                              /*$js = 'class="btn btn-mini btn-primary" title="'.$this->lang->line("report").'"';
                                              echo form_button('edit','<span class="i-printer">',$js);
                                              } */
                                            ?>				
                                            <!-- <th class="tac"><input type='checkbox' id='checkall1' class='checkall1'/></th>
                                            <th class="tac"><?php echo $this->lang->line('action'); ?></th> -->
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $bil = 0;
                                        if ($datatbls) {
                                            foreach ($datatbls as $datatbl):
                                                $bil++
                                                ?>
                                                <tr>
                                                    <td style="display:none" ><?php echo $datatbl->ID; ?></td>	
                                                    <td class="tac"><?php echo $bil; ?></td>
                                                    <td> 
                                                        <?php
                                                        if ($mode == "post")
                                                            echo ' <a title="Edit" class="btn btn-mini btn-link edit">';
                                                        echo $datatbl->formNo;
                                                        ?></td>
                                                    <td><?php echo $datatbl->supplierName ?></td>
                                                    <td class="tac"><?php echo date("d-m-Y", strtotime($datatbl->poDate)); ?></td>
                                                    <td <?php if ($para_display_option == "0") echo 'style="display:none;"'; ?> class="tac">

                                                        <input type='checkbox' class='checkbox' name='updateAble' id='updateAble' value='1'/>
                                                    </td>

                                                    <td class="tac">
                                                        <?php
                                                        if ($para_display_option == "1") {
                                                            $js = 'class="btn-icon btn-mini btn-danger-form remove"  title="' . $this->lang->line('delete') . '"';
                                                            echo form_button('remove', '<span class="i-trashcan">', $js);
                                                        } else {
                                                            $js = 'class="btn-icon btn-mini btn-warning-form unconfirm"  title="Unconfirm"';
                                                            echo form_button('unconfirm', '<span class="i-reply">', $js);
                                                        }
                                                        ?>

                                                    </td>
                                                    </td>
                                                </tr>
                                            <?php endforeach;
                                        }
                                        ?>
                                    </tbody>
                                </table>
                                <?php
                                if ($para_display_option == "1") {
                                    echo"<div class='footer'>
            	<div class='side fr'>
                	<div class='btn-group'>";
                                    $data = array(
                                        'name' => 'updatePosting',
                                        'class' => 'btn-form btn-warning-form');
                                    $js = 'onclick="updatePosting()"';
                                    echo form_submit($data, $this->lang->line('updatePosting'), $js);
                                    echo"</div>
                </div>
            </div>";
                                }
                                ?>                                         
                            </div>
                            <div class="dialog" id="unconfirm" style="display: none;" title="Unconfirming Transaction">
                                <p>Your about to un-confirm this transaction</p>
                                <p><i>To confirm back this transaction go to related transaction menu</i></p>
                            </div>
                            <div class="dialog" id="row_delete" style="display: none;" title="Delete?">
                                <p>Row will be deleted</p>
                            </div>
                            <div class="dialog" id="row_edit" style="display: none;" title="Edit ?">
                                <p><?php echo $this->lang->line('message1'); ?></p>
                            </div>  
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>