<style>
    .AddRow,.subscriptionDetailsMain{
        background: #e5e5e5;
        padding: 10px;
        border-radius: 6px;
        margin-bottom: 10px;
    }
    .fa-plus,.fa-minus{
        cursor:pointer;
    }
</style>
<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>
            <?php
            if ($permissions->hasPermission('add_subscription_setting')) {
                echo Forms::FloatingAddButton('4c0870', '#subscription_setting_modal', CommonLanguage::getMessage('add'));
            }
            ?>
        </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:15%" ><?php print CommonLanguage::getMessage('Name'); ?></th>
                                <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('currency'); ?></th>
                                <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('status'); ?></th>
                                <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('Action'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade " id="subscription_setting_modal" data-backdrop="static" >
    <div class="modal-dialog" style="width:85%;">
        <div class="modal-content">
            <div class="modal-header clearfix text-left">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="pg-close fs-20"></i>
                </button>
                <h5 class="add-label h-labels hidden bold"><?php echo CommonLanguage::getMessage('add_subscription_setting'); ?></h5>
                <h5 class="edit-label h-labels hidden bold"><?php echo CommonLanguage::getMessage('edit_subscription_setting'); ?></h5>
            </div>
            <div class="modal-body">
                <div class="row">
                    <?php echo Forms::AlertDiv('subscription_setting_alert'); ?>
                </div>
                <form id="add_subscription_setting_form" method="POST" onsubmit="return false">
                    <input type="hidden" name="subscription_id" id="subscription_id" />
                    <div class="row form-group">
                        <div class="col-md-2">
                            <label><?php print CommonLanguage::getMessage('Name'); ?></label>
                            <span class="red-star">*</span>
                            <input type="text" placeholder="" id="subscription_name" name="subscription_name" class="form-control" required>
                        </div>
                        <div class="col-md-2">
                            <label><?php print CommonLanguage::getMessage('currency'); ?></label>
                            <span class="red-star">*</span>
                            <input type="hidden" id="currency_id" name="currency_id" class="full-width" required>
                        </div> 
                        <div class="col-md-2">
                            <label><?php print CommonLanguage::getMessage('currency_round'); ?></label>
                            <span class="red-star">*</span>
                            <input type="number" id="currency_round" name="currency_round" class="form-control" required>
                        </div>
                        <div class="col-md-2">
                            <label><?php print CommonLanguage::getMessage('round_type'); ?></label>
                            <span class="red-star">*</span>
                            <select name="round_type" id="round_type" data-init-plugin="select2" class="full-width" required>
                                <option value=""><?php  echo CommonLanguage::getMessage('select');?></option>
                                <option value="0"><?php  echo CommonLanguage::getMessage('round');?></option>
                                <option value="1"><?php  echo CommonLanguage::getMessage('round_up');?></option>
                                <option value="2"><?php  echo CommonLanguage::getMessage('round_down');?></option>
                            </select>
                        </div>
                        <div class="col-md-2">
                            <label><?php print CommonLanguage::getMessage('period'); ?></label>
                            <span class="red-star">*</span>
                            <input type="number"  id="period" name="period" class="form-control" required>
                        </div>
                         <div class="col-md-2">
                            <label><?php print CommonLanguage::getMessage('period_unit'); ?></label>
                            <span class="red-star">*</span>
                            <select name="period_unit" id="period_unit" class="full-width" data-init-plugin="select2" required > 
                                <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>
                    <div class="row form-group">
                        <div class="col-md-2">
                            <label><?php print CommonLanguage::getMessage('bill_start'); ?></label>
                            <span class="red-star">*</span>
                            <span class="fa fa-info-circle" title="<?php  echo CommonLanguage::getMessage('bill_start_info');?>"></span>
                            <input type="text"  id="bill_start" name="bill_start" class="form-control" required>
                        </div>
                        <div class="col-md-2">
                            <label><?php print CommonLanguage::getMessage('Category'); ?></label>
                            <input type="hidden" id="category_id" name="category_id" class="full-width">
                        </div>
                        <div class="col-md-1">
                            <div><label><?php print CommonLanguage::getMessage('is_ttc'); ?></label></div>
                            <div><input type="checkbox" id="is_ttc"/></div>
                        </div> 
                        <div class="col-md-1">
                            <div><label><?php print CommonLanguage::getMessage('is_active'); ?></label></div>
                            <div><input type="checkbox" id="is_active" /></div>
                        </div>
                        <div class="col-md-1">
                            <div><label><?php print CommonLanguage::getMessage('paid_after'); ?></label></div>
                            <div><input type="checkbox" id="paid_type"/></div>
                        </div>

                    </div>
                    <div class="row form-group">

                    </div>
                    <hr>
                    <div id="subscription_details">
                        <div class="col-md-12 subscriptionDetailsMain m-b-10 form-group " data-detail-id ="0" >
                            <div class="row form-group">
                                <div class="col-md-4 ">
                                    <label ><?php print CommonLanguage::getMessage('product'); ?></label>
                                    <span class="red-star">*</span>
                                    <span class="fa fa-plus-circle m-l-5 openFrame" data-src="Inventory/listproduct?is_pop=1&is_new=1" style="color:#8a7dbe;cursor:pointer"></span>
                                    <input type="hidden" class="product_id full-width common-disabled req"/>
                                </div>
                                <div class="col-md-2">
                                    <label ><?php print CommonLanguage::getMessage('UOM'); ?></label>
                                    <span class="red-star">*</span>
                                    <span class="fa fa-plus-circle m-l-5 openFrame" data-src="Inventory/GeneralSetting?is_pop=1&ID=2" style="color:#8a7dbe;cursor:pointer"></span>
                                    <input type="hidden" class="uom_id full-width common-disabled req"/>
                                </div>
                                <div class="col-md-2">
                                    <label><?php print CommonLanguage::getMessage('type'); ?></label>
                                    <span class="red-star">*</span>
                                    <select name="type" class="type full-width common-disabled req" 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 class="col-md-2 hidden depend_previous_div m-t-20">
                                    <input type="checkbox" class="depend_previous" />
                                    <label><?php print CommonLanguage::getMessage('depend_previous'); ?></label>
                                </div>
                                <div class="col-md-2 pull-right AddButtons">
                                    <span class="fa fa-plus" style="color:#4c0870;margin-top:15px;margin-right:25px"></span>
                                    <span class="fa fa-minus" style="color:#ed9421;margin-top:15px;"></span>
                                </div>
                            </div>
                            <div class="row form-group">
                                <div class="subscription_detail_div ">
                                    <div class="row ">
                                        <div class="col-xs-2 p-l-5 start_end_div">
                                            <label ><?php print CommonLanguage::getMessage('start'); ?></label>
                                        </div>
                                        <div class="col-xs-2 start_end_div">
                                            <label ><?php print CommonLanguage::getMessage('end'); ?></label>
                                        </div>
                                        <div class="col-xs-2">
                                            <label ><?php print CommonLanguage::getMessage('Price'); ?></label>
                                            <span class="red-star">*</span>
                                        </div>
                                        <div class="col-xs-2">
                                            <label ><?php print CommonLanguage::getMessage('fees_type'); ?></label>
                                            <span class="red-star">*</span>
                                        </div>
                                    </div>
                                    <div class="subscription-details-items">
                                        <div class="row  mp-row form-group">
                                            <div class="col-xs-2 p-l-5 start_end_div">
                                                <input type="text"   name="start" class="form-control start">
                                            </div>
                                            <div class="col-xs-2 start_end_div">
                                                <input type="text"   name="end" class="form-control end">
                                            </div>
                                            <div class="col-xs-2">
                                                <input type="number" min="1"   name="price" class="form-control price">
                                            </div>
                                            <div class="col-xs-2" >
                                                <select name="fees_type_id" class="full-width fees_type_id" data-init-plugin="select2"> 
                                                    <option value=""><?php echo CommonLanguage::getMessage('select'); ?></option>
                                                    <option value="1"><?php echo CommonLanguage::getMessage('fixed'); ?></option>
                                                    <option value="2"><?php echo CommonLanguage::getMessage('per_qty'); ?></option>
                                                </select>
                                            </div>
                                            <div class="col-xs-2">
                                                <span class="fa fa-plus sub_action_btn" style="color:#4c0870;margin-top:15px;margin-right:25px"></span>
                                                <span class="fa fa-minus sub_action_btn" style="color:#ed9421;margin-top:15px;"></span>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <hr>
                </form>
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" id="CloseProduct" data-dismiss="modal"  >  <?php print CommonLanguage::getMessage('Close'); ?></button>
                <button class="btn btn-primary" id="submit_subscription_setting"  >   <?php print CommonLanguage::getMessage('Save'); ?> </button>
            </div>
            <?php echo Forms::ModalLoader('subscription_setting_loader'); ?>
        </div>
    </div>
</div>
<script>
    $(function () {
        RebuildDataTable($('.panel-heading .panel-title').html(), [0], "InventoryAjax/subscription_list_ajax");
        $('#tableWithSearch').on('click', '.delete', function () {
            var entry_value = $(this).parents('div.Action').attr('data-id');
            IncludeDeleteModal(common_delete_modal, {subscription_id: entry_value}, function () {
                RebuildDataTable($('.panel-heading .panel-title').html(), [0], "InventoryAjax/subscription_list_ajax");
            }, "InventoryAjax/deleteSubscriptionSetting");
        });
        build_select2($("#category_id"), "xhr/commonSearch?table=tbl_subscription_category&col_id=category_id&col_name=category_name");
        build_select2($("#currency_id"), "xhr/CurrencySearch");
        build_select2($("input.product_id"), "xhr/ProductSearch?Status_ID=1");
        build_select2($("input.uom_id"), "xhr/productuomSearch?pid=" + $("input.product_id").val());

        new Switchery($("#is_active").get(0), {color: $.Pages.getColor("primary")});
        new Switchery($("#is_ttc").get(0), {color: $.Pages.getColor("primary")});
        new Switchery($("#paid_type").get(0), {color: $.Pages.getColor("primary")});
        $(".floatingContainer").on("click", function () {
            $('#subscription_id').val(0);
            $('#subscription_setting_modal .add-label').removeClass('hidden');
            if(!$('#is_active').is(":checked")){
                $('#is_active').trigger('click');
            }
            if(!$('#paid_type').is(":checked")){
                $('#paid_type').trigger('click');
            }
            $(".AddButtons .fa-minus").css("display", "inline-block");
            $(".common-disabled").attr('disabled',false);
        });
        $("#subscription_details").on("click", ".AddButtons .fa-plus", function (e) {
            //            $("#subscription_details .AddButtons .fa-minus").css("display", "inline-block")
            var Parent = $(this).parents(".subscriptionDetailsMain"), NewItem = Parent.clone().insertAfter(Parent);
            NewItem.find("input").val('');
            NewItem.attr('data-detail-id', 0);
            NewItem.find(".select2-container").remove();
            $('input.start,input.end', NewItem).attr('readonly', false);
            $('.start_end_div',NewItem).removeClass('hidden')  
            $('.depend_previous',NewItem).prop('checked',false)
            $('.depend_previous_div',NewItem).addClass('hidden')  
            $('.common-disabled',NewItem).attr('disabled',false)  
            $(".subscription-details-items .mp-row .sub_action_btn", NewItem).css("display", "inline-block");
            $('.subscription_detail_div .subscription-details-items .mp-row:gt(0)', NewItem).remove();
            build_select2($("input.product_id", NewItem), "xhr/ProductSearch?Status_ID=1")
            build_select2($("input.uom_id", NewItem), "xhr/productuomSearch?pid=" + $("input.product_id", NewItem).val());
            $('select.fees_type_id,select.type', NewItem).select2();
            NewItem.find(".AddButtons .fa-minus").css("display", "inline-block");
            $(".select2-container", NewItem).show()
        });
        $('#subscription_details').on('change','select.fees_type_id',function(){
            var parent = $(this).parents('.subscriptionDetailsMain');
            if(($('select.type',parent).val() == 1 && $(this).val() == 2) ||  ($('select.type',parent).val() >= 2)){
                $('.depend_previous_div',parent).removeClass('hidden')
            }else{
                $('.depend_previous_div',parent).addClass('hidden')
                $('.depend_previous',parent).prop('checked',false);
            }
        });
        $('#subscription_details').on('change','.type',function(){
            var parent = $(this).parents('.subscriptionDetailsMain');
            $('input.start,input.end',parent).val($(this).val()== 1 ? 0 : '').attr('readonly',$(this).val()== 1 ? true : false);
            $(".subscription-details-items .mp-row .sub_action_btn",parent).css("display",$(this).val()== 1  ? "none" : "inline-block");
            if($(this).val()== 1){
                $(".subscription-details-items .mp-row:gt(0)",parent).remove();
                $('.start_end_div',parent).addClass('hidden');
                $('.depend_previous_div',parent).addClass('hidden');
                $('.depend_previous',parent).prop('checked',false);
            }else {
                $('.depend_previous_div',parent).removeClass('hidden') 
                $('.start_end_div',parent).removeClass('hidden');
            }
            !$(this).val() ? $('.depend_previous_div',parent).addClass('hidden') : '';
        });
        $("#subscription_details").on('change', 'input.product_id', function () {
            var parent = $(this).parents('.subscriptionDetailsMain');
            $("input.uom_id", parent).val('').trigger('change');
            build_select2($("input.uom_id", parent), "xhr/productuomSearch?pid=" + $(this).val());
        });
        $("#subscription_details").on("click", " .AddButtons .fa-minus", function (e) {
            if ($(".subscriptionDetailsMain").length > 1) {
                $(this).closest(".subscriptionDetailsMain").empty().remove();
            }
        });
        $("#subscription_details").on("click", ".subscription-details-items .fa-plus", function (e) {
            var Parent = $(this).parents(".mp-row"), NewItem = Parent.clone().insertAfter(Parent);
            NewItem.find("input").val('');
            NewItem.find(".select2-container").remove();
            $('select.fees_type_id', NewItem).select2();
            $(".select2-container", NewItem).show()
        });
        $("#subscription_details").on("click", ".subscription-details-items .fa-minus", function (e) {
            var parent = $(this).parents('.subscriptionDetailsMain');
            if ($(".mp-row", parent).length > 1) {
                $(this).closest(".mp-row").empty().remove();
            }
        });
        $('#subscription_setting_modal').on('hidden.bs.modal', function () {
            $('.subscriptionDetailsMain:gt(0)').remove();
            $('.subscriptionDetailsMain').attr('data-detail-id',0);
            $('.subscription-details-items .mp-row:gt(0)').remove();
            $('.start_end_div').removeClass('hidden')
            $('.subscriptionDetailsMain input,.subscriptionDetailsMain select').val('').trigger('change');
        });
        $('#period_unit').on('change',function(){
            $("#bill_start").val('');
            if($(this).val() == 1){
                $("#bill_start").datepicker();
                $("#bill_start").data('datepicker').remove();
            }else if($(this).val() == 2){
                $("#bill_start").datepicker();
                $("#bill_start").data('datepicker').remove();
            }else{
                $("#bill_start").datepicker({
                    changeMonth: true,
                    changeYear: false,
                    autoclose:true,
                    showButtonPanel: true,
                    format: 'mm-dd'
                });
            }
        });
        $("#submit_subscription_setting").on("click", function () {
            if (!$("#add_subscription_setting_form").valid()) {
                return;
            }
            var Data = new FormData(document.getElementById("add_subscription_setting_form"));
            if($('#period_unit').val() == 1){
                if(parseInt($('#bill_start').val()) > 7 || parseInt($('#bill_start').val()) < 1){
                    AddAlert("#subscription_setting_alert", "<?php echo CommonLanguage::getMessage('bill_start_week'); ?>", "danger");
                    return; 
                }
            }else if($('#period_unit').val()  == 2){
                if(parseInt($('#bill_start').val()) > 31 || parseInt($('#bill_start').val()) < 1){
                    AddAlert("#subscription_setting_alert", "<?php echo CommonLanguage::getMessage('bill_start_month'); ?>", "danger");
                    return; 
                }
            }
            var subscription_details = [], subscription_details_ids = [],
                subscription_details_empty= false,subscription_details_fees_empty = false;
            $('.subscriptionDetailsMain').each(function (main_index) {
                var parent = $(this), type = $('select.type', parent).val();
                var subscription_details_fees = [],fees_type_id,not_empty_count=0;//required-fields
                $('input.req,select.req',parent).each(function(){
                    if($(this).val())
                        not_empty_count++;
                });
                if(not_empty_count != 0 && not_empty_count < 3)
                    subscription_details_empty = true;
                if($('input.uom_id', parent).val() && $('input.product_id', parent).val() && type){
                    $(".mp-row", parent).each(function (index) {
                        if ($("select.fees_type_id", $(this)).val() && $("input.price", $(this)).val()) {
                            subscription_details_fees.push({start: $(".start", $(this)).val(),
                                                            end: $(".end", $(this)).val(),
                                                            price: $("input.price", $(this)).val(),
                                                            fees_type_id: $("select.fees_type_id", $(this)).val()});
                            fees_type_id =$("select.fees_type_id", $(this)).val();
                        }else{
                            subscription_details_fees_empty = true;
                        }
                    });
                    subscription_details.push({product_id: $('input.product_id', parent).val(),
                                               uom_id: $('input.uom_id', parent).val(),
                                               type_id: type,
                                               detail_id: parent.attr('data-detail-id'),
                                               depend_previous: ((type== 1 && fees_type_id == 1) ? 0 : ($('input.depend_previous:checked',parent).length > 0 ? 1 : 0)),
                                               subscription_details_fees: subscription_details_fees});
                    if (parent.attr('data-detail-id') > 0)
                        subscription_details_ids.push(parent.attr('data-detail-id'));
                }
            });
            if (subscription_details.length == 0) {
                AddAlert("#subscription_setting_alert", "<?php echo CommonLanguage::getMessage('input_option'); ?>", "danger");
                return;
            }
            if (subscription_details_empty || subscription_details_fees_empty) {
                AddAlert("#subscription_setting_alert", "<?php echo CommonLanguage::getMessage('input_option'); ?>", "danger");
                return;
            }
            Data.append('is_ttc', $('#is_ttc').is(":checked") ? 1 : 0)
            Data.append('is_active', $('#is_active').is(":checked") ? 1 : 0)
            Data.append('paid_type', $('#paid_type').is(":checked") ? 1 : 0)
            Data.append('subscription_details', JSON.stringify(subscription_details));
            Data.append('subscription_details_ids', JSON.stringify(subscription_details_ids));
            //            return;
            $("#subscription_setting_loader").removeClass('hidden');
            $.ajax({
                url: "InventoryAjax/submitSubscriptionSetting",
                method: "POST",
                data: Data,
                dataType: "json",
                processData: false,
                contentType: false,
                complete: function (jqXHR, v2) {
                    if (v2 == 'success') {
                        if (jqXHR.responseJSON[1] == "success") {
                            hideModalAlert('subscription_setting_modal', 'subscription_setting_alert');
                            RebuildDataTable($('.panel-heading .panel-title').html(), [0], "InventoryAjax/subscription_list_ajax");
                        }
                        AddAlert("#subscription_setting_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#subscription_setting_loader").addClass('hidden');
                }
            });
        });

        $('#tableWithSearch').on('click', '.edit', function () {
            $("#subscription_setting_modal .edit-label").removeClass('hidden');
            var subscription_id = $(this).parents('div.Action').attr('data-id');
            $('#subscription_id').val(subscription_id);
            CommonViewById('InventoryAjax/getSubscriptionSettingData', {subscription_id: subscription_id},
                           '#subscription_setting_loader', function (response) {
                var subscription_main = response['main'], subscription_details = response['details'],
                    fees_details_row = $("#subscription_details .subscription-details-items .mp-row:eq(0)").clone(),
                    current_details_row = $('#subscription_details .subscriptionDetailsMain:eq(0)').clone();

                $('#subscription_name').val(subscription_main.subscription_name);
                $('#currency_round').val(subscription_main.currency_round);
                $('#period').val(subscription_main.period);
                $('#period_unit').val(subscription_main.period_unit).select2();
                $('#round_type').val(subscription_main.round_type).select2();
                $('#bill_start').val(subscription_main.bill_start);
                if((subscription_main.is_active == 1 && !$('#is_active').is(":checked")) || (subscription_main.is_active == 0 && $('#is_active').is(":checked"))){
                    $('#is_active').trigger('click');     
                }
                if( (subscription_main.is_ttc == 1 && !$('#is_ttc').is(":checked")) || (subscription_main.is_ttc == 0 && $('#is_ttc').is(":checked"))){
                    $('#is_ttc').trigger('click');     
                } 
                if( (subscription_main.paid_type == 1 && !$('#paid_type').is(":checked")) || (subscription_main.paid_type == 0 && $('#paid_type').is(":checked"))){
                    $('#paid_type').trigger('click');     
                }
                $('#currency_id').val(subscription_main.currency_id).attr('data-select-name', subscription_main.Currency_Name + ' - ' + subscription_main.Currency_Code).trigger('change');
                $('#category_id').val(subscription_main.category_id).attr('data-select-name', subscription_main.category_name).trigger('change');
                if (subscription_details.length > 0) {
                    $("#subscription_details .subscriptionDetailsMain").remove();
                    $.each(subscription_details, function (index, value) {
                        var fees_details = subscription_details[index]['fees_details'],
                            current_type_id = subscription_details[index]['type_id'];
                        current_details_row = current_details_row == null ? $("#subscription_details .subscriptionDetailsMain:eq(0)").clone() : current_details_row;
                        $(".select2-container", current_details_row).remove();
                        $(".subscription_detail_div .subscription-details-items .mp-row", current_details_row).remove();
                        current_details_row.attr('data-detail-id', subscription_details[index]['detail_id']);
                        $('input.product_id', current_details_row).val(subscription_details[index]['product_id']).attr('data-select-name', subscription_details[index]['product_name']);
                        $('input.uom_id', current_details_row).val(subscription_details[index]['uom_id']).attr('data-select-name', subscription_details[index]['UOM_Name']);
                        build_select2($("input.product_id", current_details_row), "xhr/ProductSearch?Status_ID=1")
                        build_select2($("input.uom_id", current_details_row), "xhr/productuomSearch?pid=" + subscription_details[index]['product_id']);
                        $('select.type', current_details_row).val(subscription_details[index]['type_id']).select2();
                        $(".AddButtons .fa-minus",current_details_row).css("display",subscription_details[index]['entry_id']  ? "none" : "inline-block");
                        $(".common-disabled",current_details_row).attr("disabled",subscription_details[index]['entry_id']  ? true : false);
                        $("#subscription_details").append(current_details_row);
                        $(".select2-container", current_details_row).show();
                        var fees_type_id;
                        if(fees_details.length > 0){
                            $("#subscription_details .subscription_detail_div .subscription-details-items .mp-row", current_details_row).remove();
                            $.each(fees_details, function (j, val) {
                                fees_details_row = fees_details_row == null ? $("#subscription_details .subscription-details-items .mp-row:eq(0)").clone() : fees_details_row;
                                $(".select2-container", fees_details_row).remove();
                                $('input.start', fees_details_row).val(fees_details[j]['start']).attr('readonly',current_type_id == 1 ? true :false);
                                $('input.end', fees_details_row).val(fees_details[j]['end']).attr('readonly',current_type_id == 1 ? true : false);
                                $('input.price', fees_details_row).val(fees_details[j]['price']);
                                $('select.fees_type_id', fees_details_row).val(fees_details[j]['fees_type_id']).select2();
                                $(".subscription-details-items", current_details_row).append(fees_details_row); 
                                $(".subscription-details-items .mp-row .sub_action_btn",current_details_row).css("display",current_type_id == 1  ? "none" : "inline-block");
                                fees_type_id = fees_details[j]['fees_type_id'];
                                $(".select2-container", fees_details_row).show();
                                fees_details_row = null;
                            });
                            if (current_type_id == 1){
                                $('.start_end_div',current_details_row).addClass('hidden')
                            }else{
                                $('.start_end_div',current_details_row).removeClass('hidden')  
                            }
                        }
                        if((subscription_details[index]['type_id'] >= 2) || (fees_type_id == 2 && subscription_details[index]['type_id'] == 1)){
                            $('.depend_previous_div',current_details_row).removeClass('hidden');
                            $('input.depend_previous',current_details_row).prop('checked',subscription_details[index]['depend_previous'] == 1 ? true :false); 
                        }else{
                            $('input.depend_previous',current_details_row).prop('checked',false); 
                            $('.depend_previous_div',current_details_row).addClass('hidden'); 
                        }
                        current_details_row = null
                    });
                }
            });
        });
    });
</script>