<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title">
                <h4 class="bold"><?php echo CommonLanguage::getMessage($title); ?> </h4>
            </div>
            <div class="row">
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage('subscription_name') ?></label>
                    <input id="subscription_id" type="hidden" class="full-width" name="subscription_id" >
                </div>
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage('product') ?></label>
                    <input id="product_id" type="hidden" class="full-width" name="product_id" >
                </div> 
                <div class="col-md-3">
                    <label><?php print CommonLanguage::getMessage('type'); ?></label>
                    <select name="type_id" class="type full-width" id="type_id" data-init-plugin="select2"  > 
                        <option value=""><?php echo CommonLanguage::getMessage('select'); ?></option>
                        <option value="1"><?php echo CommonLanguage::getMessage('recurring'); ?></option>
                        <option value="2"><?php echo CommonLanguage::getMessage('usage'); ?></option>
                        <option value="3"><?php echo CommonLanguage::getMessage('incremental'); ?></option>
                    </select>
                </div>
            </div>
        </div>
        <div class="panel-body">
            <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:5%" ><?php echo CommonLanguage::getMessage('subscription_name') ?></th>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('product'); ?></th> 
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('UOM'); ?></th>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('start'); ?></th>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('end'); ?></th>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('price'); ?></th>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('currency'); ?></th>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('fees_type'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                        <tfoot>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th>
                                <button class="btn btn-primary full-width no-print" id="submit_subs_details_fees_prices"><?php echo CommonLanguage::getMessage('Save'); ?></button>
                            </th>
                        </tfoot>
                    </table>
                </div>
            </div>
            <?php echo Forms::ModalLoader('subs_details_fees_loader'); ?>
        </div>
    </div>
</div>
<script>
    $(function () {
        build_select2($("#subscription_id"), "xhr/commonSearch?col_id=subscription_id&col_name=subscription_name&table=tbl_subscription");
        build_select2($('#product_id'), 'xhr/ProductSearch?Status_ID=1');
        ChangeTable();
        $('#subscription_id,#product_id,#type_id').on('change', function () {
            ChangeTable();
        });
        $('#submit_subs_details_fees_prices').on('click',function(){
            var subscription_details_fees = [],
                is_empty_input = false,is_neagtive_value =false;
            $('#tableWithSearch tbody tr').each(function(){
                var parent = $(this),current_price_tag = $('input[name=current_price]',parent);
                if(!current_price_tag.val()){
                    is_empty_input = true;
                }
                if(current_price_tag.val() < 0){
                    is_neagtive_value = true;
                }
                subscription_details_fees.push({detail_fees_id:current_price_tag.attr('data-id'),
                                                price:current_price_tag.val()});
            });
            if(is_empty_input){
                toastr.error('<?php echo CommonLanguage::getMessage('input_field_empty');?>');
                return;
            } 
            if(is_neagtive_value){
                toastr.error('<?php echo CommonLanguage::getMessage('negative_value');?>');
                return;
            }
            $('#subs_details_fees_loader').removeClass('hidden');
            $.ajax({
                url: "InventoryAjax/submitSubscriptionDetailsFees",
                data: {subscription_details_fees:subscription_details_fees},
                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]);
                        }
                    }
                    $('#subs_details_fees_loader').addClass('hidden')
                }
            }); 
        });
    });
    function ChangeTable() {
        var Data = {
            subscription_id: $('#subscription_id').val(),
            product_id: $("#product_id").val(),
            type_id: $("#type_id").val()
        }
        RebuildDataTable($('.panel-heading .panel-title').html(), [0, 1, 2, 3, 4, 5, 6], "InventoryAjax/subscription_details_list", Data,'', [0,1,2]);

    }
</script>