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

<section class="panel">
    <header class="panel-heading clearfix panel-theme">
        <i class="fa fa-undo"></i> Sales Return
        <div class="pull-right">
            <button class="btn btn-success" id="create-return-btn">Create Sales Return</button>
        </div>
    </header>

    <div class="panel-body">
        <div class="custom-data-table">
            <table id="tbl_sales_return" class="table table-bordered table-striped">
                <thead>
                    <tr>
                        <th>SR No</th>
                        <th>Invoice Number</th>
                        <th>Customer Name</th>
                        <th>Return Date</th>
                        <th>Action</th>
                    </tr>
                </thead>
            </table>
        </div>
    </div>
</section>

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

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

    // Initialize DataTable for Sales Return listing
    var table = $("#tbl_sales_return").DataTable({
        scrollY: "600px",
        pageLength: 50,
        processing: true,
        serverSide: true,
        ajax:{
            url:"<?php echo Yii::app()->createUrl('salesReturn/GetList'); ?>",
            type:"POST"
        },
        columns:[
            {title:"SR No"},
            {title:"Invoice Number"},
            {title:"Customer Name"},
            {title:"Return Date"},
            {title:"Action", orderable:false, searchable:false}
        ]
    });

    // Open modal to create new sales return
    $('#create-return-btn').click(function(){
        $.get('<?php echo Yii::app()->createUrl("salesReturn/create"); ?>', function(html){
            $('#sales-return-modal-content').html(html);
            $('#sales-return-modal').modal('show');
        });
    });
// View Sales Return
$(document).on('click','.view-return-btn',function(){

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

$.get('<?php echo Yii::app()->createUrl("salesReturn/view"); ?>',{id:id},function(html){

$('#sales-return-modal-content').html(html);

$('#sales-return-modal').modal('show');

});

});
    // Submit Sales Return form
$(document).on('click','#saveReturn', function() {

    var btn = $(this);

    btn.prop('disabled', true)
       .addClass('btn_loding')
       .removeClass('btn-success');

    $("#loader").show();

    var valid=false;

    $('.return_qty').each(function(){
        if($(this).val()>0){
            valid=true;
        }
    });

    if(!valid){
        $("#loader").hide();
        btn.prop('disabled', false)
           .removeClass('btn_loding')
           .addClass('btn-success');

        t8.error("Enter return qty for at least one product");
        return;
    }

    $.ajax({

        url:'<?php echo Yii::app()->createUrl("salesReturn/save"); ?>',

        type:'POST',

        data:$('#sales-return-form').serialize(),

        dataType:'json',

        success:function(res){

            $("#loader").hide();

            btn.prop('disabled', false)
               .removeClass('btn_loding')
               .addClass('btn-success');

            if(res.status==='success'){

                t8.success(res.message);

                $('#sales-return-modal').modal('hide');

                $('#tbl_sales_return').DataTable().ajax.reload(null,false);

            }
            else{

                if(typeof res.message==='object'){
                    $.each(res.message,function(k,v){
                        t8.error(v);
                    });
                }else{
                    t8.error(res.message);
                }

            }

        },

        error:function(xhr){

            $("#loader").hide();

            btn.prop('disabled', false)
               .removeClass('btn_loding')
               .addClass('btn-success');

            t8.error("Something went wrong : "+xhr.responseText);

        }

    });

});

});
</script>