
<style>
    .table thead tr >th{

        background-color:#4c0870;
        /*        width:16% !important;*/
        color:#fff;
    }
    .table-responsive{
        /*        overflow-x:auto !important;*/
    }

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


    .ValSpan {
        opacity:0;
    }

    /*
    .dropdown-menu>li>a{
    color:#000 !important;
    opacity: 1;
    }
    .DropDownForForms > .dropdown-menu{
    height:auto;
    cursor: pointer;
    overflow-y:auto;
    }
    */
    #Main_Account_Name,.Account_Name{
        text-transform: capitalize;
    }
    .newitem{

        font-size: 1.5em;
        text-align: center;
    }

</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 print $title; ?></div>
            <div class="row form-group">
                <div class="card-group horizontal " id="accordion" role="tablist" aria-multiselectable="true">
                    <div class="card card-default m-b-0">
                        <div class="card-header " role="tab" id="headingOne">
                            <h4 class="card-title">
                                <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne" class="collapsed btn btn-search">
                                    <span class="filter-title"> <i class="fa fa-search"></i> <?php  echo CommonLanguage::getMessage('advanced_search');?></span>
                                </a>
                            </h4>
                        </div>
                        <div id="collapseOne" class="collapse" role="tabcard" aria-labelledby="headingOne" aria-expanded="false" style="">
                            <div class="card-block">
                                <div class="row p-15" >
                                    <div class="col-md-3">
                                        <label><?php echo CommonLanguage::getMessage('Date') ?></label>
                                        <input id="range_date" type="text" class="form-control" readonly name="range_date" >
                                    </div>
                                    <div class="col-md-3">
                                        <label><?php echo CommonLanguage::getMessage('due_date') ?></label>
                                        <input id="range_duedate" type="text" class="form-control" readonly name="range_duedate" >
                                    </div>


                                    <div class="col-md-3">
                                        <div class="row">
                                            <div class="col-md-6"><label><?php echo CommonLanguage::getMessage('from_code') ?></label></div>
                                            <div class="col-md-6"><label><?php echo CommonLanguage::getMessage('to_code') ?></label></div>
                                        </div>
                                        <div class="row">
                                            <div class="col-md-6"><input type="number" min="1" id="from_account" class="form-control full-width"></div>
                                            <div class="col-md-6"><input type="number" min="1" id="to_account" class="form-control full-width"></div>
                                        </div>

                                    </div>
                                    <div class="col-md-3">
                                        <div class="row">
                                            <div class="col-md-6"><label><?php echo CommonLanguage::getMessage('from_amount') ?></label></div>
                                            <div class="col-md-6"><label><?php echo CommonLanguage::getMessage('to_amount') ?></label></div>
                                        </div>
                                        <div class="row">
                                            <div class="col-md-6"><input type="number" min="0" step="any" id="from_amount" class="form-control full-width"></div>
                                            <div class="col-md-6"><input type="number" min="0" step="any" id="to_amount" class="form-control full-width"></div>
                                        </div>

                                    </div>
                                </div>
                                <div class="row p-15" >

                                    <div class="col-md-3 trans_curr_div">
                                        <label><?php echo CommonLanguage::getMessage('trans_in_currency'); ?></label>
                                        <input type="hidden" class="full-width" id="trans_in_currrency"/>
                                        <?php // echo Forms::CurrencyDropDown(1, 'complete') ?>
                                    </div>
                                    <div class=" col-md-3">
                                        <label><?php print CommonLanguage::getMessage('currency'); ?></label>
                                        <?php echo Forms::CurrencyDropDown(1, 'complete') ?>
                                    </div>
                                    <div class="col-md-1">

                                        <label><?php print CommonLanguage::getMessage('active'); ?></label>
                                        <input type="checkbox" checked id="Active" name="Active"/>
                                    </div>
                                    <div class="col-md-2">
                                        <label><?php print CommonLanguage::getMessage('enter_nb_digits'); ?></label>
                                        <input type="number" id="digits" min="2" value="20" class="form-control" >
                                    </div>
                                    <div class="col-md-2">
                                        <label><?php print CommonLanguage::getMessage('enter_nb_of_grouping'); ?></label>
                                        <input type="number" id="Grouping" min="3" value="3" class="form-control" >
                                    </div>
                                    <div class="col-md-2">
                                        <label><?php print CommonLanguage::getMessage('project'); ?></label>
                                        <input type="hidden" id="project_id"  name="project_id" class="full-width" >
                                    </div>
                                </div>
                                <div class="row p-15">


                                </div>
                                <div class="row p-15" >
      

                                    <div class="col-md-2" style="display: grid;">
                                        <label><?php print CommonLanguage::getMessage('opening_balance'); ?></label>
                                        <input type="checkbox" checked id="opening_balance" name="opening_balance"/>
                                    </div>
                                    <div class="col-md-2" style="display: grid;">
                                        <label><?php print CommonLanguage::getMessage('close_balance'); ?></label>
                                        <input type="checkbox" checked id="close_balance" name="close_balance"/>
                                    </div>
                                    <?php if ($Is_Actual) { ?>
                                    <div class="col-md-1">
                                        <label><?php print CommonLanguage::getMessage('actual_only'); ?></label>
                                        <input type="checkbox"  id="Actual_TrialBalance" name="Actual_TrialBalance"/>
                                    </div>
                                    <?php } ?>
                                </div>

                                <div class="row p-15" >
             
                                </div>
                                <div class="row p-15" >
                                    <div class="col-md-2 pull-right">
                                        <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>
                        </div>
                    </div>


                </div>
            </div>

        </div>
        <div class="panel-body">
            <div class=" table-responsive"  id="TrialBalanceDiv">
            </div>
        </div>
    </div>
</div>



<script>
    var trial_balance = <?php echo json_encode(CommonLanguage::getMessage('trial_balance')); ?>;
    var fromdate = <?php echo json_encode($from_day) ?>, today = <?php echo json_encode($today) ?>;
    var is_actual = <?php echo json_encode($Is_Actual); ?>;
    var startDate = '', endDate = '', startDueDate = '', endDueDate = '';

    $(function () {
        TrailBalanceAjax()
        build_select2($("#trans_in_currrency"), "xhr/CurrencySearch", 1)
        build_select2($("#project_id"), "xhr/commonSearch?table=tbl_projects&col_id=project_id&col_name=project_name")

        new Switchery($("#Actual_TrialBalance").get(0), {color: $.Pages.getColor("complete")})
        new Switchery($("#opening_balance").get(0), {color: $.Pages.getColor("complete")})
        new Switchery($("#close_balance").get(0), {color: $.Pages.getColor("complete")})
        new Switchery($("#Active").get(0), {color: $.Pages.getColor("complete")})


        $("input[name=range_date]").daterangepicker({
            ranges: {
                'Today': [moment(), moment()],
                'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                'Last 7 Days': [moment().subtract(6, 'days'), moment()],
                'Last 30 Days': [moment().subtract(29, 'days'), moment()],
                'This Month': [moment().startOf('month'), moment().endOf('month')],
                'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
            }, startDate: startDate,
            allowEmpty: true,
            autoUpdateInput: false,
        }, function (start, end, label) {
        });
        $('input[name="range_date"]').on('apply.daterangepicker', function (ev, picker) {
            window.startDate = picker.startDate.format('YYYY-MM-DD'),
                window.endDate = picker.endDate.format('YYYY-MM-DD');
            $(this).val(picker.startDate.format('YYYY-MM-DD') + ' - ' + picker.endDate.format('YYYY-MM-DD'));
        });
        $('input[name="range_date"]').on('cancel.daterangepicker', function (ev, picker) {
            startDate = ''
            endDate = ''
            $(this).val('');
        });

        $("input[name=range_duedate]").daterangepicker({
            ranges: {
                'Today': [moment(), moment()],
                'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                'Last 7 Days': [moment().subtract(6, 'days'), moment()],
                'Last 30 Days': [moment().subtract(29, 'days'), moment()],
                'This Month': [moment().startOf('month'), moment().endOf('month')],
                'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
            }, allowEmpty: true,
            //            autoApply: true,
            autoUpdateInput: false,
            locale: {
                cancelLabel: 'Clear'
            }
        }, function (start, end, label) {

        });
        $('input[name="range_duedate"]').on('apply.daterangepicker', function (ev, picker) {
            window.startDueDate = picker.startDate.format('YYYY-MM-DD'),
                window.endDueDate = picker.endDate.format('YYYY-MM-DD');
            $(this).val(picker.startDate.format('YYYY-MM-DD') + ' - ' + picker.endDate.format('YYYY-MM-DD'));
        });
        $('input[name="range_duedate"]').on('cancel.daterangepicker', function (ev, picker) {
            startDueDate = ''
            endDueDate = ''
            $(this).val('');
        });

    });

    $("#Generate_Report").on("click", function () {
        TrailBalanceAjax()
    })
    function TrailBalanceAjax() {


        var CurrentDate = {
            From_Date: startDate,
            To_Date: endDate,
            digits: $("#digits").val(),
            Grouping: $("#Grouping").val(),
            Currency_ID: $("#CurrencyDropDown button input").val(),
            Opening_Balance: $("#opening_balance").is(":checked") ? 1 : 0,
            with_close: $("#close_balance").is(":checked") ? 1 : 0,
            Active: $("#Active").is(":checked") ? 1 : 0,
            unmoved: $("#unmoved").is(":checked") ? 1 : 0,
            Actual_Only: $("#Actual_TrialBalance").is(":checked") ? 1 : 0,
            trans_type: $("#transaction_type").val(),
            Is_Actual: is_actual,
            from_jv: $("#from_jv").val(),
            to_jv: $("#to_jv").val(),
            from_account: $("#from_account").val(),
            to_account: $("#to_account").val(),
            multi_accounts: $("#multi_accounts").val(),
            Transaction_Date_From: startDate,
            Transaction_Date_To: endDate,
            trans_currency: $("#trans_in_currrency").val(),
            from_amount: $("#from_amount").val(),
            to_amount: $("#to_amount").val(),
            ref_type: $("#ref_type").val(),
            family_acc: $("#family_acc").val(),
            debit_credit_type: $("#TypeTrans").val(),
            project_id: $("#project_id").val(),
            due_date_from: startDueDate,
            due_date_to: endDueDate
        };

        $("#AjaxIsBusy").removeClass("HideMe")
        $.ajax({
            url: "ACCAjax/activity_trial_balance_ajax",
            data: {data: CurrentDate},
            complete: function (v, vv) {
                if (vv == "success") {
                    var Data = v.responseText;
                    $("#TrialBalanceDiv").html(Data)
                    RebuildDataTable('Trial Balance', [0, 1, 2, 3, 4, 5, 6, 7], '', [], '#TrialBalanceTable', [],
                                     {bSort: false

                                     }, '<h4>From Date: ' + (startDate ? startDate : fromdate) + '<br>To Date: ' + (endDate ? endDate : today) + '<br>Currency: ' + $("#CurrencyDropDown button span").text());
                }
            }
        })//end of ajax complete
    }


    function FixFloatVal(what) {
        return what.replace(/\.0$|\.00$|\.000$/, "");
    }

    // to remove many digits after the period
    $(function () {
        $(".ValSpan").each(function () {
            $(this).html(AddCommas( public_round_number(parseFloat($.trim($(this).text()))))).animate({opacity: 1})
        })
    })


</script>


