
<style>
    .table thead tr >th{
        background-color: #4c0870;
        color:#fff;
    }
    .table thead tr th[class*='sorting_'] {
        color: #FFFFFF;
    }
    .bg-master-lighter {
        border:none;
        background-color:#fff;
        border-top:1px solid #ddd; 

    }
    .btn-default{
        border: none;
        background-color: #eee;
    }
    .btn-default:hover{
        background-color:#ed9421 !important;
        color:#fff;
        border: none;
    }
    .p-0{
        padding:0!important;
    }
    /*    .main-div{
    
            background: #4c0870;
            padding: 10px;
            border-radius: 6px;
            margin-bottom: 10px;
    
        }*/
</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 CommonLanguage::getMessage('dynamic_report_blocks'); ?></div>

            <div class="clearfix"></div>
        </div>
        <div class="panel-body">
            <?php
            if ($permissions->hasPermission('add_dynamic_report_blocks')) {
                echo Forms::FloatingAddButton('4c0870', '#add_block', CommonLanguage::getMessage('add'));
            }
            ?>
            <table class="table table-bordered table-hover demo-table-search" 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('Action'); ?></th>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>



    </div>
</div>
<div class="modal fade" data-backdrop="static" id="add_block" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
    <div class="modal-dialog " style="width:90%;" >
        <div class="modal-content">
            <div class="modal-header clearfix text-left">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true"><i class="pg-close fa-22"></i></span></button>
                <h4 class="modal-title"><?php print CommonLanguage::getMessage('dynamic_report_blocks') ?></h4>
                <div id="assigAlert"></div>
            </div>
            <div class="modal-body">
                <form id="assigForm">

                    <div class="row">

                        <div class="main-div row m-t-15 form-group">
                            <div id="row-divs"  class="col-md-12">

                                <div class="col-md-6">
                                    <label><?php echo CommonLanguage::getMessage('title') ?><span class="red-star">*</span></label>
                                    <input type="text" id="sub_title" class="form-control full-width sub_title"/>
                                </div>
                                <div class="col-md-12">
                                    <div class="col-md-2 form-group" >
                                        <label><?php print CommonLanguage::getMessage('type'); ?><span class="red-star">*</span></label>
                                    </div>
                                    <div class="col-md-2 form-group" >
                                        <label><?php print CommonLanguage::getMessage('account'); ?><span class="red-star">*</span></label>
                                    </div>
                                    <div class="col-md-2 form-group" >
                                        <label><?php print CommonLanguage::getMessage('title'); ?><span class="red-star">*</span></label>
                                    </div>
                                    <div class="col-md-2 form-group" >
                                        <label><?php print CommonLanguage::getMessage('operation'); ?><span class="red-star">*</span></label>
                                    </div>
                                    <div class="col-md-1 form-group" >
                                        <label><?php print CommonLanguage::getMessage('with_child'); ?><span class="red-star">*</span></label>
                                    </div>
                                    <div class="col-md-2 form-group" >
                                        <label><?php print CommonLanguage::getMessage('types'); ?></label>
                                    </div>

                                </div>
                                <div class="col-md-12 row-item">
                                    <div class="col-md-2 form-group" >
                                        <select class="sub_type full-width" data-init-plugin="select2">
                                            <option value="0"><?php echo CommonLanguage::getMessage('select') ?></option>
                                            <option value="1"><?php echo CommonLanguage::getMessage('Account') ?></option>
                                            <option value="2"><?php echo CommonLanguage::getMessage('block') ?></option>
                                            <option value="3"><?php echo CommonLanguage::getMessage('stock') ?></option>
                                        </select>
                                    </div>
                                    <div class="col-md-2 form-group " >
                                        <div class="acc_div">
                                            <input type="hidden" class="full-width account_id" />
                                        </div>
                                    </div>
                                    <div class="col-md-2 form-group" >
                                        <input type="text" class="form-control account_title" />
                                    </div>
                                    <div class="col-md-2 form-group" >
                                        <select class="full-width operation" data-init-plugin="select2">
                                            <option value=""><?php echo CommonLanguage::getMessage('Select') ?></option>
                                            <option value="+">+</option>
                                            <option value="-">-</option>
                                        </select>
                                    </div>
                                    <div class="col-md-1 form-group " >
                                        <input type="checkbox" class=" with_child" />
                                    </div>
                                    <div class="col-md-2 form-group trans_div" >
                                        <select  class="full-width trans_type" multiple  data-init-plugin="select2">
                                            <option value="inv"><?php echo CommonLanguage::getMessage('invoice') ?></option>
                                            <option value="pur"><?php echo CommonLanguage::getMessage('purchase') ?></option>
                                            <option value="pay"><?php echo CommonLanguage::getMessage('payment') ?></option>
                                            <option value="rec"><?php echo CommonLanguage::getMessage('receipt') ?></option>
                                            <option value="mjv"><?php echo CommonLanguage::getMessage('manual_jv') ?></option>
                                            <option value="exp"><?php echo CommonLanguage::getMessage('expense') ?></option>
                                            <option value="rin"><?php echo CommonLanguage::getMessage('return_invoice') ?></option>
                                            <option value="rpr"><?php echo CommonLanguage::getMessage('return_purchase') ?></option>
                                            <option value="dwt"><?php echo CommonLanguage::getMessage('deposit_withdrawal') ?></option>
                                            <option value="cre"><?php echo CommonLanguage::getMessage('credit_note') ?></option>
                                            <option value="deb"><?php echo CommonLanguage::getMessage('debit_note') ?></option>
                                        </select>
                                    </div>
                                    <div class="col-md-2 form-group stock_div hidden" >
                                        <select  class="full-width stock_type"   data-init-plugin="select2">
                                            <option value=""><?php echo CommonLanguage::getMessage('Select') ?></option>
                                            <option value="begin"><?php echo CommonLanguage::getMessage('begining_stock') ?></option>
                                            <option value="end"><?php echo CommonLanguage::getMessage('ending_stock') ?></option>
                                        </select>
                                    </div>
                                    <div class="col-md-1 add-row">
                                        <span class="sub_add fa fa-plus-circle" style="color:#4c0870;margin-top:15px;margin-right:25px"></span>
                                        <span class="sub_delete fa fa-minus-circle" style="color:#ed9421;margin-top:15px;"></span>
                                    </div>
                                </div>
                            </div>

                        </div>

                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal"><?php print CommonLanguage::getMessage('Close') ?></button>
                <button type="button" class="btn bg-complete white" id="assignBtn" style="width:auto"><?php print CommonLanguage::getMessage('Save') ?></button>
            </div>
        </div>
    </div>
</div>

<script>
    var title = <?php echo json_encode(CommonLanguage::getMessage('dynamic_report_blocks')); ?>,
            Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>,
            Required_Fields = <?php echo json_encode(CommonLanguage::getMessage('Required_Fields')); ?>,
            Failed_Submit = <?php echo json_encode(CommonLanguage::getMessage('Failed_Submit')); ?>;
    var IsEdit = false, IDToEdit = 0;
    $(function () {
        build_select2($('input.account_id'), 'xhr/allAccountsSearch')

        $(".floatingContainer").on("click", function () {
            $(".row-item:gt(0)").remove()
            $('input.account_id').val("").attr("data-select-name", "")
            build_select2($('input.account_id'), 'xhr/allAccountsSearch')
            $("select.operation").val("").select2()
            IsEdit = false;
            IDToEdit = 0;
        })
        RebuildDataTable(title, [0], "ACCAjax/dynamic_report_blocks_ajax")
        $("#row-divs").on("change", 'select.sub_type', function () {
            var parent = $(this).parents('.row-item');
            $('input.account_id', parent).val("").attr("data-select-name", "")
            var url = $(this).val() == 1 ? 'xhr/allAccountsSearch' : 'xhr/search_dynamic_blocks';
            build_select2($('input.account_id', parent), url)
            if ($(this).val() != 1) {
                $("select.trans_type", parent).val([]).prop('readonly', true).select2()
            } else {
                $("select.trans_type", parent).prop('readonly', false).select2()
            }
            if ($(this).val() == 3) {
                $(".stock_div", parent).removeClass("hidden")
                $(".trans_div", parent).addClass("hidden")
                $(".account_id", parent).parents('.acc_div').addClass("hidden")
            } else {
                $(".stock_div", parent).addClass("hidden")
                $(".trans_div", parent).removeClass("hidden")
                $(".account_id", parent).parents('.acc_div').removeClass("hidden")
            }
        })
        $("#row-divs").on("click", " .add-row .sub_add", function (e) {
            $("#row-divs  .sub_delete").css("display", "inline-block")
            var Parent = $(this).parents(".row-item"), NewItem = Parent.clone().insertAfter(Parent);
            NewItem.find("input").val('');
            $(".select2-container", NewItem).empty().remove()
            $('input.with_child', NewItem).prop("checked", false)
            $('input.account_id', NewItem).val("").attr("data-select-name", "")
            $('select.sub_type', NewItem).val("0").select2()
            $('select.trans_type', NewItem).val("").select2()
            $('select.stock_type', NewItem).val("").select2()
            build_select2($('input.account_id', NewItem), 'xhr/allAccountsSearch')
            $("select.operation", NewItem).val("").select2()
            $(".select2-container", NewItem).show()

        })
        $("#row-divs").on("click", ".add-row .sub_delete", function (e) {
            var main_div = $(this).parents('.main-div');
            if ($(".row-item", main_div).length == 1) {
                $('input.account_id', main_div).val("").attr("data-select-name", "")
                build_select2($('input.account_id', main_div), 'xhr/allAccountsSearch')
                $("select.operation", main_div).val("").select2()
                $("input.with_child", main_div).prop("checked", false)
            } else {
                $(this).parents(".row-item").empty().remove();
            }
        })



        $("#assignBtn").on("click", function () {
            var validate = true;
            if ($("#sub_title").val() == '') {
                validate = false;
            }
            var data = {
                sub_title: $("#sub_title").val(),
                is_edit: (IsEdit ? 1 : 0),
                sub_id: IDToEdit,
                sub: []
            };


            $("#row-divs .row-item").each(function () {
                if ($(this).find("select.sub_type").val() != '0'
                        && ($(this).find('select.sub_type').val() == 3 || ($(this).find('select.sub_type').val() != 3 && $(this).find("input.account_id").val() != ''))
                        && $(this).find("select.operation").val() != "") {
//                    console.log('here')
                    data.sub.push({
                        account_id: $(this).find("input.account_id").val(),
                        sub_type: $(this).find('select.sub_type').val(),
                        trans_type: $(this).find('select.trans_type').val(),
                        stock_type: $(this).find('select.stock_type').val(),
                        detail_sub_title: $(this).find('input.account_title').val(),
                        operation: $(this).find("select.operation").val(),
                        with_child: $(this).find("input.with_child").is(":checked") ? 1 : 0
                    })
                } else {
                    validate = false;
                }
            })




            if (!validate || data.sub.length == 0) {
                $("#assigAlert").attr("class", "alert alert-danger").html(Required_Fields)
                return false;
            }
            $.ajax({
                url: "ACCAjax/add_edit_dynamic_report_blocks",
                method: "POST",
                data: data,
                complete: function (v1, v2) {
                    if (v2 == "success" && v1.responseText == 'OK') {
                        $("#assigAlert").attr("class", "alert alert-success").html(Submitted_Successfully)
                        RebuildDataTable(title, [0], "ACCAjax/dynamic_report_blocks_ajax")
                        setTimeout(function () {
                            $("#assigAlert").addClass('hidden')
                            $("#add_block").modal("hide")
                        }, 5000)
                    } else {
                        $("#assigAlert").attr("class", "alert alert-danger").html(Failed_Submit)
                    }
                }
            })

        })

        $("#tableWithSearch").on("click", ".delete", function () {
            var entry_value = $(this).parents('.Action').attr('data-id');
            var data = {
                id: entry_value
            };
            IncludeDeleteModal(common_delete_modal, data, function () {
                RebuildDataTable(title, [0], "ACCAjax/dynamic_report_blocks_ajax")

            }, "ACCAjax/delete_dynamic_report");
        })

        $("#tableWithSearch tbody").on('click', '.Edit', function () {
            IsEdit = true;
            IDToEdit = $(this).parents('.Action').attr('data-id');
            $.ajax({
                url: "ACCAjax/get_dynamic_report_block_by_id",
                method: "GET",
                dataType: 'json',
                data: {id: IDToEdit},
                complete: function (v1, v2) {
                    if (v2 == "success") {
                        var data = v1.responseJSON['main'],
                                details = v1.responseJSON['details'];
                        $(".row-item:gt(0)").remove()

                        var current_div = '';
                        $("#sub_title").val(data['sub_title'])
                        var y = 0;
                        $.each(details, function (index, value) {
//                            console.log(value['sub_type'])
                            if (y == 0) {
                                $(".row-item:eq(0) input.account_id").val(value['sub_type'] == 1 ? value['account_id'] : value['detail_sub_id']).attr("data-select-name", (value['sub_type'] == 1 ? value['account_name'] : value['sub_title']))
                                var url = value['sub_type'] == 1 ? 'xhr/allAccountsSearch' : 'xhr/search_dynamic_blocks';
                                build_select2($(".row-item:eq(0) input.account_id"), url)
                                $(".row-item:eq(0) select.operation").val(value['operation']).select2()
                                $(".row-item:eq(0) select.sub_type").val(value['sub_type']).select2()
                                $(".row-item:eq(0) select.trans_type").val(value['trans_types']).select2()
                                $(".row-item:eq(0) select.stock_type").val(value['stock_type']).select2()
                                $(".row-item:eq(0) input.account_title").val(value['detail_sub_title'])
                                $(".row-item:eq(0) input.with_child").prop("checked", value['with_child'] == 1 ? true : false)
                                if (value['sub_type'] != 1) {
                                    $(".row-item:eq(0) select.trans_type").val([]).prop('readonly', true).select2()
                                } else {
                                    $(".row-item:eq(0) select.trans_type").prop('readonly', false).select2()
                                }
                                if (value['sub_type'] == 3) {
                                    $(".row-item:eq(0) .stock_div").removeClass("hidden")
                                    $(".row-item:eq(0) .trans_div").addClass("hidden")
                                    $('.row-item:eq(0) .acc_div').addClass("hidden")
                                } else {
                                    $(".row-item:eq(0) .stock_div").addClass("hidden")
                                    $(".row-item:eq(0) .trans_div").removeClass("hidden")
                                    $('.row-item:eq(0) .acc_div').removeClass("hidden")
                                }
                            } else {
                                var item = $(".row-item:eq(0)").clone();
                                $(".select2-container", item).empty().remove()
                                item.find("input.account_id").val(value['sub_type'] == 1 ? value['account_id'] : value['detail_sub_id']).attr("data-select-name", (value['sub_type'] == 1 ? value['account_name'] : value['sub_title']))
                                var url = value['sub_type'] == 1 ? 'xhr/allAccountsSearch' : 'xhr/search_dynamic_blocks';
                                build_select2(item.find("input.account_id"), url)
                                item.find("select.operation").val(value['operation']).show().select2()
                                item.find("select.sub_type").val(value['sub_type']).show().select2()
                                item.find("select.trans_type").val(value['trans_types']).show().select2()
                                item.find("select.stock_type").val(value['stock_type']).show().select2()
                                item.find("input.account_title").val(value['detail_sub_title'])
                                item.find("input.with_child").prop("checked", value['with_child'] == 1 ? true : false)
                                if (value['sub_type'] != 1) {
                                    item.find("select.trans_type").val([]).prop('readonly', true).select2()
                                } else {
                                    item.find("select.trans_type").prop('readonly', false).select2()
                                }
                                if (value['sub_type'] == 3) {
                                    $(".stock_div",item).removeClass("hidden")
                                    $(".trans_div",item).addClass("hidden")
                                    $('.acc_div', item).addClass("hidden")
                                } else {
                                    $(".stock_div",item).addClass("hidden")
                                    $(".trans_div",item).removeClass("hidden")
                                    $(".acc_div", item).removeClass("hidden")
                                }
                                item.appendTo($("#row-divs"))
                                $(".select2-container", item).show()




                            }
                            y++;
                        });
                    }
                }
            })

            $("#add_block").modal("show")

        })


    })





</script>
