<style>
    #tableWithSearch thead tr >th{
        background-color:#4c0870;
        color:#fff;
    }
    #tableWithSearch thead tr th[class*='sorting_'] {
        color: #fff;
    }
    .bg-master-lighter {
        border:none;
        background-color:#fff;
        border-top:1px solid #ddd;  
    }
    .datepicker thead tr .dow {
        color:#4c0870  !important;
    }
    .datepicker table tr td.day:hover {
        background: #4c0870  !important;
        color:#fff;
    }
    .datepicker table tr td.active {
        background-color: #4c0870  !important;
    }
    .datepicker thead tr .datepicker-switch {
        color: #4c0870   !important;
        font-size: 13px;
    }
    .datepicker table tr td span.active {
        background-color: #4c0870  !important;
    }
    .datepicker table tr td span:hover {
        background-color: #4c0870  !important;
        color:#fff;
    }
    datepicker thead tr .next:before, .datepicker thead tr .prev:before {
        color: #4c0870  !important;
        font-family: 'FontAwesome';
        font-size: 10px;
    }
    .datepicker thead tr .next:before, .datepicker thead tr .prev:before {
        color: #4c0870  !important;
        font-family: 'FontAwesome';
        font-size: 10px;
    }
    /*
    .btn-default:hover{
    background-color:#ed9421 !important;
    color:#fff;
    border: none;
    }
    */
    .complete ,.btn-group  .active{
        background-color:#ed9421 !important;
        color:#fff;
    }
    /*    .MonthPicker,.YearPicker,.FromToPicker{
            display:none;
        }*/
    #selected_entry{
        height:90px;
        color: black;
        margin-top:20px;
        font-size: 16px;
        padding-top: 2%;
    }
    .select_table td{
        padding-left: 70px;
    }
    .dataTables_wrapper .dataTables_paginate .paginate_button.current, .dataTables_wrapper .dataTables_paginate .paginate_button.current:hover,.dataTables_wrapper .dataTables_paginate .paginate_button:hover{
        color:#fff !important;
        border-radius: 50%;
        background: #ed9421 !important;
        border-color: #4c0870
    }
    .dataTables_wrapper .dataTables_paginate .paginate_button.disabled:hover{
        color:#fff !important;
    }
    #tableWithSearch tbody td{
        text-align: initial !important;
    }

</style>

<script src="<?php echo THEME_PATH; ?>/plugins/moment/moment.min.js"></script>
<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title"> <?php echo CommonLanguage::getMessage('view_dynamic_report'); ?> </div>
            <div class="row form-group">
                <div class="col-md-12 ">

                    <div class="col-md-3" >
                        <label><?php echo CommonLanguage::getMessage('Date') ?></label>
                        <input id="From_To_Date" type="text" class="form-control" name="From_To_Date" >
                    </div> 
                    <div class="col-md-3 <?php echo!empty($report_id) ? 'hidden' : '' ?>" >
                        <label><?php echo CommonLanguage::getMessage('report') ?></label>
                        <input type="hidden" id="report_id" class="full-width" value="<?php echo!empty($report_id) ? $report_id : '' ?>" data-select-name="<?php echo (!empty($report_name) ? $report_name : '') ?>"/>
                    </div>
                    <div class=" col-md-3">
                        <label><?php print CommonLanguage::getMessage('currency'); ?></label>
                        <?php echo Forms::CurrencyDropDown(1, 'complete') ?>
                    </div>
                    <?php if ($is_actual) { ?>
                        <div class="col-md-2" style="display:grid;">
                            <label><?php print CommonLanguage::getMessage('actual_only'); ?></label>
                            <input type="checkbox"  id="actual_only" name="actual_only"/>
                        </div>
                    <?php } ?>
                    <div class="col-md-2 " style="    margin-top: 24px;">
                        <label><?php echo CommonLanguage::getMessage('inverse_report')?></label>
                        <input type="checkbox" id="inverse_report"/>
                    </div>
                    <div class="col-md-3 " style="    margin-top: 24px;">
                        <button id="Generate_Report" type="button" class="btn btn-complete pull-right" data-toggle="dropdown" aria-expanded="false">
                            <?php print CommonLanguage::getMessage('generate_report'); ?></button>
                    </div>

                </div>
                <div class="col-md-12 ">
                    <div class="col-md-3 hidden" >
                        <label><?php echo CommonLanguage::getMessage('type') ?></label>
                        <select id="type" class="full-width" data-init-plugin="select2">
                            <option value="0"><?php echo CommonLanguage::getMessage('All') ?></option>
                            <option value="1"><?php echo CommonLanguage::getMessage('only_totals') ?></option>
                            <option value="2"><?php echo CommonLanguage::getMessage('only_details') ?></option>
                        </select>
                    </div>




                </div>
                <?php
//                  print_r($stock_blocks);
                if (!empty($stock_blocks)) {
//                    print_r($stock_blocks);
                    foreach ($stock_blocks as $detail_id => $row) {
                        ?>
                        <div class="col-md-12 stock_rows" >
                            <div class="col-md-3" >
                                <label><?php echo $row['detail_sub_title'] ?></label>
                                <select data-id="<?php echo $detail_id ?>" data-type="<?php echo $row['trans_types']?>" name="stock_type" class="full-width stock_type" data-init-plugin="select2">
                                    <option value="0" type="acc"> <?php echo $row['trans_types'] == 'begin' ? $begining_acc : $end_acc; ?></option>
                                    <option value="manual" type="manual"><?php echo CommonLanguage::getMessage('manual') ?></option>
                                    <?php foreach ($methods as $method): ?>
                                        <option  value= "<?php echo $method['Pricing_Method_ID']; ?>" type="method" ><?php echo $method['Pricing_Method_Name']; ?></option>
                                    <?php endforeach; ?>
                                    <option  data-type-id= "0" type="last_cost" data-currency="<?php echo smc_session()['local_currency_symbol'] ?>"><?php echo CommonLanguage::getMessage('last_cost') ?></option>

                                </select>

                            </div>
                            <div class="col-md-3 hidden stock_value_div">
                                <label><?php echo CommonLanguage::getMessage('stock_value') ?></label>
                                <input type="number" step="any" class="form-control stock_value" name="stock_value" >

                            </div>        
                        </div>        
                        <?php
                    }
                }
                ?>
            </div>
        </div>
        <div class="panel-body">

            <table class="table table-bordered table-hover demo-table-search" id="tableWithSearch">
                <thead>
                    <tr>
                        <th  style="width:15%" > <?php print CommonLanguage::getMessage('Name'); ?></th>
                        <th class="text-center" style="width:10%"> <?php print CommonLanguage::getMessage('total_debit'); ?></th>
                        <th class="text-center" style="width:10%"> <?php print CommonLanguage::getMessage('total_credit'); ?></th>
                        <th class="text-center" style="width:10%"> <?php print CommonLanguage::getMessage('operation'); ?></th>
                        <th class="text-center" style="width:10%"> <?php print CommonLanguage::getMessage('balance'); ?></th>
                    </tr>
                </thead>
                <tbody>

                </tbody>
<!--                <tfoot>
                <th></th><th></th>
                <th><span class="total_debit">0</span>&nbsp;<span class="currency-symbol"><?php echo $defCurrencySymbol ?></span></th>
                <th><span class="total_credit">0</span>&nbsp;<span class="currency-symbol"><?php echo $defCurrencySymbol ?></span></th>
                <th></th>
                <th><span class="total">0</span>&nbsp;<span class="currency-symbol"><?php echo $defCurrencySymbol ?></span></th>
                </tfoot>-->
            </table>
        </div>

    </div>
</div>

<script>
    var IsActual = <?php echo $is_actual ? "true" : "false" ?>;
    var title = <?php echo json_encode(CommonLanguage::getMessage('view_dynamic_report')); ?>;
    var today = <?php echo json_encode($today) ?>;
    $(function () {
        build_select2($("#report_id"), "xhr/search_report_by_role")
        new Switchery($("#with_details").get(0), {color: $.Pages.getColor("complete")})
        new Switchery($("#only_totals").get(0), {color: $.Pages.getColor("complete")})
        new Switchery($("#actual_only").get(0), {color: $.Pages.getColor("complete")})
        new Switchery($("#inverse_report").get(0), {color: $.Pages.getColor("complete")})

        $("input[name=From_To_Date]").daterangepicker({
        }, function (start, end, label) {
            window.startDate = start.format("YYYY-MM-DD"),
                    window.endDate = end.format("YYYY-MM-DD")

        });
        $(".stock_type").on("change", function () {
            var parent = $(this).parents(".stock_rows");
            if ($(this).val() === 'manual')
                $(".stock_value_div", parent).removeClass('hidden');
            else
                $(".stock_value_div", parent).addClass('hidden');
        });
        $("#Generate_Report").on("click", function () {
            ChangeTable()
        })
        $("#report_id").on("change", function () {
            $.ajax({
                url: "ACCAjax/get_dynamic_report_by_id",
                data: {id: $(this).val()},
                dataType: 'JSON',
                complete: function (v1, v2) {
                    if (v2 == 'success') {
                        $("#type").val(v1.responseJSON['main']['type']).select2()
                        $("#CurrencyDropDown button input").val(v1.responseJSON['main']['Currency_ID'])
                        $("#CurrencyDropDown button span:eq(0)").html(v1.responseJSON['main']['Currency_Name'] + ' - ' + v1.responseJSON['main']['Currency_Code'])
                    }
                }
            })
        })
    })
//get_dynamic_report_by_id
    function ChangeTable() {
        var data = {
            report_id: $("#report_id").val(),
            is_actual: IsActual ? 1 : 0,
            actual_only: $("#actual_only").is(":checked") ? 1 : 0,
            inverse_report: $("#inverse_report").is(":checked") ? 1 : 0,
            currency_id: $("#CurrencyDropDown button input").val(),
            from_date: window.startDate ? window.startDate : today,
            to_date: window.endDate ? window.endDate : today,
            type: $("#type").val(),
            stock_arr:[]
        };
        $(".stock_rows").each(function(){
            data.stock_arr.push({
                detail_id:$("select.stock_type",$(this)).attr("data-id"),
                stock_type:$("select.stock_type",$(this)).val(),
                stock_value:$("input.stock_value",$(this)).val(),
                method:$("select.stock_type option:selected",$(this)).attr("type"),
                stock_period:$("select.stock_type",$(this)).attr("data-type")
                
            })
        })
        $.ajax({
            url: "ACCAjax/build_dynamic_report",
            data: data,
            dataType: 'JSON',
            complete: function (v1, v2) {
                if (v2 == 'success') {
//                    $("#tableWithSearch tbody").empty()
                    $("#tableWithSearch").DataTable().destroy();
                    $("#tableWithSearch tbody").empty().html(v1.responseJSON['html'])
                    RebuildDataTable('', [], "", {}, '', '', {
                        initComplete: function (settings, json) {
                            var symbol = $("#CurrencyDropDown ul li[value=" + $("#CurrencyDropDown button input").val() + "]").attr("mysymbol");
                            $(".currency-symbol").html(symbol)
//                            console.log(v1.responseJSON['total'])
                            $(".total_debit").html(AddCommas(public_round_number(v1.responseJSON['data']['total_debit'])));
                            $(".total_credit").html(AddCommas(public_round_number(v1.responseJSON['data']['total_credit'])));
                            $(".total").html(AddCommas(public_round_number(v1.responseJSON['data']['total'])));
                        }
                    });
                }
            }
        })
//         RebuildDataTable(title, [], 'ACCAjax/customer_supplier_vat_report', Date,'#tableWithSearch')

    }


</script>
