<?php
$cs = Yii::app()->getClientScript();
$cs->registerCoreScript('jquery'); 

?>

<section class="panel">
    <header class="panel-heading clearfix panel-theme">
        <i class="fa fa-file-invoice"></i> Sales Invoices
        <div class="pull-right">
            <button class="btn btn-success" id="create-invoice-btn">Create Invoice</button>
        </div>
    </header>

    <div class="panel-body">
        <div class="custom-data-table">
           <table id="tbl_sales_invoice" class="table table-bordered table-striped">
    <thead>
        <tr>
            <th>SI No</th>
            <th>Invoice Number</th>
            <th>Customer Name</th>
            <th>Invoice Date</th>
            <th>Total Amount</th>
            <th>Grand Total</th>
            <th>Action</th>
        </tr>
    </thead>
</table>
        </div>
    </div>
</section>

<!-- Modal container -->
<div class="modal fade" id="invoice-modal" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-lg">
        <div class="modal-content" id="invoice-modal-content"></div>
    </div>
</div>

<script>
$(document).ready(function () {

    var table = $("#tbl_sales_invoice").DataTable({

    scrollY: "600px",
    pageLength: 50,
    processing: true,
    serverSide: true,

    ajax:{
        url:"<?php echo Yii::app()->createUrl('salesInvoice/GetList'); ?>",
        type:"POST"
    },

    columns:[
        {title:"SI No"},
        {title:"Invoice Number"},
        {title:"Customer Name"},
        {title:"Invoice Date"},
        {title:"Total Amount", className:"text-right"},
        {title:"Grand Total", className:"text-right"},
        {title:"Action", orderable:false, searchable:false}
    ],

    fnCreatedRow:function(nRow,aData){
        $(nRow).addClass("context-menu");
        AddContextMenu(nRow,aData);
    }

});
    // Open create invoice modal
    $('#create-invoice-btn').click(function(){
        $.get('<?php echo Yii::app()->createUrl("salesInvoice/create"); ?>', function(html){
            $('#invoice-modal-content').html(html);
            $('#invoice-modal').modal('show');
        });
    });

});
$(document).on("click",".view-invoice-btn",function(){

    var id=$(this).data("id");

    loadPopupModal(
        "salesInvoice/viewInvoice&id="+id,
        "View Invoice",
        "lg"
    );

});
// Context Menu
function AddContextMenu(row, datas) {

    $(row).contextMenu({
        selector: 'td',
        items: {
            // "view": {
            //     name: "View Invoice",
            //     icon: "fa fa-eye",
            //     callback: function () {
            //         loadPopupModal(
            //             "salesInvoice/viewInvoice&id=" + datas[0], // assuming datas[0] = id
            //             "View Invoice",
            //             "lg"
            //         );
            //     }
            // },
            "print": {
                name: "View invoice",
                icon: "fa fa-print",
                callback: function () {
                    window.open('<?php echo Yii::app()->createUrl("salesInvoice/viewInvoice"); ?>&id=' + datas[0], "_blank");
                }
            }
        }
    });
}
</script>
