<style>

    .dropdown-toggle {
        background-color:#4c0870;
        border-color : #4c0870;
    }

    .dropdown-toggle:hover{
        background-color:#4c0870;
        border-color : #4c0870;
    }

    #tableWithSearch thead tr >th{
        background-color:#4c0870;
        color:#fff;
        /*width: 6% !important;*/
        font-size:12px;
    }
    #tableWithSearch thead tr th[class*='sorting_'] {
        color: #fff;
    }
    .bg-master-lighter {
        border:none;
        background-color:#fff;
        border-top:1px solid #4c0870;  
    }
    .btn-group {
        /*        background-color:#ed9421 !important;*/
        color:#fff;
    }
    .btn-default{
        border: none;
        background-color: #eee;
    }
    .btn-default:hover{
        background-color:#ed9421 !important;
        color:#fff;
        border: none;
    }
    /*    .table-responsive {

    overflow-x: visible; 
    }*/
    .dropdown-menu>li>a:hover, .dropdown-menu>li>a:focus{
        background-color:#ed9421 !important; ;
        color:#fff;

    }
    .btn-default:hover{
        background-color:#ed9421 !important;
        color:#fff;
        border: none;
    }
    /*    .complete ,.btn-group  .active{
    background-color:#ed9421 !important;
    color:#fff;
    }*/
    /*    .MonthPicker,.YearPicker,.FromToPicker,#PrePaidList_filter{
    display:none;
    }*/
    /*    #selected_entry{
    height:90px;
    color: black;
    margin-top:20px;
    font-size: 16px;
    padding-top: 2%;
    }*/
    /*    .select_table td{
    padding-left: 70px;
    }*/


</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="clearfix"></div>
        </div>
        <div class="panel-body">
            <div class="row form-group">
                <input type="hidden" name="is_activity" id="is_activity" value="<?php echo $is_activity;?>"/>
                <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_jv') ?></label></div>
                                            <div class="col-md-6"><label><?php echo CommonLanguage::getMessage('to_jv') ?></label></div>
                                        </div>
                                        <div class="row">
                                            <div class="col-md-6"><input type="number" min="1" id="from_jv" class="form-control full-width"></div>
                                            <div class="col-md-6"><input type="number" min="1" id="to_jv" 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_account') ?></label></div>
                                            <div class="col-md-6"><label><?php echo CommonLanguage::getMessage('to_account') ?></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>
                                <div class="row p-15" >
                                    <div class="col-md-3">
                                        <label><?php echo CommonLanguage::getMessage('job') ?></label>
                                        <input type="hidden" class="full-width" id="job_ids"/>
                                    </div>

                                    <div class="col-md-3">
                                        <label><?php echo CommonLanguage::getMessage('multi_accounts') ?></label>
                                        <input type="hidden" class="full-width" id="multi_accounts"/>
                                    </div>
                                    <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_currency"/>
                                        <?php // echo Forms::CurrencyDropDown(1, 'complete') ?>
                                    </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">
                                        <label><?php echo CommonLanguage::getMessage('entry_type') ?></label>
                                        <input type="hidden" class="full-width" id="ref_type"/>
                                    </div>
                                    <div class="col-md-3">
                                        <label><?php echo CommonLanguage::getMessage('family_accounts') ?></label>
                                        <input type="hidden" class="full-width" id="family_acc"/>
                                    </div>
                                    <div class="col-md-3">
                                        <label><?php echo CommonLanguage::getMessage('type'); ?></label>
                                        <select id="transaction_type" class="full-width" multiple  data-init-plugin="select2">
                                            <option value=""><?php echo CommonLanguage::getMessage('All') ?></option>
                                            <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>
                                    <?php if ($IsActual) { ?>
                                        <div class="col-md-1">
                                            <label><?php print CommonLanguage::getMessage('actual_only'); ?></label>
                                            <input type="checkbox"  id="actual_only" name="actual_only"/>
                                        </div>
                                    <?php } ?>
                                </div>
                                <div class="row p-15" >
                                    <div class="col-md-3">
                                        <label><?php echo CommonLanguage::getMessage('projects') ?></label>
                                        <input type="hidden" class="full-width" id="project_ids"/>
                                    </div>
                                    <div class="col-md-3 " >
                                        <label><?php print CommonLanguage::getMessage('salesman'); ?></label>
                                        <input  id="salesman_id"  class="full-width" type="hidden" />

                                    </div>
                                    <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 class="row">
                <div class="table-responsive">
                    <table class="table table-bordered table-hover demo-table-search" id="tableWithSearch">
                        <thead>
                            <tr>
                                <th class="text-center" style="width:5%;"><?php print CommonLanguage::getMessage('jv'); ?></th>
                                <th class="text-center" ><?php print CommonLanguage::getMessage('subtype'); ?></th>
                                <th class="text-center" ><?php print CommonLanguage::getMessage('reference'); ?></th>
                                <th class="text-center" ><?php print CommonLanguage::getMessage('user_ref'); ?></th>
                                <th class="text-center" ><?php print CommonLanguage::getMessage($is_activity == 0 ? 'account' :'activity_code'); ?></th>
                                <th class="text-center" ><?php print CommonLanguage::getMessage($is_activity == 0 ? 'account_name' : 'activity_title'); ?></th>
                                <!--<th class="text-center" ><?php print CommonLanguage::getMessage('amount'); ?></th>-->

                                <th class="text-center" ><?php print CommonLanguage::getMessage('description'); ?></th>
                                <th class="text-center" ><?php print CommonLanguage::getMessage('Date'); ?></th>
                                <th class="text-center" ><?php print CommonLanguage::getMessage('due_date'); ?></th>
                                <th class="text-center" ><?php print CommonLanguage::getMessage('job'); ?></th>
                                <th class="text-center" ><?php print CommonLanguage::getMessage('debit'); ?></th>
                                <th class="text-center" ><?php print CommonLanguage::getMessage('credit'); ?></th>
                                <th class="text-center" ><?php print CommonLanguage::getMessage('debit') . ' ' . smc_session()['local_currency_symbol']; ?></th>
                                <th class="text-center" ><?php print CommonLanguage::getMessage('credit') . ' ' . smc_session()['local_currency_symbol']; ?></th>
                                <?php if (smc_session()['default_setting']['activate_second_currency'] == 1) { ?>
                                    <th class="text-center" ><?php print CommonLanguage::getMessage('debit') . ' ' . smc_session()['second_currency']['symbol']; ?></th>
                                    <th class="text-center" ><?php print CommonLanguage::getMessage('credit') . ' ' . smc_session()['second_currency']['symbol']; ?></th>
                                <?php } ?>
                <!--<th class="text-center"><?php print CommonLanguage::getMessage('Employee'); ?></th>-->
                                <th class="text-center no-print"><?php print CommonLanguage::getMessage('Action'); ?></th>
                            </tr>
                        </thead>
                        <tbody>

                        </tbody>
                        <tfoot>
                            <tr>
                                <th></th><th></th><th></th><th></th> <th></th><th></th><th></th><th></th> <th></th><th></th><th></th>
                                <th><?php echo CommonLanguage::getMessage('total') . ':' ?></th>
                                <th><span id="total_debit"></span>&nbsp&nbsp<span class="CurrencyIndicator"><?php echo smc_session()['local_currency_symbol'] ?></span></th>
                                <th><div><span id="total_credit"></span>&nbsp&nbsp<span class="CurrencyIndicator"><?php echo smc_session()['local_currency_symbol'] ?></span></div></th>
                                <?php if (smc_session()['default_setting']['activate_second_currency'] == 1) { ?>
                                    <th><span id="total_debit_second"></span>&nbsp&nbsp<span class="CurrencyIndicator"><?php echo smc_session()['second_currency']['symbol'] ?></span></th>
                                    <th><div><span id="total_credit_second"></span>&nbsp&nbsp<span class="CurrencyIndicator"><?php echo smc_session()['second_currency']['symbol'] ?></span></div></th>
                                <?php } ?>
                                <th></th>
                            </tr>
                            <tr>
                                <th></th><th></th><th></th><th></th> <th></th><th></th><th></th><th></th> <th></th><th></th><th></th>
                                <th><?php echo CommonLanguage::getMessage('balance') . ':' ?></th>
                                <th><div class="text-center"><span id="total_balance"></span>&nbsp&nbsp<span class="CurrencyIndicator"><?php echo smc_session()['local_currency_symbol'] ?></span></div></th>
                                <th></th>
                                <?php if (smc_session()['default_setting']['activate_second_currency'] == 1) { ?>

                                    <th><div class="text-center"><span id="total_balance_second"></span>&nbsp&nbsp<span class="CurrencyIndicator"><?php echo smc_session()['second_currency']['symbol'] ?></span></div></th>
                                    <th></th>
                                <?php } ?>
                                <th></th>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
    </div>

</div>
</div>

<script>

    var is_actual = <?php echo json_encode($IsActual); ?>,
            jv_id = <?php echo json_encode($jv_id); ?>,
            title = <?php echo json_encode($IsActual ? CommonLanguage::getMessage('act_daily_journals') : CommonLanguage::getMessage('daily_journal')); ?>,
            startDate = '', endDate = '',
            startDueDate = '', endDueDate = '';
    $(function () {
        build_select2($("#salesman_id"), "xhr/employeeSearch")
        build_select2($("#job_ids"), "ACCAjax/JobSearch", 1)
        build_select2($("#project_ids"), "ACCAjax/projectSearch", 1)
//        build_select2($("#job_ids"), "xhr/CommonSearch?col_id=job_id&col_name=job_name&table=tbl_accounting_jobs", 1)
//        build_select2($("#trans_currency"), "xhr/CommonSearch?col_id=Currency_ID&col_name=Currency_Name&table=tbl_common_currency", 1)
        build_select2($("#trans_currency"), "xhr/CurrencySearch", 1)
        build_select2($("#multi_accounts"), ("xhr/accIDSearch?type=Payment"), 1)
        build_select2($("#ref_type"), "xhr/CommonSearch?col_id=subtype_abbrv&col_name=subtype_abbrv&table=tbl_accounting_transaction_subtype", 1)
        build_select2($("#family_acc"), "xhr/CommonSearch?col_id=family_id&col_name=family_name&table=tbl_family_accounts", 1)
        new Switchery($("#actual_only").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')]
            },
            allowEmpty: true,
            //            autoApply: true,
            autoUpdateInput: false,
            locale: {
                cancelLabel: 'Clear'
            }
        }, function (start, end, label) {
//            console.log(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');
//            console.log('here')
            $(this).val(picker.startDate.format('YYYY-MM-DD') + ' - ' + picker.endDate.format('YYYY-MM-DD'));
        });
        $('input[name="range_date"]').on('cancel.daterangepicker', function (ev, picker) {
//            console.log('there')
            window.startDate = ''
            window.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) {
//            window.startDueDate = start.format("YYYY-MM-DD"),
//                    window.endDueDate = end.format("YYYY-MM-DD");
        });
        $('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) {
            window.startDueDate = ''
            window.endDueDate = ''
            $(this).val('');
        });

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

        if (jv_id != 0) {
            $("#from_jv").val(jv_id),
                    $("#to_jv").val(jv_id)
        }
        ChangeTable()
    })




    function ChangeTable() {
        var MyData = {
            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(),
            job_ids: $("#job_ids").val(),
            project_ids: $("#project_ids").val(),
            multi_accounts: $("#multi_accounts").val(),
            Transaction_Date_From: startDate,
            Transaction_Date_To: endDate,
            due_date_from: startDueDate,
            due_date_to: endDueDate,
            trans_currency: $("#trans_currency").val(),
            from_amount: $("#from_amount").val(),
            to_amount: $("#to_amount").val(),
            ref_type: $("#ref_type").val(),
            family_acc: $("#family_acc").val(),
            salesman_id: $("#salesman_id").val(),
            is_activity: $("#is_activity").val(),
            jv_id: jv_id,
            actual_only: $("#actual_only").is(":checked") ? 1 : 0
        }

        buildTotals(MyData)
        var optns = {
            cb_ft: function (dt_optns) {
                // stop sorting for column since on click on header checkbox the sorting is triggered
                dt_optns.columnDefs.push({targets: [0], "orderable": false});
            },
        };
        RebuildDataTable(title, [], 'ACCAjax/dailyJVsRequest', MyData, '#tableWithSearch', [0, 1, 7, 8, 9], optns)
    }

    function buildTotals(data) {


        $.ajax({
            url: "ACCAjax/getdilyJVsTotals",
            method: "GET",
            dataType: 'json',
            data: {data: data},
            complete: function (v1, v2) {
                if (v2 == "success") {
                    var total_arr = v1.responseJSON;

                    $("#total_debit").html(AddCommas(public_round_number(parseFloat(total_arr['Debit']['amount'] || 0))))
                    $("#total_credit").html(AddCommas(public_round_number(parseFloat(total_arr['Credit']['amount'] || 0))))
                    $("#total_balance").html(AddCommas(public_round_number(parseFloat((total_arr['Debit']['amount'] - total_arr['Credit']['amount']) || 0))))

                    $("#total_debit_second").html(AddCommas(public_round_number(parseFloat(total_arr['Debit']['second_amount'] || 0))))
                    $("#total_credit_second").html(AddCommas(public_round_number(parseFloat(total_arr['Credit']['second_amount'] || 0))))
                    $("#total_balance_second").html(AddCommas(public_round_number(parseFloat((total_arr['Debit']['second_amount'] - total_arr['Credit']['second_amount']) || 0))))
                }
            }
        })
    }

</script>