<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title"> <?php print $title; ?></div>
        </div>
        <div class="panel-body">
            <input type="hidden" name="current_balance_type" id="current_balance_type" value="<?php  echo $current_type;?>"/>
            <div class="row">
                <?php echo Forms::AlertDiv('subscription_fees_alert'); ?>
            </div>
            <div class="row form-group">
                <div class="col-md-3">
                    <label><?php print CommonLanguage::getMessage('subscription_name'); ?></label>
                    <input type="hidden" id="subscription_id" name="subscription_id" class="full-width" >
                </div>
                <div class="col-md-3">
                    <label><?php print CommonLanguage::getMessage('customer_name'); ?></label>
                    <input type="hidden" id="customer_id" name="customer_id" class="full-width" >
                </div>
                <div class="col-md-2">
                    <label><?php print CommonLanguage::getMessage('period_unit'); ?></label>
                    <select name="period_unit" id="period_unit" class="full-width" data-init-plugin="select2" > 
                        <option value=""><?php echo CommonLanguage::getMessage('select'); ?></option>
                        <option value="1"><?php echo CommonLanguage::getMessage('week'); ?></option>
                        <option value="2"><?php echo CommonLanguage::getMessage('month'); ?></option>
                        <option value="3"><?php echo CommonLanguage::getMessage('year'); ?></option>
                    </select>
                </div>
                <div class="col-md-2 <?php  echo $current_type == 'open' ? '' : 'hidden';?>">
                    <label><?php print CommonLanguage::getMessage('type'); ?></label>
                    <select name="current_type" id="current_type" class="full-width" data-init-plugin="select2" > 
                        <option value="1"><?php echo CommonLanguage::getMessage('Add'); ?></option>
                        <option value="2"><?php echo CommonLanguage::getMessage('Edit'); ?></option>
                    </select>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12 table-responsive">
                    <table class="table table-bordered inventory" id="tableWithSearch" >
                        <thead>
                            <tr>
                                <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('subscription_name'); ?></th>
                                <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('customer_name'); ?></th>
                                <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('address'); ?></th>
                                <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('customer_subscription_title'); ?></th>
                                <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('product'); ?></th>
                                <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('previous'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                        <tfoot>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th>
                                <button class="btn btn-primary full-width" id="submit_cust_sub_open_balance"><?php echo CommonLanguage::getMessage('Save'); ?></button>
                            </th>
                        </tfoot>
                    </table>
                </div>
            </div>
            <?php echo Forms::ModalLoader('cust_sub_loader'); ?>
        </div>
    </div>
</div>
<script>
    $(function(){
        ChangeTable();
        build_select2($("#subscription_id"), "xhr/commonSearch?col_id=subscription_id&col_name=subscription_name&table=tbl_subscription");
        build_select2($("#customer_id"), "xhr/customerSearch");
        $('#subscription_id,#customer_id,#period_unit,#current_type').on('change',function(){
            ChangeTable();
        });

        $('#submit_cust_sub_open_balance').on('click',function(){
            var subscription_fees = {},is_empty_input = false,is_negative_value = false,is_zero_value = false,
                row = $("#tableWithSearch").dataTable().fnGetNodes();
            for (var i = 0; i < row.length; i++) {
                var parent = $(row[i]),
                    open_balance_div = $('input[name=open_balance]',parent),
                    customer_subscription_id = open_balance_div.attr('data-customer-subscription-id');
                if(!open_balance_div.val()){
                    is_empty_input = true;
                }
                if(parseFloat(open_balance_div.val()) < 0)
                    is_negative_value = true; 
                if(parseFloat(open_balance_div.val()) == 0)
                    is_zero_value = true;
                if(!(customer_subscription_id in  subscription_fees)){
                    var subscription_details = [];
                    subscription_details.push({current_quantity:open_balance_div.val(),
                                               subscription_detail_id:open_balance_div.attr('data-subscription-detail-id')});
                    subscription_fees[customer_subscription_id] = {customer_subscription_id:customer_subscription_id,
                                                                   main_date:open_balance_div.attr('data-main-date'),
                                                                   subscription_id:open_balance_div.attr('data-subscription-id'),
                                                                   subscription_details:subscription_details
                                                                  }
                }else{
                    subscription_fees[customer_subscription_id]['subscription_details'].push({current_quantity:open_balance_div.val(),
                                                                                              subscription_detail_id:open_balance_div.attr('data-subscription-detail-id')}); 

                }

            };
            if(is_empty_input){
                toastr.error('<?php echo CommonLanguage::getMessage('Required_Fields');?>');
                return;
            } 
            if(is_negative_value){
                toastr.error('<?php echo CommonLanguage::getMessage('negative_value');?>');
                return;
            }  
            if(is_zero_value && $('#current_balance_type').val() == 'open'){
                toastr.error('<?php echo CommonLanguage::getMessage('zero_value');?>');
                return;
            }
            $('#cust_sub_loader').removeClass('hidden');
            $.ajax({
                url: "InventoryAjax/submitCustSubOpenBalance",
                data: {subscription_fees:subscription_fees,current_balance_type :$('#current_balance_type').val()},
                method:"post",
                dataType:"json",
                complete: function (jqXHR, v2) {
                    if (v2 == 'success') {
                        if (jqXHR.responseJSON[1] == "success") {
                            ChangeTable();
                            toastr.success(jqXHR.responseJSON[0]);
                        }else{
                            toastr.error(jqXHR.responseJSON[0]);
                        }
                    }
                    $('#cust_sub_loader').addClass('hidden')
                }
            }); 
        });
    });
    function ChangeTable() {
        var data = {period_unit:$('#period_unit').val(),
                    customer_id:$('#customer_id').val(),
                    subscription_id:$('#subscription_id').val(),
                    current_type:$('#current_type').val(),
                    current_balance_type:$('#current_balance_type').val()}
        var optns = {
            cb_ft: function (dt_optns) {
                // stop sorting for column since on click on header checkbox the sorting is triggered
                dt_optns.columnDefs.push({targets: [0,1,2,3,4], "orderable": false});
            },
        };
        RebuildDataTable($('#panel-heading .panel-title').html(), [], "InventoryAjax/customer_subscription_open_balance", data,'', [0,1,2,3,4],optns);
        //        $.ajax({
        //            url: "InventoryAjax/customer_subscription_open_balance",
        //            data: data,
        //            complete: function (v1, v2) {
        //                if (v2 == 'success') {
        //                    $("#tableWithSearch").DataTable().destroy();
        //                    $("#tableWithSearch tbody").empty().html(v1.responseText);
        //                    var optns = {
        //                        cb_ft: function (dt_optns) {
        //                            // stop sorting for column since on click on header checkbox the sorting is triggered
        //                            dt_optns.columnDefs.push({targets: [0,1,2,3,4], "orderable": false});
        //                        },
        //                    };
        //                    RebuildDataTable($('#panel-heading .panel-title').html(), [], "", {}, '', [0,1,2,3,4],optns);
        //                }
        //            }
        //        });
    }
</script>